npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

git-pull-run

v1.4.0

Published

Run commands and scripts after git pull

Downloads

22,822

Readme

npm npm

Run Commands on Changes after Git Pull

Automatically run commands like npm install when fetching changes from git, but only if certain files have changed.

How It Works

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

Install

npm install --save-dev git-pull-run

This package should be executed as a post-merge git hook.

Command Line Options

> npx git-pull-run --help
Usage: git-pull-run [options]

Options:
  -V --version             output the version number
  -p, --pattern <glob>     pattern to match files (required)
  -c, --command <command>  execute shell command for each matched file
  -s, --script <script>    execute npm script for each matched file
  -m, --message <message>  print message to the console if matches were found
  -d, --debug              print additional debug information (default: false)
  -h, --help               display help for command
  • --pattern <pattern>: Required 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 the pattern. The command is going to be executed inside the directory of the changed file.
    • uses execa internally with the cwd option set as directory of the matched file.
  • --script <script>: NPM script to execute on the shell for each changed file that matches the pattern. Same as option --command "npm run <script>". The script is going to be executed inside the directory of the changed file.
  • --message <message>: Message to print on the shell if any changed files matches the pattern. The message is printed only once and not for each changed file.
  • --debug: Run in debug mode and print additional information about the changed files and commands and scripts that are being executed.

Usage

Run npm install when package-lock.json changes

post-merge git hook with Husky:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# 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"

Run npm install in a multi-package monorepo

post-merge git hook with Husky:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# 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"

Show custom message

post-merge git hook with Husky:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# 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..."

FAQ

Match package.json or package-lock.json?

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.

Run npm install or npm ci?

Discussion on: But what the hell is package-lock.json?:

npm install does not ignore package.json versions, nor does it ignore the package-lock.json. What it does is verify that the package.json and package-lock.json correspond to each other. That is, if the semver versions described in package.json fit with the locked versions in package-lock.json, npm install will use the latter completely, just like npm ci would.

Now, if you change package.json such that the versions in package-lock.json are no longer valid, your npm install will be treated as if you'd done npm install [email protected], where x.y.z is the new version in the package.json for some-package.

Error: Command failed with exit code 1: npm

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

What about Yarn?

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>".