Nessuna descrizione

semver 4.3KB

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