forked from Dash-Industry-Forum/dash.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githook.cjs
50 lines (39 loc) · 1.27 KB
/
githook.cjs
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
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 callerTemplate = `#!/bin/sh
node .git/hooks/pre-commit.cjs;`
const pathToHooksFolder = path.join(`${__dirname}`, '.git', 'hooks');
function writeHook(name, content) {
const precommitFile = path.join(pathToHooksFolder, name);
fs.writeFile(precommitFile, content, { 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('pre-commit.cjs', precommitTemplate);
writeHook('pre-commit', callerTemplate);
});
} else {
writeHook('pre-commit.cjs', precommitTemplate);
writeHook('pre-commit', callerTemplate);
}
});