Chatie / git-scripts

Git Hooks Integration for Chatie Projects
https://npmjs.com/package/@chatie/git-scripts
Apache License 2.0
1 stars 4 forks source link

fix: :bug: Cannot find module '../package.json' #23

Closed binsee closed 2 years ago

binsee commented 2 years ago

Fixes #22

binsee commented 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 code

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)

test in @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:

test in 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:


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().