Skip to content

Commit

Permalink
feat(husky): add post pull hook to suggest running 'npm install' (mdn…
Browse files Browse the repository at this point in the history
…#22308)

* feat(husky): add post pull hook to suggest running 'npm install'

* Rename to save-git-history

* update package.json
  • Loading branch information
OnkarRuikar authored Mar 3, 2024
1 parent 7d64b1d commit 2250802
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if [ "$BRANCH" != "main" ]; then
exit 0
fi

if [ -f ".husky/_/history" ]; then
lastHash=$(cat ./.husky/_/history)
isUpdated=$(git diff $lastHash HEAD -- ./package.json)
if [ "$isUpdated" != "" ]; then
echo "\n⚠🔥 'package.json' has changed. Please run 'npm install'! 🔥"
fi
else
npm install
fi
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"yargs": "~17.7.0"
},
"scripts": {
"postinstall": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/save-git-history.ts",
"prepare": "(husky || true) && npm run gentypes && npm run build",
"diff": "node --loader=ts-node/esm --no-warnings=ExperimentalWarning scripts/diff.ts",
"unittest": "NODE_ENV=test c8 mocha index.test.ts --recursive \"{,!(node_modules)/**}/*.test.ts\"",
Expand Down
24 changes: 23 additions & 1 deletion scripts/lib/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,26 @@ const getFileContent = (commit: string, path: string): string =>
})
.trim();

export { getMergeBase, getGitDiffStatuses, getFileContent };
/**
* Get the current branch name
* @returns The output from the `git rev-parse --abbrev-ref HEAD` command
*/
const getBranchName = (): string =>
child_process
.execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' })
.trim();

/**
* Get commit hash of HEAD
* @returns The output from the `git rev-parse HEAD` command
*/
const getHashOfHEAD = (): string =>
child_process.execSync('git rev-parse HEAD', { encoding: 'utf-8' }).trim();

export {
getMergeBase,
getGitDiffStatuses,
getFileContent,
getBranchName,
getHashOfHEAD,
};
15 changes: 15 additions & 0 deletions scripts/save-git-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* This script stores the 'main' branch's HEAD commit hash in .husky/_/history
* The stored commit hash is used by the post-merge script .husky/post-merge
*/

import fs from 'node:fs';

import { getBranchName, getHashOfHEAD } from './lib/git.js';

const HUSKY_ROOT = '.husky/_/';
const HISTORY_FILE = HUSKY_ROOT + 'history';

if ('main' === getBranchName() && fs.existsSync(HUSKY_ROOT)) {
fs.writeFileSync(HISTORY_FILE, getHashOfHEAD());
}

0 comments on commit 2250802

Please sign in to comment.