How To Npm Install Dependencies From Git Download UPDATED

How To Npm Install Dependencies From Git Download

Cover image for Automatically Install NPM Dependencies on Git Pull

Chris

Chris

Posted on • Updated on

Automatically Install NPM Dependencies on Git Pull

UPDATE: I released a npm package for this script: git-pull-run. Please report whatsoever issues or suggestions to improve on GitHub.

Together with my team I've been working on ane project repository with multiple packages - a monorepo. Of course, we're using Git and branches, so there is nearly no friction betwixt u.s.. Except when information technology comes to dependencies - in our case npm dependencies - but I guess it also holds true for other environments. When I pull the latest changes on my current branch or I switch between dissimilar branches, I have to be aware if the package-lock.json (lock file) has been inverse. If then, I accept to run npm install to make certain my dependencies are upwardly to appointment with the latest changes. Otherwise, I might run across hard-to-detect bugs where the electric current evolution works on some automobile but non on the other due to outdated dependencies.

Hooks To The Rescue

We're already using a pre-commit hook to automatically run linting and formatting on git commit. That's quite piece of cake with Git Hooks and a tool like Husky. Fortunately, Git too supports a mail-merge hook that runs after a git pull is done on a local repository. This is exactly the point in time where nosotros need to update our dependencies to see if they take changed. For detailed steps on how to go started with hooks, I recommend following this guide.

Detect Changes

When we git pull the latest changes, nosotros need a listing of all changed files. If this list contains a parcel-lock.json, we demand to run npm install to update our dependencies. If we working on a monorepo with multiple packages as in my case, we need to run it for each inverse package. The following git diff prints the list of changed files:

            git diff              --name-simply              HEAD@{1}              HEAD                      

Enter fullscreen mode Leave fullscreen fashion

With a uncomplicated regular expression, we can filter all paths containing a bundle-lock.json file. I did put the regex into the PACKAGE_LOCK_REGEX variable, because this part must be inverse depending on the bodily project structure. It contains a matching grouping (i.east. the get-go pair of parentheses) that starts with packages/, because in our monorepo all packages alive under this directory (except for evolution dependencies which live at project root directory). The event of regex filter is saved as assortment into the PACKAGES variable.

                          IFS              =              $'              \n              '              PACKAGE_LOCK_REGEX              =              "(^packages              \/              .*              \/              package-lock              \.              json)|(^bundle-lock              \.              json)"              PACKAGES              =(              "              $(git unequal              --name-merely              Caput@{i}              HEAD |              grep              -E              "              $PACKAGE_LOCK_REGEX              "              )              "              )                      

Enter fullscreen mode Exit fullscreen manner

Run Install

Finally, we need to run npm install for each changed bundle. As Git runs on the project root directory and the inverse files are actually paths to lock files, we must change the directory before running install. With $(dirname package) we can easily extract the directories from the path.

                          if              [[              ${              PACKAGES              [@]              }              ]]              ;              then   for              package              in              $PACKAGES              ;              practise                            echo              "📦                            $package                              was inverse. Running npm install to update your dependencies..."              DIR              =              $(              dirname              package)              cd              "              $DIR              "              &&              npm              install                            done fi                      

Enter fullscreen fashion Exit fullscreen mode

Post Merge Hook

All the in a higher place snippets tin can be combined into the following beat script, that is going to exist executed by Husky as mail service-merge hook.

The file must be saved as mail service-merge (no .sh extension) inside the .husky folder. I'm running on macOS with zsh equally default shell (see shebang #!/bin/zsh) and it's working. However, I didn't examination it with fustigate, and so in that location might be some changes necessary if you lot run a unlike shell.

Test Information technology

In order to verify if the hook is working, we can reset the current local branch to a previous country (e.thousand. rewind 20 commits) and and then pull the changes back.

            git reset              --difficult              Caput~20              &&              git pull                      

Enter fullscreen mode Exit fullscreen mode

If the packet-lock.json has been changed in one of the commits, the hook will impress a nice little bulletin for each lock file and it will automatically run npm install for us. If you use Git-integration of Editors similar VSCode, you demand to check the output of the Git log in club see what's going on.

DOWNLOAD HERE

Posted by: cherylparsomen.blogspot.com

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel