You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

semver.js 4.6KB

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