07f63cc9006874c782c8d6b2fcda81b482df5593.svn-base 1.86 KB
#!/usr/bin/env node
// Standalone semver comparison program.
// Exits successfully and prints matching version(s) if
// any supplied version is valid and passes all tests.

var argv = process.argv.slice(2)
  , versions = []
  , range = []
  , gt = []
  , lt = []
  , eq = []
  , semver = require("../semver")

main()

function main () {
  if (!argv.length) return help()
  while (argv.length) {
    var a
    switch (a = argv.shift()) {
      case "-v": case "--version":
        versions.push(argv.shift())
        break
      case "-r": case "--range":
        range.push(argv.shift())
        break
      case "-h": case "--help": case "-?":
        return help()
      default:
        versions.push(a)
        break
    }
  }

  versions = versions.filter(semver.valid)
  for (var i = 0, l = range.length; i < l ; i ++) {
    versions = versions.filter(function (v) {
      return semver.satisfies(v, range[i])
    })
    if (!versions.length) return fail()
  }
  return success(versions)
}

function fail () { process.exit(1) }

function success () {
  versions.sort(semver.compare)
    .map(semver.clean)
    .forEach(function (v,i,_) { console.log(v) })
}

function help () {
  console.log(["Usage: semver -v <version> [-r <range>]"
              ,"Test if version(s) satisfy the supplied range(s),"
              ,"and sort them."
              ,""
              ,"Multiple versions or ranges may be supplied."
              ,""
              ,"Program exits successfully if any valid version satisfies"
              ,"all supplied ranges, and prints all satisfying versions."
              ,""
              ,"If no versions are valid, or ranges are not satisfied,"
              ,"then exits failure."
              ,""
              ,"Versions are printed in ascending order, so supplying"
              ,"multiple versions to the utility will just sort them."
              ].join("\n"))
}