-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.js
127 lines (106 loc) · 3.63 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
var fs = require('fs')
var path = require('path')
var exec = require('child_process').exec
var normalize = require('normalize-path')
module.exports = {
isHusky: function (filename) {
var data = fs.readFileSync(filename, 'utf-8')
return data.indexOf('# husky') !== -1
},
hooksDir: function (callback) {
exec('git rev-parse --git-dir', function (error, stdout, stderr) {
if (error) {
callback(stderr, null)
} else {
callback(null, stdout.trim() + '/hooks')
}
})
},
write: function (filename, data) {
fs.writeFileSync(filename, data)
fs.chmodSync(filename, 0755)
},
create: function (dir, name, cmd) {
var filename = dir + '/' + name
var arr = [
'#!/bin/sh',
'# husky'
]
// Assuming that this file is in node_modules/husky/src
var packageDir = path.join(__dirname, '..', '..', '..')
// dir being .git/hooks
var projectDir = path.join(dir, '..', '..')
// In order to support projects with package.json in a different directory
// than .git, find relative path from project directory to package.json
var relativePath = path.join('.', path.relative(projectDir, packageDir))
// On Windows normalize path (i.e. convert \ to /)
var normalizedPath = normalize(relativePath)
// Hook script
arr = arr.concat([
'cd ' + normalizedPath,
// Fix for issue #16 #24
// Test if script is defined in package.json
'[ -f package.json ] && cat package.json | grep -q \'"' + cmd + '"\\s*:\'',
// package.json or script can't be found exit
'[ $? -ne 0 ] && exit 0',
])
// On OS X and Linux, try to use nvm if it's installed
if (process.platform !== 'win32') {
// ~ is unavaible, so $HOME is used
var home = process.env.HOME
// This will load default Node version or version specified by .nvmrc
arr = arr.concat([
'export NVM_DIR="' + home + '/.nvm"'
])
if (process.platform === 'darwin') {
// If nvm was installed using homebrew,
// nvm script will be found in /usr/local/opt/nvm
arr = arr.concat([
'BREW_NVM_DIR="/usr/local/opt/nvm"',
'[ -s "$BREW_NVM_DIR/nvm.sh" ] && . "$BREW_NVM_DIR/nvm.sh"'
])
}
arr = arr.concat([
// If nvm was installed using install script,
// nvm script will be found in $NVM_DIR
'[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"',
// Test if nvm is in PATH and load version specified by .nvmrc
'command -v nvm >/dev/null 2>&1 && [ -f .nvmrc ] && nvm use'
])
}
// Can't find npm message
var npmNotFound = 'husky - can\'t find npm in PATH. Skipping ' + cmd + ' script in package.json'
arr = arr.concat([
// Test if npm is in PATH
'command -v npm >/dev/null 2>&1 || { echo >&2 "' + npmNotFound + '"; exit 0; }',
// Run script
'npm run ' + cmd,
'if [ $? -ne 0 ]; then',
' echo',
' echo "husky - ' + name + ' hook failed (add --no-verify to bypass)"',
' echo',
' exit 1',
'fi',
''
])
// Create hooks directory if needed
if (!fs.existsSync(dir)) fs.mkdirSync(dir)
// Create hook file
var data = arr.join('\n')
if (!fs.existsSync(filename)) {
this.write(filename, data)
} else {
if (this.isHusky(filename)) {
this.write(filename, data)
} else {
console.log('skipping .git/hooks/' + name + ' (existing user hook)')
}
}
},
remove: function (dir, name) {
var filename = dir + '/' + name
if (fs.existsSync(filename) && this.isHusky(filename)) {
fs.unlinkSync(dir + '/' + name)
}
}
}