semver 4.3 KB

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