semver 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. , semver = require("../semver")
  12. main()
  13. function main () {
  14. if (!argv.length) return help()
  15. while (argv.length) {
  16. var a
  17. switch (a = argv.shift()) {
  18. case "-v": case "--version":
  19. versions.push(argv.shift())
  20. break
  21. case "-r": case "--range":
  22. range.push(argv.shift())
  23. break
  24. case "-h": case "--help": case "-?":
  25. return help()
  26. default:
  27. versions.push(a)
  28. break
  29. }
  30. }
  31. versions = versions.filter(semver.valid)
  32. if (!versions.length) return fail()
  33. for (var i = 0, l = range.length; i < l ; i ++) {
  34. versions = versions.filter(function (v) {
  35. return semver.satisfies(v, range[i])
  36. })
  37. if (!versions.length) return fail()
  38. }
  39. return success(versions)
  40. }
  41. function fail () { process.exit(1) }
  42. function success () {
  43. versions.sort(semver.compare)
  44. .map(semver.clean)
  45. .forEach(function (v,i,_) { console.log(v) })
  46. }
  47. function help () {
  48. console.log(["Usage: semver -v <version> [-r <range>]"
  49. ,"Test if version(s) satisfy the supplied range(s),"
  50. ,"and sort them."
  51. ,""
  52. ,"Multiple versions or ranges may be supplied."
  53. ,""
  54. ,"Program exits successfully if any valid version satisfies"
  55. ,"all supplied ranges, and prints all satisfying versions."
  56. ,""
  57. ,"If no versions are valid, or ranges are not satisfied,"
  58. ,"then exits failure."
  59. ,""
  60. ,"Versions are printed in ascending order, so supplying"
  61. ,"multiple versions to the utility will just sort them."
  62. ].join("\n"))
  63. }