index.mjs 953 B

1234567891011121314151617181920212223
  1. import c from 'child_process'
  2. import f, { writeFileSync as w } from 'fs'
  3. import p from 'path'
  4. let 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' ]
  5. export default (d = '.husky') => {
  6. if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
  7. if (d.includes('..')) return '.. not allowed'
  8. if (!f.existsSync('.git')) return `.git can't be found`
  9. let _ = (x = '') => p.join(d, '_', x)
  10. let { status: s, stderr: e } = c.spawnSync('git', ['config', 'core.hooksPath', `${d}/_`])
  11. if (s == null) return 'git command not found'
  12. if (s) return '' + e
  13. f.mkdirSync(_(), { recursive: true })
  14. w(_('.gitignore'), '*')
  15. f.copyFileSync(new URL('husky', import.meta.url), _('h'))
  16. l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\${0%/*}/h"`, { mode: 0o755 }))
  17. w(_('husky.sh'), '')
  18. return ''
  19. }