forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-argv.js
More file actions
272 lines (210 loc) · 5.7 KB
/
parse-argv.js
File metadata and controls
272 lines (210 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const { existsSync, lstatSync } = require('fs')
const { resolve, normalize, join } = require('path')
const untildify = require('untildify')
const getPngSize = require('./get-png-size')
const { warn } = require('./logger')
const generators = require('../generators')
const defaultParams = require('./default-params')
function die (msg) {
warn(msg)
warn()
process.exit(1)
}
function profile (value, argv) {
if (!value) {
return
}
const profilePath = resolve(process.cwd(), untildify(value))
if (!existsSync(profilePath)) {
die(`Profile param does not point to a file or folder that exists!`)
}
if (
!value.endsWith('.json') &&
!lstatSync(profilePath).isDirectory()
) {
die(`Specified profile (${value}) is not a .json file`)
}
argv.profile = profilePath
}
function mode (value, argv) {
const possibleValues = Object.keys(require('../modes'))
if (!value) {
argv.mode = possibleValues
return
}
const list = value.split(',')
if (list.includes('all')) {
argv.mode = possibleValues
return
}
if (list.some(mode => !possibleValues.includes(mode))) {
die(`Invalid mode requested: "${value}"`)
}
argv.mode = list
}
function include (value, argv) {
if (!value) {
return
}
const possibleValues = Object.keys(require('../modes'))
if (value.includes('all')) {
argv.include = possibleValues
return
}
if (value.some(mode => !possibleValues.includes(mode))) {
die(`Invalid include requested: "${value}"`)
}
}
function quality (value, argv) {
if (!value) {
argv.quality = defaultParams.quality
return
}
const numeric = parseInt(value, 10)
if (isNaN(numeric)) {
die(`Invalid quality level number specified`)
}
if (numeric < 1 || numeric > 12) {
die(`Invalid quality level specified (${value}) - should be between 1 - 12`)
}
argv.quality = numeric
}
function filter (value) {
if (value && !Object.keys(generators).includes(value)) {
die(`Unknown filter value specified (${value}); there is no such generator`)
}
}
function padding (value, argv) {
if (!value) {
argv.padding = [ 0, 0 ]
return
}
const sizes = (Array.isArray(value) ? value : value.split(','))
.map(val => parseInt(val, 10))
if (sizes.length > 2) {
die(`Invalid padding specified`)
}
sizes.forEach(size => {
if (isNaN(size)) {
die(`Invalid padding specified (not numbers)`)
}
if (size < 0) {
die(`Invalid padding specified (not all positive numbers)`)
}
})
argv.padding = sizes.length === 1
? [ sizes[0], sizes[0] ]
: sizes
}
function icon (value, argv) {
if (!value) {
warn(`No source icon file specified, so using the sample one`)
argv.icon = normalize(join(__dirname, '../../samples/icongenie-icon.png'))
return
}
const { appDir } = require('./app-paths')
argv.icon = resolve(appDir, untildify(value))
if (!existsSync(argv.icon)) {
die(`Path to source icon file does not exists: "${value}"`)
}
const { width, height } = getPngSize(argv.icon)
if (width === 0 && height === 0) {
die(`Icon source is not a PNG file!`)
}
if (width < 64 || height < 64) {
die(`Icon source file does not have the minimum 64x64px resolution`)
}
}
function background (value, argv) {
if (!value) {
return
}
const { appDir } = require('./app-paths')
argv.background = resolve(appDir, untildify(value))
if (!existsSync(argv.background)) {
die(`Path to background source file does not exists: "${value}"`)
}
const { width, height } = getPngSize(argv.background)
if (width === 0 && height === 0) {
die(`Background source file is not a PNG file!`)
}
if (width < 128 || height < 128) {
die(`Background source file does not have the minimum 128x128px resolution`)
}
}
function getColorParser (name, defaultValue) {
return (value, argv) => {
if (!value) {
argv[name] = argv.themeColor || defaultValue
return
}
if (
(value.length !== 3 && value.length !== 6) ||
/^[0-9A-Fa-f]+$/.test(value) !== true
) {
die(`Invalid ${name} color specified: "${value}"`)
}
argv[name] = '#' + value
}
}
function splashscreenIconRatio (value, argv) {
if (!value && value !== 0) {
argv.splashscreenIconRatio = defaultParams.splashscreenIconRatio
return
}
const numeric = parseFloat(value)
if (isNaN(numeric)) {
die(`Invalid splashscreen icon ratio number specified`)
}
if (numeric < 0 || numeric > 100) {
die(`Invalid splashscreen icon ratio specified (${value}) - should be between 0 - 100`)
}
argv.splashscreenIconRatio = numeric
}
function output (value) {
if (!value) {
die(`The "output" param is required`)
}
}
function assets (value, argv) {
const possibleValues = Object.keys(require('../modes'))
if (!value) {
argv.assets = []
return
}
const list = value.split(',')
if (list.includes('all')) {
argv.assets = possibleValues
return
}
if (list.some(mode => !possibleValues.includes(mode))) {
die(`Invalid assets requested: "${value}"`)
}
argv.assets = list
}
const parsers = {
profile,
mode,
quality,
filter,
padding,
icon,
background,
splashscreenIconRatio,
themeColor: getColorParser('themeColor'),
pngColor: getColorParser('pngColor', defaultParams.pngColor),
splashscreenColor: getColorParser('splashscreenColor', defaultParams.splashscreenColor),
svgColor: getColorParser('svgColor', defaultParams.svgColor),
include, // profile file param
output, // profile cmd
assets // profile cmd
}
module.exports = function (argv, list) {
list.forEach(name => {
const fn = parsers[name]
if (fn === void 0) {
die(`Invalid command parameter specified (${name})`)
}
fn(argv[name], argv)
})
}