Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
lib/ | 12-May-2024 | - | 303 | 247 | ||
LICENSE | D | 12-May-2024 | 748 | 16 | 12 | |
README.md | D | 12-May-2024 | 6 KiB | 165 | 119 | |
package.json | D | 12-May-2024 | 1.3 KiB | 55 | 54 |
README.md
1# libnpmversion 2 3[](https://npm.im/libnpmversion) 4[](https://npm.im/libnpmversion) 5[](https://github.com/npm/cli/actions/workflows/ci-libnpmversion.yml) 6 7Library to do the things that 'npm version' does. 8 9## USAGE 10 11```js 12const npmVersion = require('libnpmversion') 13 14// argument can be one of: 15// - any semver version string (set to that exact version) 16// - 'major', 'minor', 'patch', 'pre{major,minor,patch}' (increment at 17// that value) 18// - 'from-git' (set to the latest semver-lookin git tag - this skips 19// gitTagVersion, but will still sign if asked) 20npmVersion(arg, { 21 path: '/path/to/my/pkg', // defaults to cwd 22 23 allowSameVersion: false, // allow tagging/etc to the current version 24 preid: '', // when arg=='pre', define the prerelease string, like 'beta' etc. 25 tagVersionPrefix: 'v', // tag as 'v1.2.3' when versioning to 1.2.3 26 commitHooks: true, // default true, run git commit hooks, default true 27 gitTagVersion: true, // default true, tag the version 28 signGitCommit: false, // default false, gpg sign the git commit 29 signGitTag: false, // default false, gpg sign the git tag 30 force: false, // push forward recklessly if any problems happen 31 ignoreScripts: false, // do not run pre/post/version lifecycle scripts 32 scriptShell: '/bin/bash', // shell to run lifecycle scripts in 33 message: 'v%s', // message for tag and commit, replace %s with the version 34 silent: false, // passed to @npmcli/run-script to control whether it logs 35}).then(newVersion => { 36 console.error('version updated!', newVersion) 37}) 38``` 39 40## Description 41 42Run this in a package directory to bump the version and write the new data 43back to `package.json`, `package-lock.json`, and, if present, 44`npm-shrinkwrap.json`. 45 46The `newversion` argument should be a valid semver string, a valid second 47argument to [semver.inc](https://github.com/npm/node-semver#functions) (one 48of `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, 49`prerelease`), or `from-git`. In the second case, the existing version will 50be incremented by 1 in the specified field. `from-git` will try to read 51the latest git tag, and use that as the new npm version. 52 53If run in a git repo, it will also create a version commit and tag. This 54behavior is controlled by `gitTagVersion` (see below), and can be 55disabled by setting `gitTagVersion: false` in the options. 56It will fail if the working directory is not clean, unless `force: true` is 57set. 58 59If supplied with a `message` string option, it will 60use it as a commit message when creating a version commit. If the 61`message` option contains `%s` then that will be replaced with the 62resulting version number. 63 64If the `signGitTag` option is set, then the tag will be signed using 65the `-s` flag to git. Note that you must have a default GPG key set up in 66your git config for this to work properly. 67 68If `preversion`, `version`, or `postversion` are in the `scripts` property 69of the package.json, they will be executed in the appropriate sequence. 70 71The exact order of execution is as follows: 72 731. Check to make sure the git working directory is clean before we get 74 started. Your scripts may add files to the commit in future steps. 75 This step is skipped if the `force` flag is set. 762. Run the `preversion` script. These scripts have access to the old 77 `version` in package.json. A typical use would be running your full 78 test suite before deploying. Any files you want added to the commit 79 should be explicitly added using `git add`. 803. Bump `version` in `package.json` as requested (`patch`, `minor`, 81 `major`, explicit version number, etc). 824. Run the `version` script. These scripts have access to the new `version` 83 in package.json (so they can incorporate it into file headers in 84 generated files for example). Again, scripts should explicitly add 85 generated files to the commit using `git add`. 865. Commit and tag. 876. Run the `postversion` script. Use it to clean up the file system or 88 automatically push the commit and/or tag. 89 90Take the following example: 91 92```json 93{ 94 "scripts": { 95 "preversion": "npm test", 96 "version": "npm run build && git add -A dist", 97 "postversion": "git push && git push --tags && rm -rf build/temp" 98 } 99} 100``` 101 102This runs all your tests, and proceeds only if they pass. Then runs your 103`build` script, and adds everything in the `dist` directory to the commit. 104After the commit, it pushes the new commit and tag up to the server, and 105deletes the `build/temp` directory. 106 107## API 108 109### `npmVersion(newversion, options = {}) -> Promise<String>` 110 111Do the things. Returns a promise that resolves to the new version if 112all is well, or rejects if any errors are encountered. 113 114### Options 115 116#### `path` String 117 118The path to the package being versionified. Defaults to process.cwd(). 119 120#### `allowSameVersion` Boolean 121 122Allow setting the version to the current version in package.json. Default 123`false`. 124 125#### `preid` String 126When the `newversion` is pre, premajor, preminor, or prepatch, this 127defines the prerelease string, like 'beta' etc. 128 129#### `tagVersionPrefix` String 130 131The prefix to add to the raw semver string for the tag name. Defaults to 132`'v'`. (So, by default it tags as 'v1.2.3' when versioning to 1.2.3.) 133 134#### `commitHooks` Boolean 135 136Run git commit hooks. Default true. 137 138#### `gitTagVersion` Boolean 139 140Tag the version, default true. 141 142#### `signGitCommit` Boolean 143 144GPG sign the git commit. Default `false`. 145 146#### `signGitTag` Boolean 147 148GPG sign the git tag. Default `false`. 149 150#### `force` Boolean 151 152Push forward recklessly if any problems happen. Default `false`. 153 154#### `ignoreScripts` Boolean 155 156Do not run pre/post/version lifecycle scripts. Default `false`. 157 158#### `scriptShell` String 159 160Path to the shell, which should execute the lifecycle scripts. Defaults to `/bin/sh` on unix, or `cmd.exe` on windows. 161 162#### `message` String 163 164The message for the git commit and annotated git tag that are created. 165