Closed binsee closed 2 years ago
In this use case, process.cwd()
would be more reliable, because we need to locate the codebase directory where the hook script is to take effect, not the directory of the hook scripts.
Example:
Test with the following code in pre-push
:
const pkgFile = path.join(process.cwd(), 'package.json')
const packageVersion = require(pkgFile).version
console.info('__filename', __filename)
console.info('__dirname', __dirname)
console.info('process.cwd()', process.cwd())
console.info('packageVersion', packageVersion)
process.exit(1)
@chatie/git-scripts
@chatie/git-scripts
project
binsee@localhost > ~/CodeWork/chatie-git-scripts > binsee/issue22 ± > pwd
/Users/binsee/CodeWork/chatie-git-scripts
binsee@localhost > ~/CodeWork/chatie-git-scripts > binsee/issue22 ± > git push
__filename /Users/binsee/CodeWork/chatie-git-scripts/bin/pre-push.ts
__dirname /Users/binsee/CodeWork/chatie-git-scripts/bin
process.cwd() /Users/binsee/CodeWork/chatie-git-scripts
packageVersion 0.7.3
Failed to exec pre-push hook script
error: failed to push some refs to 'github.com:binsee/chatie-git-scripts.git'
we need:
path.join(__dirname, '../package.json')
path.join(process.cwd(), 'package.json')
puppet-xp
puppet-xp
project, depends on pre-push
hook provided by @chatie/git-scripts
✘ binsee@localhost > ~/CodeWork/puppet-xp > test ± > pwd
/Users/binsee/CodeWork/puppet-xp
binsee@localhost > ~/CodeWork/puppet-xp > test ± > git push
__filename /Users/binsee/CodeWork/puppet-xp/node_modules/@chatie/git-scripts/dist/bin/pre-push.js
__dirname /Users/binsee/CodeWork/puppet-xp/node_modules/@chatie/git-scripts/dist/bin
process.cwd() /Users/binsee/CodeWork/puppet-xp
packageVersion 1.10.26
Failed to exec pre-push hook script
error: failed to push some refs to 'github.com:binsee/puppet-xp.git'
we need:
path.join(__dirname, '../../../../../package.json')
path.join(process.cwd(), 'package.json')
When git executes the hook, git will change the working directory to the root directory of the code base, so it is more reliable to use process.cwd()
.
Fixes #22