-
-
Notifications
You must be signed in to change notification settings - Fork 999
/
index.ts
98 lines (84 loc) 路 2.49 KB
/
index.ts
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
import execa from 'execa'
import getStdin from 'get-stdin'
import path from 'path'
import readPkg from 'read-pkg'
import debug from '../debug'
import getConf from '../getConf'
export interface Env extends NodeJS.ProcessEnv {
HUSKY_GIT_STDIN?: string
HUSKY_GIT_PARAMS?: string
}
/**
* @param {array} argv - process.argv
* @param {promise} getStdinFn - used for mocking only
*/
export default async function run(
[, scriptPath, hookName = '', HUSKY_GIT_PARAMS]: string[],
getStdinFn: () => Promise<string> = getStdin
): Promise<number> {
// Update CWD
const cwd = path.resolve(scriptPath.split('node_modules')[0])
// Debug
debug(`Changed current working directory to '${cwd}'`)
// In some cases, package.json may not exist
// For example, when switching to gh-page branch
let pkg
try {
pkg = readPkg.sync({ cwd, normalize: false })
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
const config = getConf(cwd)
const command: string | undefined =
config && config.hooks && config.hooks[hookName]
const oldCommand: string | undefined =
pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')]
// Run command
try {
const env: Env = {}
if (HUSKY_GIT_PARAMS) {
env.HUSKY_GIT_PARAMS = HUSKY_GIT_PARAMS
}
if (
['pre-push', 'pre-receive', 'post-receive', 'post-rewrite'].includes(
hookName
)
) {
// Wait for stdin
env.HUSKY_GIT_STDIN = await getStdinFn()
}
if (oldCommand) {
console.log()
console.log(
`Warning: Setting ${hookName} script in package.json > scripts will be deprecated`
)
console.log(
`Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file`
)
console.log(
`Or run ./node_modules/.bin/husky-upgrade for automatic update`
)
console.log()
console.log(`See https://github.com/typicode/husky for usage`)
console.log()
}
if (command || oldCommand) {
console.log(`husky > ${hookName} (node ${process.version})`)
execa.shellSync(command || oldCommand, { cwd, env, stdio: 'inherit' })
}
return 0
} catch (err) {
const noVerifyMessage = [
'commit-msg',
'pre-commit',
'pre-rebase',
'pre-push'
].includes(hookName)
? '(add --no-verify to bypass)'
: '(cannot be bypassed with --no-verify due to Git specs)'
console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`)
return err.code
}
}