diff options
author | isaacs <i@izs.me> | 2020-03-14 02:39:45 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:12:57 +0300 |
commit | 54e2a1b68b0753b87a6a5fdea37a17791a95477e (patch) | |
tree | ec28cc0ca882bf65e946d660515daff4b263d37c /node_modules/libnpmversion | |
parent | cd9f70f9f0466ecb4228fc0b25b3f2034af17b29 (diff) |
libnpmversion@1.0.2
Diffstat (limited to 'node_modules/libnpmversion')
-rw-r--r-- | node_modules/libnpmversion/LICENSE | 15 | ||||
-rw-r--r-- | node_modules/libnpmversion/README.md | 154 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/commit.js | 14 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/enforce-clean.js | 32 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/index.js | 39 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/proc-log.js | 21 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/read-json.js | 19 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/retrieve-tag.js | 11 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/tag.js | 20 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/version.js | 109 | ||||
-rw-r--r-- | node_modules/libnpmversion/lib/write-json.js | 16 | ||||
-rw-r--r-- | node_modules/libnpmversion/package.json | 72 |
12 files changed, 522 insertions, 0 deletions
diff --git a/node_modules/libnpmversion/LICENSE b/node_modules/libnpmversion/LICENSE new file mode 100644 index 000000000..05eeeb88c --- /dev/null +++ b/node_modules/libnpmversion/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) Isaac Z. Schlueter + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/node_modules/libnpmversion/README.md b/node_modules/libnpmversion/README.md new file mode 100644 index 000000000..daa0b8815 --- /dev/null +++ b/node_modules/libnpmversion/README.md @@ -0,0 +1,154 @@ +# libnpmversion + +Library to do the things that 'npm version' does. + +## USAGE + +```js +const npmVersion = require('libnpmversion') + +// argument can be one of: +// - any semver version string (set to that exact version) +// - 'major', 'minor', 'patch', 'pre{major,minor,patch}' (increment at +// that value) +// - 'from-git' (set to the latest semver-lookin git tag - this skips +// gitTagVersion, but will still sign if asked) +npmVersion(arg, { + path: '/path/to/my/pkg', // defaults to cwd + + allowSameVersion: false, // allow tagging/etc to the current version + preid: '', // when arg=='pre', define the prerelease string, like 'beta' etc. + tagVersionPrefix: 'v', // tag as 'v1.2.3' when versioning to 1.2.3 + commitHooks: true, // default true, run git commit hooks, default true + gitTagVersion: true, // default true, tag the version + signGitCommit: false, // default false, gpg sign the git commit + signGitTag: false, // default false, gpg sign the git tag + force: false, // push forward recklessly if any problems happen + ignoreScripts: false, // do not run pre/post/version lifecycle scripts + message: 'v%s', // message for tag and commit, replace %s with the version +}).then(newVersion => { + console.error('version updated!', newVersion) +}) +``` + +## Description + +Run this in a package directory to bump the version and write the new data +back to `package.json`, `package-lock.json`, and, if present, +`npm-shrinkwrap.json`. + +The `newversion` argument should be a valid semver string, a valid second +argument to [semver.inc](https://github.com/npm/node-semver#functions) (one +of `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, +`prerelease`), or `from-git`. In the second case, the existing version will +be incremented by 1 in the specified field. `from-git` will try to read +the latest git tag, and use that as the new npm version. + +If run in a git repo, it will also create a version commit and tag. This +behavior is controlled by `gitTagVersion` (see below), and can be +disabled by setting `gitTagVersion: false` in the options. +It will fail if the working directory is not clean, unless `force: true` is +set. + +If supplied with a `message` string option, it will +use it as a commit message when creating a version commit. If the +`message` option contains `%s` then that will be replaced with the +resulting version number. + +If the `signGitTag` option is set, then the tag will be signed using +the `-s` flag to git. Note that you must have a default GPG key set up in +your git config for this to work properly. + +If `preversion`, `version`, or `postversion` are in the `scripts` property +of the package.json, they will be executed in the appropriate sequence. + +The exact order of execution is as follows: + +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`, explicit version number, 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. + +Take the following example: + +```json +{ + "scripts": { + "preversion": "npm test", + "version": "npm run build && git add -A dist", + "postversion": "git push && git push --tags && rm -rf build/temp" + } +} +``` + +This runs all your tests, and proceeds only if they pass. Then runs your +`build` script, and adds everything in the `dist` directory to the commit. +After the commit, it pushes the new commit and tag up to the server, and +deletes the `build/temp` directory. + +## API + +### `npmVersion(newversion, options = {}) -> Promise<String>` + +Do the things. Returns a promise that resolves to the new version if +all is well, or rejects if any errors are encountered. + +### Options + +#### `path` String + +The path to the package being versionified. Defaults to process.cwd(). + +#### `allowSameVersion` Boolean + +Allow setting the version to the current version in package.json. Default +`false`. + +#### `preid` String +When the `newversion` is pre, premajor, preminor, or prepatch, this +defines the prerelease string, like 'beta' etc. + +#### `tagVersionPrefix` String + +The prefix to add to the raw semver string for the tag name. Defaults to +`'v'`. (So, by default it tags as 'v1.2.3' when versioning to 1.2.3.) + +#### `commitHooks` Boolean + +Run git commit hooks. Default true. + +#### `gitTagVersion` Boolean + +Tag the version, default true. + +#### `signGitCommit` Boolean + +GPG sign the git commit. Default `false`. + +#### `signGitTag` Boolean + +GPG sign the git tag. Default `false`. + +#### `force` Boolean + +Push forward recklessly if any problems happen. Default `false`. + +#### `ignoreScripts` Boolean + +Do not run pre/post/version lifecycle scripts. Default `false`. + +#### `message` String + +The message for the git commit and annotated git tag that are created. diff --git a/node_modules/libnpmversion/lib/commit.js b/node_modules/libnpmversion/lib/commit.js new file mode 100644 index 000000000..bd621acb4 --- /dev/null +++ b/node_modules/libnpmversion/lib/commit.js @@ -0,0 +1,14 @@ +const git = require('@npmcli/git') + +module.exports = (version, opts) => { + const {commitHooks, allowSameVersion, signGitCommit, message} = opts + const args = ['commit'] + if (commitHooks === false) + args.push('-n') + if (allowSameVersion) + args.push('--allow-empty') + if (signGitCommit) + args.push('-S') + args.push('-m') + return git.spawn([...args, message.replace(/%s/g, version)], opts) +} diff --git a/node_modules/libnpmversion/lib/enforce-clean.js b/node_modules/libnpmversion/lib/enforce-clean.js new file mode 100644 index 000000000..980419ffb --- /dev/null +++ b/node_modules/libnpmversion/lib/enforce-clean.js @@ -0,0 +1,32 @@ +const git = require('@npmcli/git') + +// returns true if it's cool to do git stuff +// throws if it's unclean, and not forced. +module.exports = async opts => { + const { force, log } = opts + let hadError = false + const clean = await git.isClean(opts).catch(er => { + if (er.code === 'ENOGIT') { + log.warn( + 'version', + 'This is a Git checkout, but the git command was not found.', + 'npm could not create a Git tag for this release!' + ) + hadError = true + // how can merges be real if our git isn't real? + return true + } else + throw er + }) + + if (!clean) { + if (!force) + throw new Error('Git working directory not clean.') + log.warn( + 'version', + 'Git working directory not clean, proceeding forcefully.' + ) + } + + return !hadError +} diff --git a/node_modules/libnpmversion/lib/index.js b/node_modules/libnpmversion/lib/index.js new file mode 100644 index 000000000..c3f554834 --- /dev/null +++ b/node_modules/libnpmversion/lib/index.js @@ -0,0 +1,39 @@ +const readJson = require('./read-json.js') +const version = require('./version.js') +const proclog = require('./proc-log.js') + +module.exports = async (newversion, opts = {}) => { + const { + path = process.cwd(), + allowSameVersion = false, + tagVersionPrefix = 'v', + commitHooks = true, + gitTagVersion = true, + signGitCommit = false, + signGitTag = false, + force = false, + ignoreScripts = false, + preid = null, + log = proclog, + message = 'v%s', + } = opts + + const pkg = opts.pkg || await readJson(path + '/package.json') + + return version(newversion, { + path, + cwd: path, + allowSameVersion, + tagVersionPrefix, + commitHooks, + gitTagVersion, + signGitCommit, + signGitTag, + force, + ignoreScripts, + preid, + pkg, + log, + message, + }) +} diff --git a/node_modules/libnpmversion/lib/proc-log.js b/node_modules/libnpmversion/lib/proc-log.js new file mode 100644 index 000000000..b2bdd9dc9 --- /dev/null +++ b/node_modules/libnpmversion/lib/proc-log.js @@ -0,0 +1,21 @@ +// default logger. +// emits 'log' events on the process +const LEVELS = [ + 'notice', + 'error', + 'warn', + 'info', + 'verbose', + 'http', + 'silly', + 'pause', + 'resume' +] + +const log = level => (...args) => process.emit('log', level, ...args) + +const logger = {} +for (const level of LEVELS) { + logger[level] = log(level) +} +module.exports = logger diff --git a/node_modules/libnpmversion/lib/read-json.js b/node_modules/libnpmversion/lib/read-json.js new file mode 100644 index 000000000..8c160a7b4 --- /dev/null +++ b/node_modules/libnpmversion/lib/read-json.js @@ -0,0 +1,19 @@ +// the same as read-package-json-fast, but stash the +// indent and newline on known symbols for later retrieval. +const {normalize} = require('read-package-json-fast') +const jsonParse = require('json-parse-even-better-errors') +const {promisify} = require('util') +const readFile = promisify(require('fs').readFile) +const kIndent = Symbol.for('indent') +const kNewline = Symbol.for('newline') + +module.exports = async path => readFile(path, 'utf8').then(data => { + // just take the first line-break and indent, if any are present + const m = data.match(/(\r?\n)(\s*)"/) + const newline = m ? m[1] : '\n' + const indent = m ? m[2] : '' + const pkg = normalize(jsonParse(data)) + pkg[kIndent] = indent + pkg[kNewline] = newline + return pkg +}) diff --git a/node_modules/libnpmversion/lib/retrieve-tag.js b/node_modules/libnpmversion/lib/retrieve-tag.js new file mode 100644 index 000000000..b657561b8 --- /dev/null +++ b/node_modules/libnpmversion/lib/retrieve-tag.js @@ -0,0 +1,11 @@ +const {spawn} = require('@npmcli/git') +const semver = require('semver') + +module.exports = async opts => { + const tag = (await spawn(['describe', '--abbrev=0'], opts)).stdout.trim() + const match = tag.match(/v?(\d+\.\d+\.\d+(?:[-+].+)?)/) + const ver = match && semver.clean(match[1], { loose: true }) + if (ver) + return ver + throw new Error(`Tag is not a valid version: ${JSON.stringify(tag)}`) +} diff --git a/node_modules/libnpmversion/lib/tag.js b/node_modules/libnpmversion/lib/tag.js new file mode 100644 index 000000000..bd6c803a3 --- /dev/null +++ b/node_modules/libnpmversion/lib/tag.js @@ -0,0 +1,20 @@ +const git = require('@npmcli/git') + +module.exports = async (version, opts) => { + const { signGitTag, allowSameVersion, tagVersionPrefix, message } = opts + const tag = `${tagVersionPrefix}${version}` + + const flags = ['-'] + if (signGitTag) + flags.push('s') + if (allowSameVersion) + flags.push('f') + flags.push('m') + + return git.spawn([ + 'tag', + flags.join(''), + message.replace(/%s/g, version), + tag, + ], opts) +} diff --git a/node_modules/libnpmversion/lib/version.js b/node_modules/libnpmversion/lib/version.js new file mode 100644 index 000000000..6630187f5 --- /dev/null +++ b/node_modules/libnpmversion/lib/version.js @@ -0,0 +1,109 @@ +// called with all the options already set to their defaults + +const retrieveTag = require('./retrieve-tag.js') +const semver = require('semver') +const enforceClean = require('./enforce-clean.js') +const writeJson = require('./write-json.js') +const readJson = require('./read-json.js') +const git = require('@npmcli/git') +const commit = require('./commit.js') +const tag = require('./tag.js') + +const runScript = require('@npmcli/run-script') +const runner = opts => event => runScript({ + ...opts, + stdio: 'inherit', + event, +}) + +module.exports = async (newversion, opts) => { + const { + path, + allowSameVersion, + tagVersionPrefix, + commitHooks, + gitTagVersion, + signGitCommit, + signGitTag, + force, + ignoreScripts, + preid, + pkg, + log, + message, + } = opts + + const { valid, clean, inc } = semver + const current = pkg.version + const currentClean = clean(current) + + const newV = valid(newversion, { loose: true }) ? clean(newversion, { loose: true }) + : newversion === 'from-git' ? await retrieveTag(opts) + : inc(currentClean, newversion, { loose: true }, preid) + + if (!newV) { + throw Object.assign(new Error('Invalid version: ' + newversion), { + current, + requested: newversion, + }) + } + + if (newV === currentClean && !allowSameVersion) { + throw Object.assign(new Error('Version not changed'), { + current, + requested: newversion, + newVersion: newV, + }) + } + + const isGitDir = newversion === 'from-git' || await git.is(opts) + + // ok! now we know the new version, and the old version is in pkg + + // - check if git dir is clean + // returns false if we should not keep doing git stuff + const doGit = isGitDir && await enforceClean(opts) + + const runScript = ignoreScripts ? () => {} : runner({ + ...opts, + pkg, + env: { + npm_old_version: current, + npm_new_version: newV, + }, + }) + + + await runScript('preversion') + + // - update the files + pkg.version = newV + delete pkg._id + await writeJson(`${path}/package.json`, pkg) + + // try to update shrinkwrap, but ok if this fails + const locks = [`${path}/package-lock.json`, `${path}/npm-shrinkwrap.json`] + const haveLocks = [] + for (const lock of locks) { + try { + const sw = await readJson(lock) + sw.version = newV + await writeJson(lock, sw) + haveLocks.push(lock) + } catch (er) {} + } + + await runScript('version') + + if (doGit) { + // - git add, git commit, git tag + await git.spawn(['add', `${path}/package.json`, ...haveLocks], opts) + await commit(newV, opts) + await tag(newV, opts) + } else + log.verbose('version', 'Not tagging: not in a git repo or no git cmd') + + await runScript('postversion') + + return newV +} diff --git a/node_modules/libnpmversion/lib/write-json.js b/node_modules/libnpmversion/lib/write-json.js new file mode 100644 index 000000000..30ca1af0f --- /dev/null +++ b/node_modules/libnpmversion/lib/write-json.js @@ -0,0 +1,16 @@ +// write the json back, preserving the line breaks and indent +const {promisify} = require('util') +const writeFile = promisify(require('fs').writeFile) +const kIndent = Symbol.for('indent') +const kNewline = Symbol.for('newline') + +module.exports = async (path, pkg) => { + const { + [kIndent]: indent = 2, + [kNewline]: newline = '\n', + } = pkg + delete pkg._id + const raw = JSON.stringify(pkg, null, indent) + '\n' + const data = newline === '\n' ? raw : raw.split('\n').join(newline) + return writeFile(path, data) +} diff --git a/node_modules/libnpmversion/package.json b/node_modules/libnpmversion/package.json new file mode 100644 index 000000000..158a997b6 --- /dev/null +++ b/node_modules/libnpmversion/package.json @@ -0,0 +1,72 @@ +{ + "_from": "libnpmversion@latest", + "_id": "libnpmversion@1.0.2", + "_inBundle": false, + "_integrity": "sha512-W2trOIhz/5QMDfnnAe/nhbym11+ZEjXfq15XpTjccGrOMqnH7Q/CFeUrFn9HpPV+UqhCcLIsOsok0VoZJ6vCIQ==", + "_location": "/libnpmversion", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "libnpmversion@latest", + "name": "libnpmversion", + "escapedName": "libnpmversion", + "rawSpec": "latest", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/libnpmversion/-/libnpmversion-1.0.2.tgz", + "_shasum": "0f02af1b8502556642e04dfd279181931891a96a", + "_spec": "libnpmversion@latest", + "_where": "/Users/isaacs/dev/npm/cli", + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "https://izs.me" + }, + "bugs": { + "url": "https://github.com/npm/libnpmversion/issues" + }, + "bundleDependencies": false, + "dependencies": { + "@npmcli/git": "^2.0.1", + "@npmcli/run-script": "^1.2.1", + "json-parse-even-better-errors": "^2.2.0", + "read-package-json-fast": "^1.1.3", + "semver": "^7.1.3", + "stringify-package": "^1.0.1" + }, + "deprecated": false, + "description": "library to do the things that 'npm version' does", + "devDependencies": { + "require-inject": "^1.4.4", + "tap": "^14.10.6" + }, + "files": [ + "lib/*.js" + ], + "homepage": "https://github.com/npm/libnpmversion#readme", + "license": "ISC", + "main": "lib/index.js", + "name": "libnpmversion", + "repository": { + "type": "git", + "url": "git+https://github.com/npm/libnpmversion.git" + }, + "scripts": { + "postversion": "npm publish", + "prepublishOnly": "git push origin --follow-tags", + "preversion": "npm test", + "snap": "tap", + "test": "tap" + }, + "tap": { + "coverage-map": "map.js", + "check-coverage": true + }, + "version": "1.0.2" +} |