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.

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