Skip to content

Commit

Permalink
Apply 'standard' to bin/semver, add to npm test
Browse files Browse the repository at this point in the history
  • Loading branch information
pjohnmeyer authored and isaacs committed Oct 12, 2018
1 parent a34ca82 commit a4ff4ff
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 78 deletions.
161 changes: 84 additions & 77 deletions bin/semver
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,74 @@
// any supplied version is valid and passes all tests.

var argv = process.argv.slice(2)
, versions = []
, range = []
, gt = []
, lt = []
, eq = []
, inc = null
, version = require("../package.json").version
, loose = false
, includePrerelease = false
, coerce = false
, identifier = undefined
, semver = require("../semver")
, reverse = false
, options = {}

var versions = []

var range = []

var inc = null

var version = require('../package.json').version

var loose = false

var includePrerelease = false

var coerce = false

var identifier

var semver = require('../semver')

var reverse = false

var options = {}

main()

function main () {
if (!argv.length) return help()
while (argv.length) {
var a = argv.shift()
var i = a.indexOf('=')
if (i !== -1) {
a = a.slice(0, i)
argv.unshift(a.slice(i + 1))
var indexOfEqualSign = a.indexOf('=')
if (indexOfEqualSign !== -1) {
a = a.slice(0, indexOfEqualSign)
argv.unshift(a.slice(indexOfEqualSign + 1))
}
switch (a) {
case "-rv": case "-rev": case "--rev": case "--reverse":
case '-rv': case '-rev': case '--rev': case '--reverse':
reverse = true
break
case "-l": case "--loose":
case '-l': case '--loose':
loose = true
break
case "-p": case "--include-prerelease":
case '-p': case '--include-prerelease':
includePrerelease = true
break
case "-v": case "--version":
case '-v': case '--version':
versions.push(argv.shift())
break
case "-i": case "--inc": case "--increment":
case '-i': case '--inc': case '--increment':
switch (argv[0]) {
case "major": case "minor": case "patch": case "prerelease":
case "premajor": case "preminor": case "prepatch":
case 'major': case 'minor': case 'patch': case 'prerelease':
case 'premajor': case 'preminor': case 'prepatch':
inc = argv.shift()
break
default:
inc = "patch"
inc = 'patch'
break
}
break
case "--preid":
case '--preid':
identifier = argv.shift()
break
case "-r": case "--range":
case '-r': case '--range':
range.push(argv.shift())
break
case "-c": case "--coerce":
case '-c': case '--coerce':
coerce = true
break
case "-h": case "--help": case "-?":
case '-h': case '--help': case '-?':
return help()
default:
versions.push(a)
Expand All @@ -74,15 +82,14 @@ function main () {
var options = { loose: loose, includePrerelease: includePrerelease }

versions = versions.map(function (v) {
return coerce ? (semver.coerce(v) || {version: v}).version : v
return coerce ? (semver.coerce(v) || { version: v }).version : v
}).filter(function (v) {
return semver.valid(v)
})
if (!versions.length) return fail()
if (inc && (versions.length !== 1 || range.length))
return failInc()
if (inc && (versions.length !== 1 || range.length)) { return failInc() }

for (var i = 0, l = range.length; i < l ; i ++) {
for (var i = 0, l = range.length; i < l; i++) {
versions = versions.filter(function (v) {
return semver.satisfies(v, range[i], options)
})
Expand All @@ -92,62 +99,62 @@ function main () {
}

function failInc () {
console.error("--inc can only be used on a single version with no range")
console.error('--inc can only be used on a single version with no range')
fail()
}

function fail () { process.exit(1) }

function success () {
var compare = reverse ? "rcompare" : "compare"
var compare = reverse ? 'rcompare' : 'compare'
versions.sort(function (a, b) {
return semver[compare](a, b, options)
}).map(function (v) {
return semver.clean(v, options)
}).map(function (v) {
return inc ? semver.inc(v, inc, options, identifier) : v
}).forEach(function (v,i,_) { console.log(v) })
}).forEach(function (v, i, _) { console.log(v) })
}

function help () {
console.log(["SemVer " + version
,""
,"A JavaScript implementation of the http://semver.org/ specification"
,"Copyright Isaac Z. Schlueter"
,""
,"Usage: semver [options] <version> [<version> [...]]"
,"Prints valid versions sorted by SemVer precedence"
,""
,"Options:"
,"-r --range <range>"
," Print versions that match the specified range."
,""
,"-i --increment [<level>]"
," Increment a version by the specified level. Level can"
," be one of: major, minor, patch, premajor, preminor,"
," prepatch, or prerelease. Default level is 'patch'."
," Only one version may be specified."
,""
,"--preid <identifier>"
," Identifier to be used to prefix premajor, preminor,"
," prepatch or prerelease version increments."
,""
,"-l --loose"
," Interpret versions and ranges loosely"
,""
,"-p --include-prerelease"
," Always include prerelease versions in range matching"
,""
,"-c --coerce"
," Coerce a string into SemVer if possible"
," (does not imply --loose)"
,""
,"Program exits successfully if any valid version satisfies"
,"all supplied ranges, and prints all satisfying versions."
,""
,"If no satisfying versions are found, then exits failure."
,""
,"Versions are printed in ascending order, so supplying"
,"multiple versions to the utility will just sort them."
].join("\n"))
console.log(['SemVer ' + version,
'',
'A JavaScript implementation of the http://semver.org/ specification',
'Copyright Isaac Z. Schlueter',
'',
'Usage: semver [options] <version> [<version> [...]]',
'Prints valid versions sorted by SemVer precedence',
'',
'Options:',
'-r --range <range>',
' Print versions that match the specified range.',
'',
'-i --increment [<level>]',
' Increment a version by the specified level. Level can',
' be one of: major, minor, patch, premajor, preminor,',
" prepatch, or prerelease. Default level is 'patch'.",
' Only one version may be specified.',
'',
'--preid <identifier>',
' Identifier to be used to prefix premajor, preminor,',
' prepatch or prerelease version increments.',
'',
'-l --loose',
' Interpret versions and ranges loosely',
'',
'-p --include-prerelease',
' Always include prerelease versions in range matching',
'',
'-c --coerce',
' Coerce a string into SemVer if possible',
' (does not imply --loose)',
'',
'Program exits successfully if any valid version satisfies',
'all supplied ranges, and prints all satisfying versions.',
'',
'If no satisfying versions are found, then exits failure.',
'',
'Versions are printed in ascending order, so supplying',
'multiple versions to the utility will just sort them.'
].join('\n'))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The semantic version parser used by npm.",
"main": "semver.js",
"scripts": {
"test": "standard && tap test/*.js --cov -J"
"test": "standard bin/semver *.js **/*.js && tap test/*.js --cov -J"
},
"devDependencies": {
"standard": "^12.0.1",
Expand Down

0 comments on commit a4ff4ff

Please sign in to comment.