diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2015-10-15 04:38:12 +0300 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2015-10-15 08:24:58 +0300 |
commit | 1ead0a45d4b819a33ae740c90a2da12250c1baf7 (patch) | |
tree | ac494f5e5cc3928985a9ba9af17a0a48d5da33d4 /node_modules | |
parent | 759f88ae3f10368031943b3f8b836cc8b20089b3 (diff) |
which@1.2.0
Add command line options for command-line use.
PR-URL: https://github.com/npm/npm/pull/9981
Diffstat (limited to 'node_modules')
-rw-r--r-- | node_modules/which/.travis.yml | 6 | ||||
-rw-r--r-- | node_modules/which/README.md | 8 | ||||
-rwxr-xr-x | node_modules/which/bin/which | 52 | ||||
-rw-r--r-- | node_modules/which/package.json | 40 | ||||
-rw-r--r-- | node_modules/which/test/bin.js | 119 | ||||
-rw-r--r-- | node_modules/which/which.js | 25 |
6 files changed, 215 insertions, 35 deletions
diff --git a/node_modules/which/.travis.yml b/node_modules/which/.travis.yml index 4af02b3d1..7f22ad5a1 100644 --- a/node_modules/which/.travis.yml +++ b/node_modules/which/.travis.yml @@ -1,8 +1,6 @@ +sudo: false language: node_js node_js: - - '0.8' - '0.10' - '0.12' - - 'iojs' -before_install: - - npm install -g npm@latest + - '4' diff --git a/node_modules/which/README.md b/node_modules/which/README.md index d5571528a..3d6b92026 100644 --- a/node_modules/which/README.md +++ b/node_modules/which/README.md @@ -31,4 +31,10 @@ which('node', { path: someOtherPath }, function (er, resolved) { ## OPTIONS -If you pass in options, then `path` and `pathExt` are relevant. +You may pass an options object as the second argument. + +- `path`: Use instead of the `PATH` environment variable. +- `pathExt`: Use instead of the `PATHEXT` environment variable. +- `all`: Return all matches, instead of just the first one. Note that + this means the function returns an array of strings instead of a + single string. diff --git a/node_modules/which/bin/which b/node_modules/which/bin/which index 8432ce2f6..7cee3729e 100755 --- a/node_modules/which/bin/which +++ b/node_modules/which/bin/which @@ -1,14 +1,52 @@ #!/usr/bin/env node var which = require("../") -if (process.argv.length < 3) { - console.error("Usage: which <thing>") +if (process.argv.length < 3) + usage() + +function usage () { + console.error('usage: which [-as] program ...') process.exit(1) } -which(process.argv[2], function (er, thing) { - if (er) { - console.error(er.message) - process.exit(er.errno || 127) +var all = false +var silent = false +var dashdash = false +var args = process.argv.slice(2).filter(function (arg) { + if (dashdash || !/^-/.test(arg)) + return true + + if (arg === '--') { + dashdash = true + return false + } + + var flags = arg.substr(1).split('') + for (var f = 0; f < flags.length; f++) { + var flag = flags[f] + switch (flag) { + case 's': + silent = true + break + case 'a': + all = true + break + default: + console.error('which: illegal option -- ' + flag) + usage() + } } - console.log(thing) + return false }) + +process.exit(args.reduce(function (pv, current) { + try { + var f = which.sync(current, { all: all }) + if (all) + f = f.join('\n') + if (!silent) + console.log(f) + return pv; + } catch (e) { + return 1; + } +}, 0)) diff --git a/node_modules/which/package.json b/node_modules/which/package.json index af78bbeb3..a6e1ccef5 100644 --- a/node_modules/which/package.json +++ b/node_modules/which/package.json @@ -1,38 +1,40 @@ { "_args": [ [ - "which@~1.1.1", - "/Users/rebecca/code/npm" + "which@~1.2.0", + "/Users/ogd/Documents/projects/npm/npm" ] ], - "_from": "which@>=1.1.1 <1.2.0", - "_id": "which@1.1.2", + "_from": "which@>=1.2.0 <1.3.0", + "_id": "which@1.2.0", "_inCache": true, "_location": "/which", - "_nodeVersion": "2.5.0", + "_nodeVersion": "4.0.0", "_npmUser": { "email": "isaacs@npmjs.com", "name": "isaacs" }, - "_npmVersion": "3.3.1", + "_npmVersion": "3.3.2", "_phantomChildren": {}, "_requested": { "name": "which", - "raw": "which@~1.1.1", - "rawSpec": "~1.1.1", + "raw": "which@~1.2.0", + "rawSpec": "~1.2.0", "scope": null, - "spec": ">=1.1.1 <1.2.0", + "spec": ">=1.2.0 <1.3.0", "type": "range" }, "_requiredBy": [ "/", - "/node-gyp" + "/istanbul", + "/node-gyp", + "/npm-path" ], - "_resolved": "https://registry.npmjs.org/which/-/which-1.1.2.tgz", - "_shasum": "486c48af6dfecc7a7dcf9c655acf108d2dcbdf3d", + "_resolved": "https://registry.npmjs.org/which/-/which-1.2.0.tgz", + "_shasum": "a5c8df5abc792f6ce9652c8d9ca8f3a91b77e59d", "_shrinkwrap": null, - "_spec": "which@~1.1.1", - "_where": "/Users/rebecca/code/npm", + "_spec": "which@~1.2.0", + "_where": "/Users/ogd/Documents/projects/npm/npm", "author": { "email": "i@izs.me", "name": "Isaac Z. Schlueter", @@ -51,14 +53,14 @@ "devDependencies": { "mkdirp": "^0.5.0", "rimraf": "^2.3.3", - "tap": "^1.0.2" + "tap": "^2.0.0" }, "directories": {}, "dist": { - "shasum": "486c48af6dfecc7a7dcf9c655acf108d2dcbdf3d", - "tarball": "http://registry.npmjs.org/which/-/which-1.1.2.tgz" + "shasum": "a5c8df5abc792f6ce9652c8d9ca8f3a91b77e59d", + "tarball": "http://registry.npmjs.org/which/-/which-1.2.0.tgz" }, - "gitHead": "e576e42f0c377571884f844eec58b3ca4a331681", + "gitHead": "98925d6bced9ba820a17fd857e7a53a491958419", "homepage": "https://github.com/isaacs/node-which#readme", "installable": true, "license": "ISC", @@ -78,5 +80,5 @@ "scripts": { "test": "tap test/*.js" }, - "version": "1.1.2" + "version": "1.2.0" } diff --git a/node_modules/which/test/bin.js b/node_modules/which/test/bin.js new file mode 100644 index 000000000..ff7eb530c --- /dev/null +++ b/node_modules/which/test/bin.js @@ -0,0 +1,119 @@ +var t = require('tap') +var spawn = require('child_process').spawn +var node = process.execPath +var bin = require.resolve('../bin/which') + +function which (args, extraPath, cb) { + if (typeof extraPath === 'function') + cb = extraPath, extraPath = null + + var options = {} + if (extraPath) { + var sep = process.platform === 'win32' ? ';' : ':' + var p = process.env.PATH + sep + extraPath + options.env = Object.keys(process.env).reduce(function (env, k) { + if (!k.match(/^path$/i)) + env[k] = process.env[k] + return env + }, { PATH: p }) + } + + var out = '' + var err = '' + var child = spawn(node, [bin].concat(args), options) + child.stdout.on('data', function (c) { + out += c + }) + child.stderr.on('data', function (c) { + err += c + }) + child.on('close', function (code, signal) { + cb(code, signal, out.trim(), err.trim()) + }) +} + +t.test('finds node', function (t) { + which('node', function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 0) + t.equal(err, '') + t.match(out, /[\\\/]node(\.exe)?$/) + t.end() + }) +}) + +t.test('does not find flergyderp', function (t) { + which('flergyderp', function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 1) + t.equal(err, '') + t.match(out, '') + t.end() + }) +}) + +t.test('finds node and tap', function (t) { + which(['node', 'tap'], function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 0) + t.equal(err, '') + t.match(out.split(/\n/), [ + /[\\\/]node(\.exe)?$/, + /[\\\/]tap(\.cmd)?$/ + ]) + t.end() + }) +}) + +t.test('finds node and tap, but not flergyderp', function (t) { + which(['node', 'flergyderp', 'tap'], function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 1) + t.equal(err, '') + t.match(out.split(/\n/), [ + /[\\\/]node(\.exe)?$/, + /[\\\/]tap(\.cmd)?$/ + ]) + t.end() + }) +}) + +t.test('cli flags', function (t) { + var p = require('path').dirname(bin) + var cases = [ '-a', '-s', '-as', '-sa' ] + t.plan(cases.length) + cases.forEach(function (c) { + t.test(c, function (t) { + which(['which', c], p, function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 0) + t.equal(err, '') + if (/s/.test(c)) + t.equal(out, '', 'should be silent') + else if (/a/.test(c)) + t.ok(out.split(/\n/).length > 1, 'should have more than 1 result') + t.end() + }) + }) + }) +}) + +t.test('shows usage', function (t) { + which([], function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 1) + t.equal(err, 'usage: which [-as] program ...') + t.equal(out, '') + t.end() + }) +}) + +t.test('complains about unknown flag', function (t) { + which(['node', '-sax'], function (code, signal, out, err) { + t.equal(signal, null) + t.equal(code, 1) + t.equal(out, '') + t.equal(err, 'which: illegal option -- x\nusage: which [-as] program ...') + t.end() + }) +}) diff --git a/node_modules/which/which.js b/node_modules/which/which.js index 97579de8a..88bd52ba5 100644 --- a/node_modules/which/which.js +++ b/node_modules/which/which.js @@ -68,9 +68,15 @@ function which (cmd, opt, cb) { var info = getPathInfo(cmd, opt) var pathEnv = info.env var pathExt = info.ext + var found = [] ;(function F (i, l) { - if (i === l) return cb(new Error('not found: '+cmd)) + if (i === l) { + if (opt.all && found.length) + return cb(null, found) + else + return cb(new Error('not found: '+cmd)) + } var p = path.resolve(pathEnv[i], cmd) ;(function E (ii, ll) { if (ii === ll) return F(i + 1, l) @@ -79,7 +85,10 @@ function which (cmd, opt, cb) { if (!er && stat.isFile() && isExe(stat.mode, stat.uid, stat.gid)) { - return cb(null, p + ext) + if (opt.all) + found.push(p + ext) + else + return cb(null, p + ext) } return E(ii + 1, ll) }) @@ -93,6 +102,7 @@ function whichSync (cmd, opt) { var info = getPathInfo(cmd, opt) var pathEnv = info.env var pathExt = info.ext + var found = [] for (var i = 0, l = pathEnv.length; i < l; i ++) { var p = path.join(pathEnv[i], cmd) @@ -101,11 +111,18 @@ function whichSync (cmd, opt) { var stat try { stat = fs.statSync(cur) - if (stat.isFile() && isExe(stat.mode, stat.uid, stat.gid)) - return cur + if (stat.isFile() && isExe(stat.mode, stat.uid, stat.gid)) { + if (opt.all) + found.push(cur) + else + return cur + } } catch (ex) {} } } + if (opt.all && found.length) + return found + throw new Error('not found: '+cmd) } |