diff --git a/scripts/pre-commit b/scripts/pre-commit deleted file mode 100755 index bbf9415be..000000000 --- a/scripts/pre-commit +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -set -eu -echo "husky: installing Git hooks" - -mkdir -p "$husky_dir/_" -echo "_" > "$husky_dir/.gitignore" -cp "$(dirname "$0")/husky.sh" "$husky_dir/_" -git config core.hooksPath "$husky_dir" - -echo "husky: ok" -exit 1 # abort git commit diff --git a/src/commands/install.ts b/src/commands/install.ts index 9627de360..8bf06ec5f 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -1,15 +1,7 @@ -import os from 'os' import fs from 'fs' import path from 'path' import cp from 'child_process' -function copyScript(scriptName: string, destDir: string) { - fs.copyFileSync( - path.join(__dirname, '../../scripts', scriptName), - path.join(destDir, scriptName), - ) -} - export function install(dir = '.husky'): void { // Ensure that we're not trying to install outside cwd const absoluteHooksDir = path.resolve(process.cwd(), dir) @@ -22,19 +14,24 @@ export function install(dir = '.husky'): void { throw new Error(".git can't be found") } - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'husky-')) + try { + // Create .husky/_ + fs.mkdirSync(path.join(dir, '_'), { recursive: true }) - copyScript('husky.sh', tmpDir) - copyScript('pre-commit', tmpDir) + // Create .husky/.gitignore + fs.writeFileSync(path.join(dir, '.gitignore'), '_', 'utf-8') - fs.chmodSync(path.join(tmpDir, 'pre-commit'), 0o0755) + // Copy husky.sh to .husky/_/husky.sh + fs.copyFileSync( + path.join(__dirname, '../../scripts/husky.sh'), + path.join(dir, '_/husky.sh'), + ) + + cp.spawnSync('git', ['config', 'core.hooksPath', dir]) + } catch (e) { + console.log('husky - Git hooks failed to install') + throw e + } - cp.spawnSync('git', ['config', 'core.hooksPath', tmpDir]) - cp.spawnSync('git', ['commit', '--author', 'husky '], { - stdio: 'inherit', - env: { - ...process.env, - husky_dir: dir, - }, - }) + console.log('husky - Git hooks installed') }