forked from typicode/husky
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(install): better handle Git <2.9 (typicode#846)
- Loading branch information
1 parent
9967008
commit 5660b41
Showing
2 changed files
with
17 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]>'], { | ||
stdio: 'inherit', | ||
env: { | ||
...process.env, | ||
husky_dir: dir, | ||
}, | ||
}) | ||
console.log('husky - Git hooks installed') | ||
} |