This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
githooks.js
104 lines (90 loc) · 2.69 KB
/
githooks.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
98
99
100
101
102
103
104
const jsonfile = require('jsonfile')
const packageJsonFilepath = `${process.cwd()}/package.json`;
const getPackageJson = () => {
const packageJson = jsonfile.readFileSync(packageJsonFilepath);
return packageJson;
}
const writePackageJsonFile = json => {
try {
jsonfile.writeFileSync(packageJsonFilepath, json(), {spaces: 2})
} catch (err) {
console.error(err)
}
};
const addScript = (json, config) => {
const name = config.name;
const value = config.value;
const newJson = JSON.parse(JSON.stringify(json));
if (!newJson.husky) {
newJson.husky = {};
}
if (!newJson.husky.hooks) {
newJson.husky.hooks = {};
}
if (!newJson.husky.hooks[name]) {
newJson.husky.hooks[name] = value;
}
else if (newJson.husky.hooks[name].indexOf(value) === -1) {
newJson.husky.hooks[name] = `${newJson.husky.hooks[name]} && ${value}`;
}
return newJson;
}
const addScripts = () => {
const json = getPackageJson();
const newJson = [
{ name: 'pre-commit', value: 'secret-squirrel' },
{ name: 'pre-push', value: 'make verify -j3' }
].reduce((returnObject, row) => addScript(returnObject, row), json);
return newJson;
}
const removePreGitHooks = () => {
const json = getPackageJson();
delete json.config['pre-git'];
delete json.commit;
delete json.devDependencies['pre-git'];
return json;
};
const find = test => {
try {
return test();
} catch (err) {
return false;
};
};
const huskyConfigNeedsUpgrade = () => {
const { scripts } = getPackageJson();
return find(() => scripts.precommit || scripts.commitmsg || scripts.prepush);
};
const secretSquirrelPreCommitScriptExists = () => {
const json = getPackageJson();
return find(() => json.husky.hooks['pre-commit'].indexOf('secret-squirrel') !== -1);
};
const preGitHookExists = () => {
const json = getPackageJson();
return find(() => !!json.config['pre-git'] || json.devDependencies['pre-git']);
};
const run = () => {
var response = '';
// Only run locally (not in CI)
if (process.env.CIRCLECI) {
return response;
}
if (huskyConfigNeedsUpgrade()) {
require(`${process.cwd()}/node_modules/.bin/husky-upgrade`);
response += 'It upgraded the Husky config format - see https://github.com/Financial-Times/n-gage/issues/220. ';
}
if (!secretSquirrelPreCommitScriptExists()) {
writePackageJsonFile(addScripts);
response += 'It added some githook scripts. ';
};
if (preGitHookExists()) {
writePackageJsonFile(removePreGitHooks);
response += 'It deleted some config > pre-git hooks. IMPORTANT: Delete the old local hooks with: "rm -rf .git/hooks/*" ';
};
if (response !== '') {
response = `✗ Note: n-gage just edited package.json. ${response} Please review and commit`;
}
return response;
}
const response = run();
console.log(response);