diff options
author | isaacs <i@izs.me> | 2010-08-26 00:27:30 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-08-26 05:01:24 +0400 |
commit | 86bca612e823a98b8caf4c9be02fbc6a21cc0ad9 (patch) | |
tree | d27c6e733763a1145be3f4c675ed807f7730421a | |
parent | 7783953fb39ea93aeed0664dd69b691fd062b403 (diff) |
A better bundle command.
-rw-r--r-- | lib/bundle.js | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/lib/bundle.js b/lib/bundle.js index 5dd6d0b59..2d8511883 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -5,18 +5,59 @@ var npm = require("../npm") , fs = require("fs") , path = require("path") , log = require("./utils/log") + , cache = require("./cache") + , mkdir = require("./utils/mkdir-p") + , fs = require("./utils/graceful-fs") + function bundle (args, cb) { log(args, "bundle") - var location = args.pop() - , usage = "Usage: npm bundle [<pkg> [<pkg> ...]] <location>" + var location = args.shift() + , pkg = args.shift() || "." + , usage = "Usage: npm bundle <location> [<pkg>]\n" + + "(<pkg> defaults to '.')" if (!location) return cb(new Error(usage)) if (location.charAt(0) !== "/") { location = path.join(process.cwd(), location) } - fs.stat(location, function (er, s) { - if (er || !s.isDirectory()) return cb(new Error(usage)) + mkdir(location, function (er) { + if (er) return cb(new Error(usage)) npm.config.set("root", location) npm.config.set("binroot", null) - npm.commands.install(args, cb) + npm.config.set("manroot", null) + // AND for my NEXT trick... + // add the pkg to the cache, so that we get its data whether it's + // a folder or name or whatever, and then delete the cache after. + cache.add(pkg, function (er, data) { + if (er) return log.er(cb, "Error adding "+pkg+" to bundle cache")(er) + var depNames = Object.keys(data.dependencies) + , deps = depNames.map(function (d) { + var v = data.dependencies[d] + if (v === "*") v = "" + return d+"@"+v + }) + log.verbose(deps, "bundle deps") + npm.commands.install(deps, function (er) { + if (er) return cleanup(er, cb) + writeBundleShim(location, depNames, function (er) { + cleanup(er, cb) + }) + }) + }) + }) +} + +function cleanup (er_, cb) { + cache.clean(function (er) { + if (er) log.error(er, "Error cleaning bundle cache") + return cb(er_) }) } + +function writeBundleShim (location, depNames, cb) { + var data = "// generated by npm, please don't touch!\n" + + "require.paths.unshift(__dirname)\n" + + depNames.map(JSON.stringify).map(function (d) { + return "exports["+d+"] = require("+d+")\n" + }) + fs.writeFile(path.join(location, "index.js"), data, cb) +} |