-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·84 lines (75 loc) · 1.73 KB
/
index.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
84
#!/usr/bin/env node
import inquirer from 'inquirer';
import { startCompress } from './compress.js';
import { mb2b } from './utils.js';
// 获取命令执行路径
const cwd = process.cwd();
const DEFAULT_SIZE = 5;
inquirer
.prompt([
{
type: 'checkbox',
message: 'Select image format',
name: 'ext',
default: ['.jpg', '.jpeg', '.png'],
choices: [
{
name: '.jpg',
},
{
name: '.jpeg',
},
{
name: '.png',
},
{
name: '.bmp',
},
{
name: '.webp',
},
],
validate(answer) {
if (answer.length < 1) {
return 'You must choose at least one image format.';
}
return true;
},
},
{
type: 'input',
name: 'maxSize',
message: 'Input image max size (MB).',
default() {
return DEFAULT_SIZE;
},
validate(value = DEFAULT_SIZE) {
const pass = value.toString().match(/^[1-9]{1,}\d*$/) && value <= DEFAULT_SIZE;
if (pass) {
return true;
}
return 'Please enter a valid max size';
},
},
{
type: 'confirm',
name: 'isOverwrite',
message: 'Whether to overwrite the source file?',
default: false,
},
{
type: 'confirm',
name: 'isRecursion',
message: 'Whether recursion?',
default: false,
},
])
.then(answers => {
const { ext, maxSize, isOverwrite, isRecursion } = answers;
// TODO 不想使用透传,所以将参数设置为全局变量
global.ext = ext;
global.maxSize = mb2b(maxSize);
global.isOverwrite = isOverwrite;
global.isRecursion = isRecursion;
startCompress(cwd);
});