bin.mjs 667 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env node
  2. import f, { writeFileSync as w } from 'fs'
  3. import i from './index.mjs'
  4. let p, a, n, s, o, d
  5. p = process
  6. a = p.argv[2]
  7. if (a == 'init') {
  8. n = 'package.json'
  9. s = f.readFileSync(n)
  10. o = JSON.parse(s)
  11. ;(o.scripts ||= {}).prepare = 'husky'
  12. w(n, JSON.stringify(o, 0, /\t/.test(s) ? '\t' : 2) + '\n')
  13. p.stdout.write(i())
  14. try { f.mkdirSync('.husky') } catch {}
  15. w('.husky/pre-commit', p.env.npm_config_user_agent.split('/')[0] + ' test\n')
  16. p.exit()
  17. }
  18. d = c => console.error(`${c} command is deprecated`)
  19. if (['add', 'set', 'uninstall'].includes(a)) { d(a); p.exit(1) }
  20. if (a == 'install') d(a)
  21. p.stdout.write(i(a == 'install' ? undefined : a))