Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(install): better handle Git <2.9 #846

Merged
merged 2 commits into from Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
}