UNPKG

956 BJavaScriptView Raw
1import c from 'child_process'
2import f, { writeFileSync as w } from 'fs'
3import p from 'path'
4
5let l = [ 'pre-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'applypatch-msg', 'pre-applypatch', 'post-applypatch', 'pre-rebase', 'post-rewrite', 'post-checkout', 'post-merge', 'pre-push', 'pre-auto-gc' ]
6
7export default (d = '.husky') => {
8 if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
9 if (d.includes('..')) return '.. not allowed'
10 if (!f.existsSync('.git')) return `.git can't be found`
11
12 let _ = (x = '') => p.join(d, '_', x)
13 let { status: s, stderr: e } = c.spawnSync('git', ['config', 'core.hooksPath', `${d}/_`])
14 if (s == null) return 'git command not found'
15 if (s) return '' + e
16
17 f.mkdirSync(_(), { recursive: true })
18 w(_('.gitignore'), '*')
19 f.copyFileSync(new URL('husky.sh', import.meta.url), _('h'))
20 l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\${0%/*}/h"`, { mode: 0o755 }))
21 w(_('husky.sh'), '')
22 return ''
23}
\No newline at end of file