[6a3a178] | 1 | #!/usr/bin/env node
|
---|
| 2 | // Standalone semver comparison program.
|
---|
| 3 | // Exits successfully and prints matching version(s) if
|
---|
| 4 | // any supplied version is valid and passes all tests.
|
---|
| 5 |
|
---|
| 6 | const argv = process.argv.slice(2)
|
---|
| 7 |
|
---|
| 8 | let versions = []
|
---|
| 9 |
|
---|
| 10 | const range = []
|
---|
| 11 |
|
---|
| 12 | let inc = null
|
---|
| 13 |
|
---|
| 14 | const version = require('../package.json').version
|
---|
| 15 |
|
---|
| 16 | let loose = false
|
---|
| 17 |
|
---|
| 18 | let includePrerelease = false
|
---|
| 19 |
|
---|
| 20 | let coerce = false
|
---|
| 21 |
|
---|
| 22 | let rtl = false
|
---|
| 23 |
|
---|
| 24 | let identifier
|
---|
| 25 |
|
---|
| 26 | const semver = require('../')
|
---|
| 27 |
|
---|
| 28 | let reverse = false
|
---|
| 29 |
|
---|
| 30 | const options = {}
|
---|
| 31 |
|
---|
| 32 | const main = () => {
|
---|
| 33 | if (!argv.length) return help()
|
---|
| 34 | while (argv.length) {
|
---|
| 35 | let a = argv.shift()
|
---|
| 36 | const indexOfEqualSign = a.indexOf('=')
|
---|
| 37 | if (indexOfEqualSign !== -1) {
|
---|
| 38 | a = a.slice(0, indexOfEqualSign)
|
---|
| 39 | argv.unshift(a.slice(indexOfEqualSign + 1))
|
---|
| 40 | }
|
---|
| 41 | switch (a) {
|
---|
| 42 | case '-rv': case '-rev': case '--rev': case '--reverse':
|
---|
| 43 | reverse = true
|
---|
| 44 | break
|
---|
| 45 | case '-l': case '--loose':
|
---|
| 46 | loose = true
|
---|
| 47 | break
|
---|
| 48 | case '-p': case '--include-prerelease':
|
---|
| 49 | includePrerelease = true
|
---|
| 50 | break
|
---|
| 51 | case '-v': case '--version':
|
---|
| 52 | versions.push(argv.shift())
|
---|
| 53 | break
|
---|
| 54 | case '-i': case '--inc': case '--increment':
|
---|
| 55 | switch (argv[0]) {
|
---|
| 56 | case 'major': case 'minor': case 'patch': case 'prerelease':
|
---|
| 57 | case 'premajor': case 'preminor': case 'prepatch':
|
---|
| 58 | inc = argv.shift()
|
---|
| 59 | break
|
---|
| 60 | default:
|
---|
| 61 | inc = 'patch'
|
---|
| 62 | break
|
---|
| 63 | }
|
---|
| 64 | break
|
---|
| 65 | case '--preid':
|
---|
| 66 | identifier = argv.shift()
|
---|
| 67 | break
|
---|
| 68 | case '-r': case '--range':
|
---|
| 69 | range.push(argv.shift())
|
---|
| 70 | break
|
---|
| 71 | case '-c': case '--coerce':
|
---|
| 72 | coerce = true
|
---|
| 73 | break
|
---|
| 74 | case '--rtl':
|
---|
| 75 | rtl = true
|
---|
| 76 | break
|
---|
| 77 | case '--ltr':
|
---|
| 78 | rtl = false
|
---|
| 79 | break
|
---|
| 80 | case '-h': case '--help': case '-?':
|
---|
| 81 | return help()
|
---|
| 82 | default:
|
---|
| 83 | versions.push(a)
|
---|
| 84 | break
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | const options = { loose: loose, includePrerelease: includePrerelease, rtl: rtl }
|
---|
| 89 |
|
---|
| 90 | versions = versions.map((v) => {
|
---|
| 91 | return coerce ? (semver.coerce(v, options) || { version: v }).version : v
|
---|
| 92 | }).filter((v) => {
|
---|
| 93 | return semver.valid(v)
|
---|
| 94 | })
|
---|
| 95 | if (!versions.length) return fail()
|
---|
| 96 | if (inc && (versions.length !== 1 || range.length)) { return failInc() }
|
---|
| 97 |
|
---|
| 98 | for (let i = 0, l = range.length; i < l; i++) {
|
---|
| 99 | versions = versions.filter((v) => {
|
---|
| 100 | return semver.satisfies(v, range[i], options)
|
---|
| 101 | })
|
---|
| 102 | if (!versions.length) return fail()
|
---|
| 103 | }
|
---|
| 104 | return success(versions)
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | const failInc = () => {
|
---|
| 109 | console.error('--inc can only be used on a single version with no range')
|
---|
| 110 | fail()
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | const fail = () => process.exit(1)
|
---|
| 114 |
|
---|
| 115 | const success = () => {
|
---|
| 116 | const compare = reverse ? 'rcompare' : 'compare'
|
---|
| 117 | versions.sort((a, b) => {
|
---|
| 118 | return semver[compare](a, b, options)
|
---|
| 119 | }).map((v) => {
|
---|
| 120 | return semver.clean(v, options)
|
---|
| 121 | }).map((v) => {
|
---|
| 122 | return inc ? semver.inc(v, inc, options, identifier) : v
|
---|
| 123 | }).forEach((v, i, _) => { console.log(v) })
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | const help = () => console.log(
|
---|
| 127 | `SemVer ${version}
|
---|
| 128 |
|
---|
| 129 | A JavaScript implementation of the https://semver.org/ specification
|
---|
| 130 | Copyright Isaac Z. Schlueter
|
---|
| 131 |
|
---|
| 132 | Usage: semver [options] <version> [<version> [...]]
|
---|
| 133 | Prints valid versions sorted by SemVer precedence
|
---|
| 134 |
|
---|
| 135 | Options:
|
---|
| 136 | -r --range <range>
|
---|
| 137 | Print versions that match the specified range.
|
---|
| 138 |
|
---|
| 139 | -i --increment [<level>]
|
---|
| 140 | Increment a version by the specified level. Level can
|
---|
| 141 | be one of: major, minor, patch, premajor, preminor,
|
---|
| 142 | prepatch, or prerelease. Default level is 'patch'.
|
---|
| 143 | Only one version may be specified.
|
---|
| 144 |
|
---|
| 145 | --preid <identifier>
|
---|
| 146 | Identifier to be used to prefix premajor, preminor,
|
---|
| 147 | prepatch or prerelease version increments.
|
---|
| 148 |
|
---|
| 149 | -l --loose
|
---|
| 150 | Interpret versions and ranges loosely
|
---|
| 151 |
|
---|
| 152 | -p --include-prerelease
|
---|
| 153 | Always include prerelease versions in range matching
|
---|
| 154 |
|
---|
| 155 | -c --coerce
|
---|
| 156 | Coerce a string into SemVer if possible
|
---|
| 157 | (does not imply --loose)
|
---|
| 158 |
|
---|
| 159 | --rtl
|
---|
| 160 | Coerce version strings right to left
|
---|
| 161 |
|
---|
| 162 | --ltr
|
---|
| 163 | Coerce version strings left to right (default)
|
---|
| 164 |
|
---|
| 165 | Program exits successfully if any valid version satisfies
|
---|
| 166 | all supplied ranges, and prints all satisfying versions.
|
---|
| 167 |
|
---|
| 168 | If no satisfying versions are found, then exits failure.
|
---|
| 169 |
|
---|
| 170 | Versions are printed in ascending order, so supplying
|
---|
| 171 | multiple versions to the utility will just sort them.`)
|
---|
| 172 |
|
---|
| 173 | main()
|
---|