/
add.ts
33 lines (27 loc) 路 816 Bytes
/
add.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
import fs from 'fs'
import path from 'path'
function makeHookExecutable(file: string): void {
fs.chmodSync(file, 0o0755)
}
function createHookFile(file: string, cmd: string) {
const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
throw new Error(`can't create hook, ${dir} directory doesn't exist`)
}
if (fs.existsSync(file)) {
throw new Error(`${file} already exists`)
}
const data = [
'#!/bin/sh',
'# shellcheck source=./_/husky.sh',
'. "$(dirname "$(readlink -f "$0")")/_/husky.sh"',
cmd,
].join('\n')
fs.writeFileSync(file, data, 'utf-8')
// Show "./file" instead of just "file"
console.log(`created ${dir}${path.sep}${path.basename(file)}`)
}
export function add(file: string, cmd: string): void {
createHookFile(file, cmd)
makeHookExecutable(file)
}