-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·97 lines (91 loc) · 3.12 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
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
const fs = require('fs');
const exec = require('child_process').exec;
const args = process.argv.splice(2);
if (args.length === 1) {
switch (args[0]) {
case '-h':
printHelp();
break;
case 'cancel':
cancelChecker();
break;
case 'cx':
exec('git rev-parse --show-toplevel', function (error, stdout, stderr) {
if (error) {
console.error(`执行错误,获取项目路径错误: ${error}`);
return;
}
const absPath = stdout.trim();
fs.chmod(`${absPath}/.git/hooks/commit-msg.js`, 0o775, function (err) {
if (err) console.error(err);
})
});
break;
default:
break;
}
return;
} else if(args.length>1){
console.error('命令错误;');
printHelp();
return;
}
function printHelp() {
console.log('帮助 -h:');
console.log('\tnpx checker-init: 增加检查;');
console.log('\tnpx checker-init cancel: 取消检查;');
console.log('\tnpx checker-init cx: 权限不足时使用此命令提升;');
}
function cancelChecker() {
exec('git rev-parse --show-toplevel', function (error, stdout, stderr) {
if (error) {
console.error(`执行错误,获取项目路径错误: ${error}`);
return;
}
const absPath = stdout.trim();
const commitHookPath = `${absPath}/.git/hooks/`;
fs.stat(commitHookPath, function (err) {
if (err) {
console.error(err);
console.error('请确认当前项目是否通过git管理源代码');
} else {
fs.unlink(`${commitHookPath}/commit-msg.js`, function (err) {
if (err) {
console.error(err);
} else {
console.log('已取消检查');
}
});
}
});
})
}
exec('git rev-parse --show-toplevel', function (error, stdout, stderr) {
if (error) {
console.error(`执行错误,获取项目路径错误: ${error}`);
return;
}
const absPath = stdout.trim();
fs.stat(`${absPath}/.git/hooks/`, function (err) {
if (err) {
console.error(err);
console.error('请确认当前项目是否通过git管理源代码');
} else {
const commitMsg = fs.readFileSync(
`${absPath}/node_modules/commit-msg-checker/commit-msg.js`
);
fs.writeFile(`${absPath}/.git/hooks/commit-msg`, commitMsg, function (err) {
if (err) {
console.error(err);
} else {
fs.chmod(`${absPath}/.git/hooks/commit-msg`, 0o775, function (err) {
if (err) console.error(err);
console.log('已为项目添加对commit msg的检查');
console.log('Happy Hacking!');
});
}
});
}
});
});