Skip to content

Commit

Permalink
version: allow prerelease identifier (#26)
Browse files Browse the repository at this point in the history
PR-URL: #26
Credit: @dwilches
Reviewed-By: @zkat
  • Loading branch information
dwilches authored and zkat committed Jul 23, 2018
1 parent 9db1540 commit ad0dd22
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/cli/npm-version.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ npm-version(1) -- Bump a package version

## SYNOPSIS

npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]

'npm [-v | --version]' to print npm version
'npm view <pkg> version' to view a package's published version
Expand Down
8 changes: 8 additions & 0 deletions doc/misc/npm-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,14 @@ for updates immediately even for fresh package data.
The location to install global items. If set on the command line, then
it forces non-global commands to run in the specified folder.

### preid

* Default: ""
* Type: String

The "prerelease identifier" to use as a prefix for the "prerelease" part of a
semver. Like the `rc` in `1.2.0-rc.8`.

### production

* Default: false
Expand Down
2 changes: 2 additions & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Object.defineProperty(exports, 'defaults', {get: function () {
'prefer-offline': false,
'prefer-online': false,
prefix: globalPrefix,
preid: '',
production: process.env.NODE_ENV === 'production',
'progress': !process.env.TRAVIS && !process.env.CI,
proxy: null,
Expand Down Expand Up @@ -329,6 +330,7 @@ exports.types = {
'prefer-offline': Boolean,
'prefer-online': Boolean,
prefix: path,
preid: String,
production: Boolean,
progress: Boolean,
proxy: [null, false, url], // allow proxy to be disabled explicitly
Expand Down
4 changes: 2 additions & 2 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const semver = require('semver')
const stringifyPackage = require('stringify-package')
const writeFileAtomic = require('write-file-atomic')

version.usage = 'npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]' +
version.usage = 'npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]' +
'\n(run in package dir)\n' +
"'npm -v' or 'npm --version' to print npm version " +
'(' + npm.version + ')\n' +
Expand Down Expand Up @@ -47,7 +47,7 @@ function version (args, silent, cb_) {
retrieveTagVersion(silent, data, cb_)
} else {
var newVersion = semver.valid(args[0])
if (!newVersion) newVersion = semver.inc(data.version, args[0])
if (!newVersion) newVersion = semver.inc(data.version, args[0], npm.config.get('preid'))
if (!newVersion) return cb_(version.usage)
persistVersion(newVersion, silent, data, cb_)
}
Expand Down
61 changes: 61 additions & 0 deletions test/tap/version-prerelease-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var fs = require('fs')
var path = require('path')

var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var npm = require('../../')
var common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'version-shrinkwrap')
var cache = path.resolve(pkg, 'cache')

var EXEC_OPTS = { cwd: pkg }

test('setup', function (t) {
setup()
t.end()
})

test('npm version --preid=rc uses prerelease id', function (t) {
setup()

npm.load({ cache: pkg + '/cache', registry: common.registry }, function () {
common.npm(['version', 'prerelease', '--preid=rc'], EXEC_OPTS, function (err) {
if (err) return t.fail('Error perform version prerelease')
var newVersion = require(path.resolve(pkg, 'package.json')).version
t.equal(newVersion, '0.0.1-rc.0', 'got expected version')
t.end()
})
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})

function setup () {
cleanup()
mkdirp.sync(pkg)
mkdirp.sync(cache)
var contents = {
author: 'Daniel Wilches',
name: 'version-prerelease-id',
version: '0.0.0',
description: 'Test for version of prereleases with preids'
}

fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify(contents), 'utf8')
fs.writeFileSync(path.resolve(pkg, 'npm-shrinkwrap.json'), JSON.stringify(contents), 'utf8')
process.chdir(pkg)
}

function cleanup () {
// windows fix for locked files
process.chdir(osenv.tmpdir())
rimraf.sync(cache)
rimraf.sync(pkg)
}

0 comments on commit ad0dd22

Please sign in to comment.