Skip to content

Commit

Permalink
fix(install): better handle Git <2.9 (typicode#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode authored and nikoladavitkovski committed Sep 2, 2022
1 parent 9967008 commit 5660b41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
11 changes: 0 additions & 11 deletions scripts/pre-commit

This file was deleted.

37 changes: 17 additions & 20 deletions 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)
Expand All @@ -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 <husky@example.com>'], {
stdio: 'inherit',
env: {
...process.env,
husky_dir: dir,
},
})
console.log('husky - Git hooks installed')
}

0 comments on commit 5660b41

Please sign in to comment.