Skip to content

Commit

Permalink
use abstracted completion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 21, 2011
1 parent e947240 commit 660caf6
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 76 deletions.
32 changes: 1 addition & 31 deletions lib/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,7 @@
module.exports = edit
edit.usage = "npm edit <pkg>"

edit.completion = function (opts, cb) {
var conf = opts.conf
, args = conf.argv.remain
if (args.length > 3) return cb()
var local
, global
, localDir = npm.dir
, globalDir = path.join(npm.config.get("prefix"), "node_modules")
if (npm.config.get("global")) local = [], next()
else fs.readdir(localDir, function (er, pkgs) {
local = (pkgs || []).filter(function (p) {
return p.charAt(0) !== "."
})
next()
})
fs.readdir(globalDir, function (er, pkgs) {
global = (pkgs || []).filter(function (p) {
return p.charAt(0) !== "."
})
next()
})
function next () {
if (!local || !global) return
if (!npm.config.get("global")) {
global = global.map(function (g) {
return [g, "-g"]
})
}
return cb(null, local.concat(global))
}
}
edit.completion = require("./utils/completion/installed-shallow")

var npm = require("../npm")
, exec = require("./utils/exec")
Expand Down
7 changes: 2 additions & 5 deletions lib/explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// open a subshell to the package folder.

module.exports = explore
explore.usage = "npm explore <pkg>[@<version>] [<cmd>]"
explore.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, false, cb)
}
explore.usage = "npm explore <pkg> [<cmd>]"
explore.completion = require("./utils/completion/installed-shallow")

var npm = require("../npm")
, exec = require("./utils/exec")
Expand Down
37 changes: 1 addition & 36 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,8 @@ module.exports = outdated

outdated.usage = "npm outdated [<pkg> [<pkg> ...]]"

outdated.completion = function (opts, cb) {
var local
, global
, readInstalled = require("./utils/read-installed")
readInstalled(npm.prefix, function (er, data) {
local = data || {}
next()
})
readInstalled(npm.config.get("prefix"), function (er, data) {
global = data || {}
next()
})

function getNames_ (d, n) {
if (d.realName && n) {
if (n[d.realName]) return n
n[d.realName] = true
}
if (!n) n = {}
Object.keys(d.dependencies || {}).forEach(function (dep) {
getNames_(d.dependencies[dep], n)
})
return n
}
function getNames (d) {
return Object.keys(getNames_(d))
}

function next () {
if (!local || !global) return
var names = getNames(local).concat(getNames(global).map(function (g) {
return [g, "-g"]
}))
return cb(null, names)
}
outdated.completion = require("./utils/completion/installed-deep")

}

var readInstalled = require("./utils/read-installed")
, path = require("path")
Expand Down
2 changes: 2 additions & 0 deletions lib/prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ prune.usage = "npm prune"
var readInstalled = require("./utils/read-installed")
, npm = require("../npm")

prune.completion = require("./utils/completion/installed-deep")

function prune (args, cb) {
readInstalled(npm.prefix, function (er, data) {
if (er) return cb(er)
Expand Down
5 changes: 1 addition & 4 deletions lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ var readInstalled = require("./utils/read-installed")

rebuild.usage = "npm rebuild [<name>[@<version>] [name[@<version>] ...]]"

rebuild.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}
rebuild.completion = require("./utils/completion/installed-deep")

function rebuild (args, cb) {
readInstalled(npm.prefix, function (er, data) {
Expand Down

0 comments on commit 660caf6

Please sign in to comment.