11cb0ef41Sopenharmony_ci# libnpmversion 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci[](https://npm.im/libnpmversion) 41cb0ef41Sopenharmony_ci[](https://npm.im/libnpmversion) 51cb0ef41Sopenharmony_ci[](https://github.com/npm/cli/actions/workflows/ci-libnpmversion.yml) 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciLibrary to do the things that 'npm version' does. 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci## USAGE 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci```js 121cb0ef41Sopenharmony_ciconst npmVersion = require('libnpmversion') 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// argument can be one of: 151cb0ef41Sopenharmony_ci// - any semver version string (set to that exact version) 161cb0ef41Sopenharmony_ci// - 'major', 'minor', 'patch', 'pre{major,minor,patch}' (increment at 171cb0ef41Sopenharmony_ci// that value) 181cb0ef41Sopenharmony_ci// - 'from-git' (set to the latest semver-lookin git tag - this skips 191cb0ef41Sopenharmony_ci// gitTagVersion, but will still sign if asked) 201cb0ef41Sopenharmony_cinpmVersion(arg, { 211cb0ef41Sopenharmony_ci path: '/path/to/my/pkg', // defaults to cwd 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci allowSameVersion: false, // allow tagging/etc to the current version 241cb0ef41Sopenharmony_ci preid: '', // when arg=='pre', define the prerelease string, like 'beta' etc. 251cb0ef41Sopenharmony_ci tagVersionPrefix: 'v', // tag as 'v1.2.3' when versioning to 1.2.3 261cb0ef41Sopenharmony_ci commitHooks: true, // default true, run git commit hooks, default true 271cb0ef41Sopenharmony_ci gitTagVersion: true, // default true, tag the version 281cb0ef41Sopenharmony_ci signGitCommit: false, // default false, gpg sign the git commit 291cb0ef41Sopenharmony_ci signGitTag: false, // default false, gpg sign the git tag 301cb0ef41Sopenharmony_ci force: false, // push forward recklessly if any problems happen 311cb0ef41Sopenharmony_ci ignoreScripts: false, // do not run pre/post/version lifecycle scripts 321cb0ef41Sopenharmony_ci scriptShell: '/bin/bash', // shell to run lifecycle scripts in 331cb0ef41Sopenharmony_ci message: 'v%s', // message for tag and commit, replace %s with the version 341cb0ef41Sopenharmony_ci silent: false, // passed to @npmcli/run-script to control whether it logs 351cb0ef41Sopenharmony_ci}).then(newVersion => { 361cb0ef41Sopenharmony_ci console.error('version updated!', newVersion) 371cb0ef41Sopenharmony_ci}) 381cb0ef41Sopenharmony_ci``` 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci## Description 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciRun this in a package directory to bump the version and write the new data 431cb0ef41Sopenharmony_ciback to `package.json`, `package-lock.json`, and, if present, 441cb0ef41Sopenharmony_ci`npm-shrinkwrap.json`. 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciThe `newversion` argument should be a valid semver string, a valid second 471cb0ef41Sopenharmony_ciargument to [semver.inc](https://github.com/npm/node-semver#functions) (one 481cb0ef41Sopenharmony_ciof `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, 491cb0ef41Sopenharmony_ci`prerelease`), or `from-git`. In the second case, the existing version will 501cb0ef41Sopenharmony_cibe incremented by 1 in the specified field. `from-git` will try to read 511cb0ef41Sopenharmony_cithe latest git tag, and use that as the new npm version. 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciIf run in a git repo, it will also create a version commit and tag. This 541cb0ef41Sopenharmony_cibehavior is controlled by `gitTagVersion` (see below), and can be 551cb0ef41Sopenharmony_cidisabled by setting `gitTagVersion: false` in the options. 561cb0ef41Sopenharmony_ciIt will fail if the working directory is not clean, unless `force: true` is 571cb0ef41Sopenharmony_ciset. 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciIf supplied with a `message` string option, it will 601cb0ef41Sopenharmony_ciuse it as a commit message when creating a version commit. If the 611cb0ef41Sopenharmony_ci`message` option contains `%s` then that will be replaced with the 621cb0ef41Sopenharmony_ciresulting version number. 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ciIf the `signGitTag` option is set, then the tag will be signed using 651cb0ef41Sopenharmony_cithe `-s` flag to git. Note that you must have a default GPG key set up in 661cb0ef41Sopenharmony_ciyour git config for this to work properly. 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciIf `preversion`, `version`, or `postversion` are in the `scripts` property 691cb0ef41Sopenharmony_ciof the package.json, they will be executed in the appropriate sequence. 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciThe exact order of execution is as follows: 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci1. Check to make sure the git working directory is clean before we get 741cb0ef41Sopenharmony_ci started. Your scripts may add files to the commit in future steps. 751cb0ef41Sopenharmony_ci This step is skipped if the `force` flag is set. 761cb0ef41Sopenharmony_ci2. Run the `preversion` script. These scripts have access to the old 771cb0ef41Sopenharmony_ci `version` in package.json. A typical use would be running your full 781cb0ef41Sopenharmony_ci test suite before deploying. Any files you want added to the commit 791cb0ef41Sopenharmony_ci should be explicitly added using `git add`. 801cb0ef41Sopenharmony_ci3. Bump `version` in `package.json` as requested (`patch`, `minor`, 811cb0ef41Sopenharmony_ci `major`, explicit version number, etc). 821cb0ef41Sopenharmony_ci4. Run the `version` script. These scripts have access to the new `version` 831cb0ef41Sopenharmony_ci in package.json (so they can incorporate it into file headers in 841cb0ef41Sopenharmony_ci generated files for example). Again, scripts should explicitly add 851cb0ef41Sopenharmony_ci generated files to the commit using `git add`. 861cb0ef41Sopenharmony_ci5. Commit and tag. 871cb0ef41Sopenharmony_ci6. Run the `postversion` script. Use it to clean up the file system or 881cb0ef41Sopenharmony_ci automatically push the commit and/or tag. 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ciTake the following example: 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci```json 931cb0ef41Sopenharmony_ci{ 941cb0ef41Sopenharmony_ci "scripts": { 951cb0ef41Sopenharmony_ci "preversion": "npm test", 961cb0ef41Sopenharmony_ci "version": "npm run build && git add -A dist", 971cb0ef41Sopenharmony_ci "postversion": "git push && git push --tags && rm -rf build/temp" 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci``` 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ciThis runs all your tests, and proceeds only if they pass. Then runs your 1031cb0ef41Sopenharmony_ci`build` script, and adds everything in the `dist` directory to the commit. 1041cb0ef41Sopenharmony_ciAfter the commit, it pushes the new commit and tag up to the server, and 1051cb0ef41Sopenharmony_cideletes the `build/temp` directory. 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci## API 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci### `npmVersion(newversion, options = {}) -> Promise<String>` 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciDo the things. Returns a promise that resolves to the new version if 1121cb0ef41Sopenharmony_ciall is well, or rejects if any errors are encountered. 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci### Options 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci#### `path` String 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ciThe path to the package being versionified. Defaults to process.cwd(). 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci#### `allowSameVersion` Boolean 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ciAllow setting the version to the current version in package.json. Default 1231cb0ef41Sopenharmony_ci`false`. 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci#### `preid` String 1261cb0ef41Sopenharmony_ciWhen the `newversion` is pre, premajor, preminor, or prepatch, this 1271cb0ef41Sopenharmony_cidefines the prerelease string, like 'beta' etc. 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci#### `tagVersionPrefix` String 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ciThe prefix to add to the raw semver string for the tag name. Defaults to 1321cb0ef41Sopenharmony_ci`'v'`. (So, by default it tags as 'v1.2.3' when versioning to 1.2.3.) 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci#### `commitHooks` Boolean 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ciRun git commit hooks. Default true. 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci#### `gitTagVersion` Boolean 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ciTag the version, default true. 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci#### `signGitCommit` Boolean 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ciGPG sign the git commit. Default `false`. 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci#### `signGitTag` Boolean 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ciGPG sign the git tag. Default `false`. 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci#### `force` Boolean 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ciPush forward recklessly if any problems happen. Default `false`. 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci#### `ignoreScripts` Boolean 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ciDo not run pre/post/version lifecycle scripts. Default `false`. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci#### `scriptShell` String 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ciPath to the shell, which should execute the lifecycle scripts. Defaults to `/bin/sh` on unix, or `cmd.exe` on windows. 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci#### `message` String 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ciThe message for the git commit and annotated git tag that are created. 165