Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
Loading