forked from Dash-Industry-Forum/dash.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githook.js
44 lines (35 loc) · 1016 Bytes
/
githook.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
const fs = require('fs');
const path = require('path');
const precommitTemplate = `#!/usr/bin/env node
var exec = require('child_process').exec;
exec('npm run lint', {
cwd: '${__dirname.toString().replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}'
}, function (err, stdout, stderr) {
var exitCode = 0;
if (err) {
console.log(stderr || err);
exitCode = -1;
}
process.exit(exitCode);
}).stdout.on('data', function (chunk){
process.stdout.write(chunk);
});
`;
const pathToHooksFolder = path.join(`${__dirname}`, '.git', 'hooks');
function writeHook() {
const precommitFile = path.join(pathToHooksFolder, 'pre-commit');
fs.writeFile(precommitFile, precommitTemplate, { mode: 0o755 }, (err) => {
if (err) throw err;
console.log(`${precommitFile} created.`);
});
}
fs.access(pathToHooksFolder, (err) => {
if (err) {
fs.mkdir(pathToHooksFolder, { recursive: true }, (err) => {
if (err) throw err;
writeHook();
});
} else {
writeHook();
}
});