Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run script during npm version

Tags:

javascript

npm

Is there a way to run a script during npm version, that is running after the release number was increased but before creating and pushing the git tag?

like image 582
Andreas Köberle Avatar asked Oct 20 '22 08:10

Andreas Köberle


1 Answers

You can create a version script that will be called once the package version is increased but before the commit and tag.

"scripts": {
  "version": "./your_script"
}

Check the order of execution according to the npm version documentation. Below you can see the interesting excerpt, in particular point 4:

  1. Check to make sure the git working directory is clean before we get started. Your scripts may add files to the commit in future steps. This step is skipped if the --force flag is set.
  2. Run the preversion script. These scripts have access to the old version in package.json. A typical use would be running your full test suite before deploying. Any files you want added to the commit should be explicitly added using git add.
  3. Bump version in package.json as requested (patch, minor, major, etc).
  4. Run the version script. These scripts have access to the new version in package.json (so they can incorporate it into file headers in generated files for example). Again, scripts should explicitly add generated files to the commit using git add.
  5. Commit and tag.
  6. Run the postversion script. Use it to clean up the file system or automatically push the commit and/or tag.

This functionality was introduced in npm v2.13.0. See version: allow scripts to add files to the commit for more info.

like image 117
dreyescat Avatar answered Nov 01 '22 13:11

dreyescat