-
-
Notifications
You must be signed in to change notification settings - Fork 999
/
index.ts
79 lines (70 loc) 路 2.06 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
import fs from 'fs'
import path from 'path'
import { debug } from '../debug'
import { getConf } from '../getConf'
import { createHooks, removeHooks } from './hooks'
import { createLocalScript, removeLocalScript } from './localScript'
import { createMainScript, removeMainScript } from './mainScript'
// This prevents the case where someone would want to debug a node_module that has
// husky as devDependency and run npm install from node_modules directory
function isInNodeModules(dir: string): boolean {
return dir.indexOf('node_modules') !== -1
}
function getGitHooksDir(gitDir: string): string {
return path.join(gitDir, 'hooks')
}
export function install({
absoluteGitCommonDir,
relativeUserPkgDir,
userPkgDir,
pmName, // package manager name
isCI, // running in CI or not
}: {
absoluteGitCommonDir: string
relativeUserPkgDir: string
userPkgDir: string
pmName: string
isCI: boolean
}): void {
// Get conf from package.json or .huskyrc
const conf = getConf(userPkgDir)
// Checks
if (isCI && conf.skipCI) {
console.log('CI detected, skipping Git hooks installation.')
return
}
if (isInNodeModules(userPkgDir)) {
console.log(
'Trying to install from node_modules directory, skipping Git hooks installation.'
)
return
}
// Create hooks directory if it doesn't exist
const gitHooksDir = getGitHooksDir(absoluteGitCommonDir)
if (!fs.existsSync(gitHooksDir)) {
fs.mkdirSync(gitHooksDir)
}
debug(`Installing hooks in ${gitHooksDir}`)
createHooks(gitHooksDir)
createLocalScript(gitHooksDir, pmName, relativeUserPkgDir)
createMainScript(gitHooksDir)
}
export function uninstall({
absoluteGitCommonDir,
userPkgDir,
}: {
absoluteGitCommonDir: string
userPkgDir: string
}): void {
if (isInNodeModules(userPkgDir)) {
console.log(
'Trying to uninstall from node_modules directory, skipping Git hooks uninstallation.'
)
return
}
// Remove hooks
const gitHooksDir = getGitHooksDir(absoluteGitCommonDir)
removeHooks(gitHooksDir)
removeLocalScript(gitHooksDir)
removeMainScript(gitHooksDir)
}