-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostinstall.js
114 lines (102 loc) · 3.31 KB
/
postinstall.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
105
106
107
108
109
110
111
112
113
114
var path = require('path');
var fs = require('fs');
var exec = require('child_process').execSync;
var debug = require('debug')('githook-scripts:postinstall');
var VALID_HOOK_NAMES = [
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'pre-rebase',
'post-checkout',
'post-merge',
'pre-receive',
'update',
'post-receive',
'post-update',
'pre-auto-gc',
'post-rewrite',
'pre-push'
];
// we're in .../parent-module/node_modules/githook-scripts
var parentModulePath = path.resolve(process.cwd(), '../../');
var parentModulePackage;
try {
// justification in the catch statement
parentModulePackage = require(path.join(parentModulePath, 'package.json')); // eslint-disable-line global-require
} catch (err) {
// not used as a dependency, development
debug(err);
console.warn('githook-scripts: aborting hook installation, no parent package detected');
return;
}
var parentGitPath;
try {
parentGitPath = exec(
'git rev-parse --show-toplevel',
{ cwd: parentModulePath }
)
.toString()
.trim();
} catch (err) {
// git may not be installed
debug(err);
console.warn('githook-scripts: unable to run git to find the top level, skipping');
return;
}
if (!parentModulePackage.scripts || typeof parentModulePackage.scripts !== 'object') {
console.warn('githook-scripts: no "scripts" field in package.json, skipping');
return;
}
var gitHooksDirectoryPath = path.join(parentGitPath, '.git/hooks');
// `git init` can use templates that do not include a hooks directory
try {
fs.statSync(gitHooksDirectoryPath);
} catch (gitHooksDirectoryPathDoesNotExistError) {
fs.mkdirSync(gitHooksDirectoryPath);
}
if (!fs.statSync(gitHooksDirectoryPath).isDirectory()) {
console.warn('githook-scripts: the .git/hooks path exists but is not a directory, unable to install hooks');
return;
}
var githookNames = Object
.keys(parentModulePackage.scripts)
.map(function (scriptName) {
var scriptParts = /^githook:(.+)$/.exec(scriptName);
if (!scriptParts) {
debug(scriptName + ' ignored');
return false;
}
var hookName = scriptParts[1];
return hookName;
})
.filter(function (hookName) {
return !!hookName;
});
if (githookNames.length <= 0) {
console.warn('githook-scripts: no githook scripts found, add one (ex: "githook:pre-commit") to package.json and run "npm rebuild githook-scripts" to activate');
return;
}
var validGithookNames = githookNames.filter(function (hookName) {
if (VALID_HOOK_NAMES.indexOf(hookName) >= 0) {
return true;
}
console.warn('githook-scripts: "' + hookName + '" is not a valid git hook, ignoring');
return false;
});
if (validGithookNames.length <= 0) {
console.warn('githook-scripts: no valid githook scripts found, add one (ex: "githook:pre-commit") to package.json and run "npm rebuild githook-scripts" to activate');
console.warn('githook-scripts: valid githooks are ' + VALID_HOOK_NAMES.join(', '));
return;
}
validGithookNames.forEach(function (hookName) {
var hookPath = path.join(gitHooksDirectoryPath, hookName);
var scriptName = 'githook:' + hookName;
fs.writeFileSync(hookPath, '#!/bin/bash\n npm run ' + scriptName + ' "$@"');
fs.chmodSync(hookPath, '755');
debug('wrote ', hookPath);
console.log('githook-scripts: added hook ' + hookName);
});