From 34b85fda3591d1de07fd24981e43cc32b465e86f Mon Sep 17 00:00:00 2001 From: typicode Date: Wed, 27 Jan 2021 09:10:01 +0100 Subject: [PATCH 1/2] fix(install): better support Git <2.9 --- scripts/pre-commit | 11 ----------- src/commands/install.ts | 35 +++++++++++++---------------------- 2 files changed, 13 insertions(+), 33 deletions(-) delete mode 100755 scripts/pre-commit 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..93f24aedb 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,18 @@ export function install(dir = '.husky'): void { throw new Error(".git can't be found") } - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'husky-')) - - copyScript('husky.sh', tmpDir) - copyScript('pre-commit', tmpDir) - - fs.chmodSync(path.join(tmpDir, 'pre-commit'), 0o0755) + try { + fs.mkdirSync(path.join(dir, '_'), { recursive: true }) + fs.writeFileSync(path.join(dir, '.gitignore'), '_', 'utf-8') + 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') } From 4dac49e0d5cdc8bfa6a30bba19e70759f634eb30 Mon Sep 17 00:00:00 2001 From: typicode Date: Thu, 28 Jan 2021 01:30:57 +0100 Subject: [PATCH 2/2] style: add comments --- src/commands/install.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/commands/install.ts b/src/commands/install.ts index 93f24aedb..8bf06ec5f 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -15,12 +15,18 @@ export function install(dir = '.husky'): void { } try { + // Create .husky/_ fs.mkdirSync(path.join(dir, '_'), { recursive: true }) + + // Create .husky/.gitignore fs.writeFileSync(path.join(dir, '.gitignore'), '_', 'utf-8') + + // 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')