/
install.ts
48 lines (40 loc) 路 1.34 KB
/
install.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
import fs from 'fs'
import path from 'path'
import cp from 'child_process'
export function install(dir = '.husky'): void {
// Ensure that we're inside a git repository
if (cp.spawnSync('git', ['rev-parse']).status !== 0) {
// Do not fail to let projects downloaded as zip files have their dependencies installed
console.log('husky - not a Git repository, skipping hooks installation')
return
}
// Ensure that we're not trying to install outside cwd
const absoluteHooksDir = path.resolve(process.cwd(), dir)
if (!absoluteHooksDir.startsWith(process.cwd())) {
throw new Error('.. not allowed')
}
// Ensure that cwd is git top level
if (!fs.existsSync('.git')) {
throw new Error(".git can't be found")
}
try {
// Create .husky/_
fs.mkdirSync(path.join(dir, '_'), { recursive: true })
// Create .husky/.gitignore
fs.writeFileSync(path.join(dir, '.gitignore'), '_\n', 'utf-8')
// Copy husky.sh to .husky/_/husky.sh
fs.copyFileSync(
path.join(__dirname, '../../scripts/husky.sh'),
path.join(dir, '_/husky.sh'),
)
// Configure repo
const { error } = cp.spawnSync('git', ['config', 'core.hooksPath', dir])
if (error) {
throw error
}
} catch (e) {
console.log('husky - Git hooks failed to install')
throw e
}
console.log('husky - Git hooks installed')
}