11cb0ef41Sopenharmony_ci#!/usr/bin/env node 21cb0ef41Sopenharmony_ci// Standalone semver comparison program. 31cb0ef41Sopenharmony_ci// Exits successfully and prints matching version(s) if 41cb0ef41Sopenharmony_ci// any supplied version is valid and passes all tests. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst argv = process.argv.slice(2) 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cilet versions = [] 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst range = [] 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_cilet inc = null 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst version = require('../package.json').version 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_cilet loose = false 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cilet includePrerelease = false 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cilet coerce = false 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cilet rtl = false 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_cilet identifier 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cilet identifierBase 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst semver = require('../') 291cb0ef41Sopenharmony_ciconst parseOptions = require('../internal/parse-options') 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cilet reverse = false 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cilet options = {} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciconst main = () => { 361cb0ef41Sopenharmony_ci if (!argv.length) { 371cb0ef41Sopenharmony_ci return help() 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci while (argv.length) { 401cb0ef41Sopenharmony_ci let a = argv.shift() 411cb0ef41Sopenharmony_ci const indexOfEqualSign = a.indexOf('=') 421cb0ef41Sopenharmony_ci if (indexOfEqualSign !== -1) { 431cb0ef41Sopenharmony_ci const value = a.slice(indexOfEqualSign + 1) 441cb0ef41Sopenharmony_ci a = a.slice(0, indexOfEqualSign) 451cb0ef41Sopenharmony_ci argv.unshift(value) 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci switch (a) { 481cb0ef41Sopenharmony_ci case '-rv': case '-rev': case '--rev': case '--reverse': 491cb0ef41Sopenharmony_ci reverse = true 501cb0ef41Sopenharmony_ci break 511cb0ef41Sopenharmony_ci case '-l': case '--loose': 521cb0ef41Sopenharmony_ci loose = true 531cb0ef41Sopenharmony_ci break 541cb0ef41Sopenharmony_ci case '-p': case '--include-prerelease': 551cb0ef41Sopenharmony_ci includePrerelease = true 561cb0ef41Sopenharmony_ci break 571cb0ef41Sopenharmony_ci case '-v': case '--version': 581cb0ef41Sopenharmony_ci versions.push(argv.shift()) 591cb0ef41Sopenharmony_ci break 601cb0ef41Sopenharmony_ci case '-i': case '--inc': case '--increment': 611cb0ef41Sopenharmony_ci switch (argv[0]) { 621cb0ef41Sopenharmony_ci case 'major': case 'minor': case 'patch': case 'prerelease': 631cb0ef41Sopenharmony_ci case 'premajor': case 'preminor': case 'prepatch': 641cb0ef41Sopenharmony_ci inc = argv.shift() 651cb0ef41Sopenharmony_ci break 661cb0ef41Sopenharmony_ci default: 671cb0ef41Sopenharmony_ci inc = 'patch' 681cb0ef41Sopenharmony_ci break 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci break 711cb0ef41Sopenharmony_ci case '--preid': 721cb0ef41Sopenharmony_ci identifier = argv.shift() 731cb0ef41Sopenharmony_ci break 741cb0ef41Sopenharmony_ci case '-r': case '--range': 751cb0ef41Sopenharmony_ci range.push(argv.shift()) 761cb0ef41Sopenharmony_ci break 771cb0ef41Sopenharmony_ci case '-n': 781cb0ef41Sopenharmony_ci identifierBase = argv.shift() 791cb0ef41Sopenharmony_ci if (identifierBase === 'false') { 801cb0ef41Sopenharmony_ci identifierBase = false 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci break 831cb0ef41Sopenharmony_ci case '-c': case '--coerce': 841cb0ef41Sopenharmony_ci coerce = true 851cb0ef41Sopenharmony_ci break 861cb0ef41Sopenharmony_ci case '--rtl': 871cb0ef41Sopenharmony_ci rtl = true 881cb0ef41Sopenharmony_ci break 891cb0ef41Sopenharmony_ci case '--ltr': 901cb0ef41Sopenharmony_ci rtl = false 911cb0ef41Sopenharmony_ci break 921cb0ef41Sopenharmony_ci case '-h': case '--help': case '-?': 931cb0ef41Sopenharmony_ci return help() 941cb0ef41Sopenharmony_ci default: 951cb0ef41Sopenharmony_ci versions.push(a) 961cb0ef41Sopenharmony_ci break 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci options = parseOptions({ loose, includePrerelease, rtl }) 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci versions = versions.map((v) => { 1031cb0ef41Sopenharmony_ci return coerce ? (semver.coerce(v, options) || { version: v }).version : v 1041cb0ef41Sopenharmony_ci }).filter((v) => { 1051cb0ef41Sopenharmony_ci return semver.valid(v) 1061cb0ef41Sopenharmony_ci }) 1071cb0ef41Sopenharmony_ci if (!versions.length) { 1081cb0ef41Sopenharmony_ci return fail() 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci if (inc && (versions.length !== 1 || range.length)) { 1111cb0ef41Sopenharmony_ci return failInc() 1121cb0ef41Sopenharmony_ci } 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci for (let i = 0, l = range.length; i < l; i++) { 1151cb0ef41Sopenharmony_ci versions = versions.filter((v) => { 1161cb0ef41Sopenharmony_ci return semver.satisfies(v, range[i], options) 1171cb0ef41Sopenharmony_ci }) 1181cb0ef41Sopenharmony_ci if (!versions.length) { 1191cb0ef41Sopenharmony_ci return fail() 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci return success(versions) 1231cb0ef41Sopenharmony_ci} 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciconst failInc = () => { 1261cb0ef41Sopenharmony_ci console.error('--inc can only be used on a single version with no range') 1271cb0ef41Sopenharmony_ci fail() 1281cb0ef41Sopenharmony_ci} 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ciconst fail = () => process.exit(1) 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ciconst success = () => { 1331cb0ef41Sopenharmony_ci const compare = reverse ? 'rcompare' : 'compare' 1341cb0ef41Sopenharmony_ci versions.sort((a, b) => { 1351cb0ef41Sopenharmony_ci return semver[compare](a, b, options) 1361cb0ef41Sopenharmony_ci }).map((v) => { 1371cb0ef41Sopenharmony_ci return semver.clean(v, options) 1381cb0ef41Sopenharmony_ci }).map((v) => { 1391cb0ef41Sopenharmony_ci return inc ? semver.inc(v, inc, options, identifier, identifierBase) : v 1401cb0ef41Sopenharmony_ci }).forEach((v, i, _) => { 1411cb0ef41Sopenharmony_ci console.log(v) 1421cb0ef41Sopenharmony_ci }) 1431cb0ef41Sopenharmony_ci} 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ciconst help = () => console.log( 1461cb0ef41Sopenharmony_ci`SemVer ${version} 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ciA JavaScript implementation of the https://semver.org/ specification 1491cb0ef41Sopenharmony_ciCopyright Isaac Z. Schlueter 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciUsage: semver [options] <version> [<version> [...]] 1521cb0ef41Sopenharmony_ciPrints valid versions sorted by SemVer precedence 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciOptions: 1551cb0ef41Sopenharmony_ci-r --range <range> 1561cb0ef41Sopenharmony_ci Print versions that match the specified range. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci-i --increment [<level>] 1591cb0ef41Sopenharmony_ci Increment a version by the specified level. Level can 1601cb0ef41Sopenharmony_ci be one of: major, minor, patch, premajor, preminor, 1611cb0ef41Sopenharmony_ci prepatch, or prerelease. Default level is 'patch'. 1621cb0ef41Sopenharmony_ci Only one version may be specified. 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci--preid <identifier> 1651cb0ef41Sopenharmony_ci Identifier to be used to prefix premajor, preminor, 1661cb0ef41Sopenharmony_ci prepatch or prerelease version increments. 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci-l --loose 1691cb0ef41Sopenharmony_ci Interpret versions and ranges loosely 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci-p --include-prerelease 1721cb0ef41Sopenharmony_ci Always include prerelease versions in range matching 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci-c --coerce 1751cb0ef41Sopenharmony_ci Coerce a string into SemVer if possible 1761cb0ef41Sopenharmony_ci (does not imply --loose) 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci--rtl 1791cb0ef41Sopenharmony_ci Coerce version strings right to left 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci--ltr 1821cb0ef41Sopenharmony_ci Coerce version strings left to right (default) 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci-n <base> 1851cb0ef41Sopenharmony_ci Base number to be used for the prerelease identifier. 1861cb0ef41Sopenharmony_ci Can be either 0 or 1, or false to omit the number altogether. 1871cb0ef41Sopenharmony_ci Defaults to 0. 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ciProgram exits successfully if any valid version satisfies 1901cb0ef41Sopenharmony_ciall supplied ranges, and prints all satisfying versions. 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ciIf no satisfying versions are found, then exits failure. 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ciVersions are printed in ascending order, so supplying 1951cb0ef41Sopenharmony_cimultiple versions to the utility will just sort them.`) 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_cimain() 198