/
bin.ts
110 lines (91 loc) 路 2.8 KB
/
bin.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
99
100
101
102
103
104
105
106
107
108
109
110
import chalk from 'chalk'
import { isCI } from 'ci-info'
import path from 'path'
import pkgDir from 'pkg-dir'
import whichPMRuns from 'which-pm-runs'
import { checkGitDirEnv } from '../checkGitDirEnv'
import { debug } from '../debug'
import { install, uninstall } from './'
import { gitRevParse } from './gitRevParse'
// Skip install if HUSKY_SKIP_INSTALL is true
function checkSkipInstallEnv(): void {
if (['1', 'true'].includes(process.env.HUSKY_SKIP_INSTALL || '')) {
console.log(
'HUSKY_SKIP_INSTALL is set to true,',
'skipping Git hooks installation.'
)
process.exit(0)
}
}
function getDirs(
cwd: string
): { absoluteGitCommonDir: string; relativeUserPkgDir: string } {
const { prefix, gitCommonDir } = gitRevParse(cwd)
debug('Git rev-parse command returned:')
debug(` --git-common-dir: ${gitCommonDir}`)
debug(` --show-prefix: ${prefix}`)
const absoluteGitCommonDir = path.resolve(cwd, gitCommonDir)
// Prefix can be an empty string
const relativeUserPkgDir = prefix || '.'
return { relativeUserPkgDir, absoluteGitCommonDir }
}
// Get INIT_CWD env variable
function getInitCwdEnv(): string {
const { INIT_CWD } = process.env
if (INIT_CWD === undefined) {
const { name, version } = whichPMRuns()
throw new Error(
`INIT_CWD is not set, please upgrade your package manager (${name} ${version})`
)
}
debug(`INIT_CWD is set to ${INIT_CWD}`)
return INIT_CWD
}
function getUserPkgDir(dir: string): string {
const userPkgDir = pkgDir.sync(dir)
if (userPkgDir === undefined) {
throw new Error(
[
`Can't find package.json in ${dir} directory or parents`,
'Please check that your project has a package.json or create one and reinstall husky.'
].join('\n')
)
}
return userPkgDir
}
function run(): void {
type Action = 'install' | 'uninstall'
const action = process.argv[2] as Action
try {
console.log(
'husky > %s git hooks',
action === 'install' ? 'Setting up' : 'Uninstalling'
)
debug(`Current working directory is ${process.cwd()}`)
if (action === 'install') checkSkipInstallEnv()
const INIT_CWD = getInitCwdEnv()
const userPkgDir = getUserPkgDir(INIT_CWD)
checkGitDirEnv()
const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir)
if (action === 'install') {
const { name: pmName } = whichPMRuns()
debug(`Package manager: ${pmName}`)
install({
absoluteGitCommonDir,
relativeUserPkgDir,
userPkgDir,
pmName,
isCI
})
} else {
uninstall({ absoluteGitCommonDir, userPkgDir })
}
console.log(`husky > Done`)
} catch (err) {
console.log(chalk.red(err.message.trim()))
debug(err.stack)
console.log(chalk.red(`husky > Failed to ${action}`))
process.exit(1)
}
}
run()