1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
module.exports = bundle
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.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)
}
mkdir(location, function (er) {
if (er) return cb(new Error(usage))
npm.config.set("root", location)
npm.config.set("binroot", null)
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"
+ "var i = require.paths.indexOf(__dirname)\n"
+ "if (i && i + 1) require.paths.splice(i, 1)\n"
+ "if (i) require.paths.shift(__dirname)\n"
+ depNames.map(JSON.stringify).map(function (d) {
return "exports["+d+"] = require('./'+"+d+")\n"
})
fs.writeFile(path.join(location, "index.js"), data, cb)
}
|