diff options
author | Julien Gilli <julien.gilli@joyent.com> | 2015-03-16 15:55:17 -0700 |
---|---|---|
committer | Julien Gilli <julien.gilli@joyent.com> | 2015-03-16 15:55:17 -0700 |
commit | ae58fc407f916b2abb164453a5b09273c543dbc3 (patch) | |
tree | 4a16fe73f996a54f34cfb553b2995d16c1375b7f /deps/npm/lib/install.js | |
parent | 2b64132101f179c30957e3c5f16fc47a8ec942e1 (diff) | |
parent | eb2764a9452baa7cba2d98dc34fa00fc776b0a12 (diff) | |
download | node-merge-review.tar.gz |
Merge remote-tracking branch 'upstream/v0.12'merge-review
Diffstat (limited to 'deps/npm/lib/install.js')
-rw-r--r-- | deps/npm/lib/install.js | 70 |
1 files changed, 45 insertions, 25 deletions
diff --git a/deps/npm/lib/install.js b/deps/npm/lib/install.js index 7fa9058f3..73580e5af 100644 --- a/deps/npm/lib/install.js +++ b/deps/npm/lib/install.js @@ -32,38 +32,58 @@ install.completion = function (opts, cb) { // install can complete to a folder with a package.json, or any package. // if it has a slash, then it's gotta be a folder // if it starts with https?://, then just give up, because it's a url - // for now, not yet implemented. - mapToRegistry("-/short", npm.config, function (er, uri, auth) { - if (er) return cb(er) - - var options = { auth : auth } - npm.registry.get(uri, options, function (er, pkgs) { - if (er) return cb() - if (!opts.partialWord) return cb(null, pkgs) + if (/^https?:\/\//.test(opts.partialWord)) { + // do not complete to URLs + return cb(null, []) + } - var name = npa(opts.partialWord).name - pkgs = pkgs.filter(function (p) { - return p.indexOf(name) === 0 + if (/\//.test(opts.partialWord)) { + // Complete fully to folder if there is exactly one match and it + // is a folder containing a package.json file. If that is not the + // case we return 0 matches, which will trigger the default bash + // complete. + var lastSlashIdx = opts.partialWord.lastIndexOf("/") + var partialName = opts.partialWord.slice(lastSlashIdx + 1) + var partialPath = opts.partialWord.slice(0, lastSlashIdx) + if (partialPath === "") partialPath = "/" + + function annotatePackageDirMatch (sibling, cb) { + var fullPath = path.join(partialPath, sibling) + if (sibling.slice(0, partialName.length) !== partialName) { + return cb(null, null) // not name match + } + fs.readdir(fullPath, function (err, contents) { + if (err) return cb(null, { isPackage: false }) + + cb( + null, + { + fullPath: fullPath, + isPackage: contents.indexOf("package.json") !== -1 + } + ) }) + } - if (pkgs.length !== 1 && opts.partialWord === name) { - return cb(null, pkgs) - } + return fs.readdir(partialPath, function (err, siblings) { + if (err) return cb(null, []) // invalid dir: no matching - mapToRegistry(pkgs[0], npm.config, function (er, uri) { - if (er) return cb(er) + asyncMap(siblings, annotatePackageDirMatch, function (err, matches) { + if (err) return cb(err) - npm.registry.get(uri, options, function (er, d) { - if (er) return cb() - return cb(null, Object.keys(d["dist-tags"] || {}) - .concat(Object.keys(d.versions || {})) - .map(function (t) { - return pkgs[0] + "@" + t - })) - }) + var cleaned = matches.filter(function (x) { return x !== null }) + if (cleaned.length !== 1) return cb(null, []) + if (!cleaned[0].isPackage) return cb(null, []) + + // Success - only one match and it is a package dir + return cb(null, [cleaned[0].fullPath]) }) }) - }) + } + + // FIXME: there used to be registry completion here, but it stopped making + // sense somewhere around 50,000 packages on the registry + cb() } var npm = require("./npm.js") |