Skip to content

Commit

Permalink
fix(install): use relative hook path instead of absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Sep 21, 2020
1 parent a3db0d3 commit cde7ace
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (arg === 'add') {
} else if (arg === 'install') {
install({
cwd: process.cwd(),
pathToPackageDir: params[0],
dir: params[0],
})
} else if (['--version', '-v'].includes(arg)) {
version()
Expand Down
9 changes: 4 additions & 5 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ function copyScript(scriptName: string, destDir: string) {

export function install({
cwd,
pathToPackageDir = '.',
dir = '.',
}: {
cwd: string
pathToPackageDir: string
dir: string
}): void {
const absoluteHooksDir = path.resolve(cwd, pathToPackageDir)

// Ensure that we're not trying to install outside cwd
const absoluteHooksDir = path.resolve(cwd, dir)
if (!absoluteHooksDir.startsWith(cwd)) {
throw new Error('.. not allowed')
}
Expand All @@ -46,7 +45,7 @@ export function install({
stdio: 'inherit',
env: {
...process.env,
husky_dir: path.join(absoluteHooksDir, '.husky'),
husky_dir: path.join(dir, '.husky'),
},
})
}
8 changes: 8 additions & 0 deletions test/_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ function init_git {
git config user.email "test@test"
git config user.name "test"
}

function test_hooksPath {
readonly hooksPath=`git config core.hooksPath`
if [ "$hooksPath" != "$1" ]; then
echo -e "\e[0;31mERROR:\e[m core.hooksPath should be $1, was $hooksPath"
exit 1
fi
}
3 changes: 3 additions & 0 deletions test/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ npx --no-install husky add pre-commit "echo \"msg from pre-commit hook\" && exit
# Debug
# cat .husky/*

# Test core.hooksPath
test_hooksPath ".husky"

# Test pre-commit
git add package.json
git commit -m "should fail" || echo -e "\e[0;32mSUCCESS\e[m" && exit 0
3 changes: 3 additions & 0 deletions test/subdir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ npx --no-install husky add pre-commit "echo \"msg from pre-commit hook\" && exit
# Debug
# cat .husky/*

# Test core.hooksPath
test_hooksPath "sub/.husky"

# Test pre-commit
git add package.json
git commit -m "should fail" || echo -e "\e[0;32mSUCCESS\e[m" && exit 0

0 comments on commit cde7ace

Please sign in to comment.