Skip to content
Browse files

#1213: add "checkdeps" command

  • Loading branch information...
1 parent ead1c89 commit 3a1194f7c4e16ffe63eeb1bb3e9642140784268f @thejh committed Aug 17, 2011
Showing with 42 additions and 0 deletions.
  1. +41 −0 lib/checkdeps.js
  2. +1 −0 npm.js
View
41 lib/checkdeps.js
@@ -0,0 +1,41 @@
+
+// test whether everything needed is installed
+
+module.exports = exports = checkdeps
+
+var npm = require("../npm.js")
+ , output = require("./utils/output.js")
+ , log = require("./utils/log.js")
+ , ls = npm.commands.ls
+
+checkdeps.usage = "npm checkdeps"
+
+// returns whether something is NOT ok
+function _checkdeps(data) {
+ if (!data.dependencies) return true
+ return Object.keys(data.dependencies).some(function (name) {
+ var value = data.dependencies[name]
+ if (typeof value === "string")
+ return true
+ return _checkdeps(value)
+ })
+}
+
+function checkdeps(args, silent, cb) {
+ if (typeof cb !== "function") cb = silent, silent = false
+
+ if (args.length) {
+ log.warn("checkdeps doesn't take positional args")
+ }
+
+ ls([], true, function(err, data){
+ if (err) return cb(err, null)
+ var result = !_checkdeps(data)
+ if (silent) return cb(null, result)
+ if (!silent) {
+ var resultstr = 'status:'+(result?'ok':
+ 'not ok, use "npm install" to fix it or "npm ls" to see whats going on')
+ output.write(resultstr, function (er) { cb(er, result) })
+ }
+ })
+}
View
1 npm.js
@@ -105,6 +105,7 @@ var commandCache = {}
, "help"
, "help-search"
, "ls"
+ , "checkdeps"
, "search"
, "view"
, "init"

4 comments on commit 3a1194f

@isaacs
isaacs commented on 3a1194f Aug 17, 2011

This is pretty good.

  1. I like to avoid stuff like "returns true if not ok". It'd be better if _checkdeps return value was reversed, or if the function was renamed to somethingMissing or hasProblem something, so that "true" means "yes".
  2. It'd be better if the data returned was somewhat useful. Maybe return which package is missing, and where. If you did that, you could even spit out the appropriate install commands for the user to copy and paste :)
  3. Note that something could be missing beyond the first level. If the item isn't a string, then you need to recursively check its deps as well.
@thejh
Owner
thejh commented on 3a1194f Aug 17, 2011

@isaacs, about number 3: Have a look at line 20, shouldn't that work?

@isaacs
isaacs commented on 3a1194f Aug 17, 2011

Oh, right, I missed that.

Can't you end up with cycles? I think the readInstalled data structure may set a pointer to an object higher up the tree if that's the thing that satisfies the dependency.

@thejh
Owner
thejh commented on 3a1194f Aug 17, 2011

Could well be... :(

Please sign in to comment.
Something went wrong with that request. Please try again.