install-lefthook.mjs 1.0 KB

123456789101112131415161718192021
  1. #!/usr/bin/env node
  2. import { existsSync } from 'node:fs'
  3. import { spawnSync } from 'node:child_process'
  4. import { join } from 'node:path'
  5. const git = spawnSync('git', ['rev-parse', '--git-dir'], { stdio: 'ignore' })
  6. if (git.status !== 0) process.exit(0)
  7. const isWindows = process.platform === 'win32'
  8. const lefthook = join(process.cwd(), 'node_modules', '.bin', isWindows ? 'lefthook.cmd' : 'lefthook')
  9. if (!existsSync(lefthook)) process.exit(0)
  10. // On Windows the bin shim is a `.cmd` file, and recent Node (CVE-2024-27980)
  11. // refuses to launch `.cmd`/`.bat` via spawn without `shell: true` — it returns
  12. // `EINVAL` with a null status, which would otherwise fail postinstall. Quote
  13. // the path because a shell re-parses the command line and the path may contain
  14. // spaces. POSIX needs no shell: the extensionless shim is directly executable.
  15. const result = isWindows
  16. ? spawnSync(`"${lefthook}"`, ['install', '--force'], { stdio: 'inherit', shell: true })
  17. : spawnSync(lefthook, ['install', '--force'], { stdio: 'inherit' })
  18. process.exit(result.status ?? 1)