Automatically run commands like npm install when fetching changes from git, but only if certain files have changed.
Git invokes the post-merge hook after a git pull was done a local repository. This package will then run git diff-tree to get a list of changed files. Each changed file is being matched against the specified pattern and in case of a match, the specified command or script will be executed.
For more information, please refer to my post: Automatically Install NPM Dependencies on Git Pull
npm install --save-dev git-pull-runThis package should be executed as a post-merge git hook.
> npx git-pull-run --help
Usage: git-pull-run [options]
Options:
-V --version output the version number
-p, --pattern <glob> pattern to match files
-c, --command <command> execute shell command for each matched file
-s, --script <script> execute npm script for each matched file
-i, --install detect the right package manager and run install command
-m, --message <message> print message to the console if matches were found
-d, --debug print additional debug information (default: false)
-o, --once run command only once if any files match the pattern (default: false)
-h, --help display help for command--pattern <pattern>: Glob pattern to detect if certain files have changed on the remote repository when pulling changes. Each changed file (including path from root) is matched against this pattern.- uses micromatch internally and supports all matching features like wildcards, negation, extglobs and more.
--command <command>: Command to execute on the shell for each changed file that matches thepattern. The command is going to be executed inside the directory of the changed file.- uses execa internally with the
cwdoption set as directory of the matched file.
- uses execa internally with the
--script <script>: NPM script to execute on the shell for each changed file that matches thepattern. Same as option--command "npm run <script>". The script is going to be executed inside the directory of the changed file.--install: Install dependencies with the right package manager (npm, pnpm, yarn, bun, deno). It implicitly sets thepatternandcommandoptions to the appropriate values for the package manager.--message <message>: Message to print on the shell if any changed files matches thepattern. The message is printed only once and not for each changed file.--once: Run the command or script only once in the git root directory if any files match the pattern, instead of running it for each matched file.--debug: Run in debug mode and print additional information about the changed files and commands and scripts that are being executed.
Tip
The --install option is a shortcut to automatically detect the right package manager and run the install command. It cannot be used with the --pattern and --command options, because they are overwritten with the appropriate values for the package manager.
Install Husky or any other git hook manager to create a post-merge git hook.
# .husky/post-merge
# matches only the package-lock.json inside project directory
npx git-pull-run --pattern "package-lock.json" --command "npm install"On Windows, white spaces in the command like npm install must be escaped with backslashes, for example: npx git-pull-run --pattern "package-lock.json" --command "npm\ install"
# .husky/post-merge
# assumes monorepo structure with multiple packages in directory /packages
# matches any of these package-lock.json
npx git-pull-run --pattern "packages/*/package-lock.json" --command "npm install"# .husky/post-merge
# matches only the package-lock.json inside project directory
npx git-pull-run --pattern "package-lock.json" --message "Some packages were changed. You may run npm install to update your dependencies..."The package.json contains the semver versions of each package whereas the package-lock.json contains the exactly installed version of each package. See But what the hell is package-lock.json? for more information.
Discussion on: But what the hell is package-lock.json?:
npm installdoes not ignorepackage.jsonversions, nor does it ignore thepackage-lock.json. What it does is verify that thepackage.jsonandpackage-lock.jsoncorrespond to each other. That is, if the semver versions described inpackage.jsonfit with the locked versions inpackage-lock.json,npm installwill use the latter completely, just likenpm ciwould.Now, if you change
package.jsonsuch that the versions inpackage-lock.jsonare no longer valid, yournpm installwill be treated as if you'd donenpm install [email protected], where x.y.z is the new version in thepackage.jsonfor some-package.
On Windows, white spaces in the command like npm install must be escaped with backslashes, for example: npx git-pull-run --pattern "package-lock.json" --command "npm\ install"
Issue: Command failed with exit code 1: npm #1
The yarn.lock file should be used as pattern option (instead of package-lock.json) and the yarn install should be used as command option (instead of npm install). If you want to run scripts defined in the package.json with yarn instead of npm, please use the command option with --command "yarn run <script>" instead of --script "<script>".