forked from vercel/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.js
79 lines (62 loc) · 1.91 KB
/
uninstall.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
const fs = require('fs');
const path = require('path');
const detectGitDir = require('./git-dir');
// Hard to catch this output if there's no new-line after the script runs.
process.on('exit', () => console.log());
function env(name) {
if (!process.env[name]) {
throw new Error(`Required environment variable is empty or not defined - are you using an NPM-compatible package manager?: ${name}`);
}
return process.env[name];
}
const nodeBin = env('npm_node_execpath');
const packageManagerBin = env('npm_execpath');
const gitDir = detectGitDir();
if (!gitDir || !fs.existsSync(gitDir) || !fs.statSync(gitDir).isDirectory()) {
console.error('△ @zeit/git-hooks: .git/hooks directory not found or is not a directory; ignoring Git hook uninstallation:', gitDir || 'reached filesystem boundary (root or drive)');
process.exit(0);
}
const hooksDir = path.join(gitDir, 'hooks');
// Uninstall each of the hooks
function uninstallHook(name) {
const hookPath = path.join(hooksDir, name);
if (!fs.existsSync(hookPath)) {
return;
}
const isOneOfOurs = fs.lstatSync(hookPath).isSymbolicLink() && fs.readlinkSync(hookPath) === './_do_hook';
if (!isOneOfOurs) {
console.error(`△ @zeit/git-hooks: hook '${name}' appears to be a user hook; skipping: ${hookPath}`);
return;
}
fs.unlinkSync(hookPath);
}
[
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'pre-rebase',
'post-checkout',
'post-merge',
'pre-push',
'pre-receive',
'update',
'post-receive',
'post-update',
'push-to-checkout',
'pre-auto-gc',
'post-rewrite',
'rebase',
'sendemail-validate'
].forEach(uninstallHook);
function removeIfExists(path) {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
}
removeIfExists(path.join(hooksDir, '_do_hook'));
removeIfExists(path.join(hooksDir, '_detect_package_hooks'));
console.error('△ @zeit/git-hooks: hooks uninstalled successfully');