-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.js
83 lines (75 loc) · 1.75 KB
/
args.js
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
let fs = require('fs'),
_ = require('lodash'),
mkdirRecursive = require('mkdir-recursive'),
HandledError = require('./handled-error');
let args = require('yargs')
.option('dir', {
alias: 'd',
describe: 'The input images directory, current directory by default',
default: process.env.PWD,
type: 'string'
})
.option('out', {
alias: 'o',
describe: 'The output images directory, current directory by default',
default: process.env.PWD,
type: 'string'
})
.option('img', {
alias: 'i',
describe: 'The input image path',
type: 'string'
})
.option('quality', {
alias: 'q',
describe: 'The output images quality',
type: 'number',
default: 80
})
.option('scale', {
alias: 's',
describe: 'The output images scaled by ratio, 0...1',
type: 'number',
default: 0.5
})
.option('blur', {
alias: 'b',
describe: 'The output images blur value',
type: 'number',
default: 10
})
.option('brightness', {
alias: 'br',
describe: 'The output images brightness ratio, 0...1',
type: 'number',
default: 0.5
})
.help('h')
.argv;
class Args {
static get() {
this.validate();
return args;
}
static validate() {
try {
let {img, dir, out} = args;
if (img && !fs.existsSync(img)) {
throw new HandledError(`Image (${img}) doesn't exist`);
}
if (dir && !fs.existsSync(dir)) {
throw new HandledError(`Directory (${dir}) doesn't exist`);
}
// create output dir if doesn't exist
mkdirRecursive.mkdirSync(out);
return true;
} catch (err) {
if (err instanceof HandledError) {
console.log(`${err}`);
} else {
throw err;
}
}
}
};
module.exports = Args;