Skip to content

Commit

Permalink
Use a dummy package.json in git hooks folder
Browse files Browse the repository at this point in the history
This is so that `node` doesn't traverse up the directory structure to
find the `package.json` that the user is working in and use its
settings.

Since we are using commonjs in the script it will crash if
`node` finds a `package.json` file with `type: module`.

nodejs/node#34049
  • Loading branch information
koddsson committed Mar 17, 2021
1 parent 25ebdaa commit 46f7e7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const hookScript = path.resolve(
path.dirname(require.main.filename),
"scripts/commit-msg"
);
const dummyPackageJSON = path.resolve(
path.dirname(require.main.filename),
"scripts/dummy-package.json"
);

const configKey = "coworking.coauthor";

Expand Down Expand Up @@ -57,6 +61,7 @@ async function main() {
execSync(`git config --unset-all ${configKey}`);
if (fs.existsSync(repoHookLocation)) {
fs.unlinkSync(repoHookLocation);
fs.unlinkSync(path.resolve(cwd, ".git/hooks/package.json"));
}
console.log("Hope you had a good time!");
process.exit(errorCodes.NO_ERROR);
Expand Down Expand Up @@ -119,6 +124,10 @@ async function main() {

if (coworkers.length === args.length) {
fs.copyFileSync(hookScript, repoHookLocation);
fs.copyFileSync(
dummyPackageJSON,
path.resolve(cwd, ".git/hooks/package.json")
);
for (const coworker of coworkers) {
spawnSync("git", ["config", "--add", configKey, coworker]);
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/dummy-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

0 comments on commit 46f7e7b

Please sign in to comment.