-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.js
86 lines (78 loc) · 2.46 KB
/
pretty.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
85
86
#!/usr/bin/env node
/* eslint-disable */
/**
* Module dependencies.
*/
const child_process = require('child_process');
const program = require('commander');
const sgf = require("staged-git-files");
const argv = process.argv;
program
.version('0.1.0')
.usage('npm run pretty [-option] <value>')
.option('-s, --staged', 'Pretty staged js & vue files')
.option('-a, --all', 'pretty all js & vue files')
.option('-f, --files [files]', 'pretty file by name')
.option('-d, --directory [directory]', 'pretty files by directory')
.on('--help', () => {
console.log(' Examples:\n');
console.log(' npm run pretty -- -s');
console.log(' npm run pretty -- -a');
console.log(' npm run pretty -- -f src/index.js');
console.log(' npm run pretty -- -d packages/date-picker/src\n');
})
.parse(argv);
if (argv.length === 2) {
program.help();
}
if(program.staged) {
// get staged files
// res = {
// filename: 'xxxxx',
// status: 'ADDED'
// }
sgf((err, res) => {
if (err) {
console.error(err);
return;
}
let files = res.filter((item) => {
return item.filename.search(/.js$|.vue$/) !== -1;
}).map((file) => {
return file.filename;
});
fileStr = files.join(' ');
console.log('Prettying staged js & vue files in the project:');
child_process.exec(`npm run lint-fix ${fileStr}`, (err, stdout, stderr) => {
console.log(stdout);
console.log('Pretty by eslint commpleted\n');
});
});
}
if (program.All) {
console.log('Prettying all js & vue files in the project:');
child_process.exec(`npm run lint-fix --ext .js,.vue src`, (err, stdout, stderr) => {
console.log(stdout);
console.log('Pretty by eslint commpleted\n');
});
}
if(program.files) {
if (argv.length === 3) {
program.help();
}
console.log('Prettying file(s) by name:');
child_process.exec(`npm run lint-fix ${program.files}`, (err, stdout, stderr) => {
console.log(stdout);
console.log('Pretty by eslint commpleted\n');
});
}
if(program.directory) {
if (argv.length === 3) {
program.help();
}
console.log('Prettying file by directory:');
child_process.exec(`npm run lint-fix ${program.directory}/**/*.{js,vue}`, (err, stdout, stderr) => {
console.log(stdout);
console.log('Pretty by eslint commpleted\n');
});
}