semver 4.0 KB

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