-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathindex.ts
89 lines (73 loc) · 2.01 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
import fs = require('fs')
import cp = require('child_process')
import path = require('path')
function l(msg: string): void {
console.log(`husky - ${msg}`)
}
export function install(dir = '.husky'): void {
// Ensure that we're inside a git repository
if (cp.spawnSync('git', ['rev-parse']).status !== 0) {
l('not a Git repository, skipping hooks installation')
return
}
// Custom dir help
const url = 'https://typicode.github.io/husky/#/?id=custom-directory'
// Ensure that we're not trying to install outside of cwd
if (!path.resolve(process.cwd(), dir).startsWith(process.cwd())) {
throw new Error(`.. not allowed (see ${url})`)
}
// Ensure that cwd is git top level
if (!fs.existsSync('.git')) {
throw new Error(`.git can't be found (see ${url})`)
}
try {
// Create .husky/_
fs.mkdirSync(path.join(dir, '_'), { recursive: true })
// Create .husky/.gitignore
fs.writeFileSync(path.join(dir, '.gitignore'), '_\n')
// Copy husky.sh to .husky/_/husky.sh
fs.copyFileSync(
path.join(__dirname, 'husky.sh'),
path.join(dir, '_/husky.sh'),
)
// Configure repo
const { error } = cp.spawnSync('git', ['config', 'core.hooksPath', dir])
if (error) {
throw error
}
} catch (e) {
l('Git hooks failed to install')
throw e
}
l('Git hooks installed')
}
export function set(file: string, cmd: string): void {
const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
throw new Error(
`can't create hook, ${dir} directory doesn't exist (try running husky install)`,
)
}
fs.writeFileSync(
file,
`#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
${cmd}
`,
{ mode: 0o0755 },
)
l(`created ${file}`)
}
export function add(file: string, cmd: string): void {
if (fs.existsSync(file)) {
fs.appendFileSync(file, `${cmd}\n`)
l(`updated ${file}`)
} else {
set(file, cmd)
}
}
export function uninstall(): void {
cp.spawnSync('git', ['config', '--unset', 'core.hooksPath'], {
stdio: 'inherit',
})
}