-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.js
73 lines (56 loc) · 2.04 KB
/
install.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
const path = require('path')
const fs = require('fs')
const root = path.resolve(__dirname, '..', '..')
// const root = path.resolve(__dirname)
const exists = fs.existsSync || path.existsSync
const getGitFolderPath = currPath => {
const git = path.resolve(currPath, '.git')
if (!exists(git) || !fs.lstatSync(git).isDirectory()) {
console.log('githook-pre-commit: Not found .git folder in', git)
var newPath = path.resolve(currPath, '..')
// Stop if we on top folder
if (path === newPath) {
return null
}
return getGitFolderPath(newPath)
}
return git
}
const git = getGitFolderPath(root)
if (!git) {
console.log('githook-pre-commit: Not found any .git folder for installing githook-pre-commit hook')
return
}
const hooks = path.resolve(git, 'hooks')
const preCommit = path.resolve(hooks, 'pre-commit')
if (!exists(hooks)) fs.mkdirSync(hooks)
if (exists(preCommit) && !fs.lstatSync(preCommit).isSymbolicLink()) {
console.log('githook-pre-commit: Detected an existing git githook-pre-commit hook')
fs.writeFileSync(preCommit + '.old', fs.readFileSync(preCommit))
console.log('githook-pre-commit: Old githook-pre-commit hook backuped to githook-pre-commit.old')
}
// delete githook-pre-commit
try {
fs.unlinkSync(preCommit)
} catch (e) {}
const preCommitPath = path.resolve(__dirname, 'pre-commit').replace(/\\/g, '/')
const preCommitTpl = `#!/usr/bin/env bash
${preCommitPath}
RESULT=$?
[ $RESULT -ne 0 ] && exit 1
exit 0
`
try {
fs.writeFileSync(preCommit, preCommitTpl)
} catch (e) {
console.error('githook-pre-commit: Failed to create the hook file in your .git/hooks folder because:')
console.error('githook-pre-commit: ' + e.message)
console.error('githook-pre-commit: The hook was not installed.')
}
// change file permission
try {
fs.chmodSync(preCommit, '777')
} catch (e) {
console.error('githook-pre-commit: chmod 0777 the githook-pre-commit file in your .git/hooks folder because:')
console.error('githook-pre-commit: ' + e.message)
}