This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
/
bundle.js
113 lines (101 loc) · 3.75 KB
/
bundle.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module.exports = bundle
var npm = require("../npm")
, path = require("path")
, log = require("./utils/log")
, cache = require("./cache")
, readJson = require("./utils/read-json")
, mkdir = require("./utils/mkdir-p")
, fs = require("./utils/graceful-fs")
, conf = require("./utils/ini").configList
, rm = require("./utils/rm-rf")
, url = require("url")
, notAllowed = [ "adduser", "build", "bundle", "config", "init", "link"
, "owner", "publish", "restart", "start", "stop", "tag"
, "unpublish", "update-dependents", "view", "bn" ]
bundle.usage = "npm bundle\n"
+ "npm bundle destroy\n"
+ "npm bundle <cmd> <args>\n"
+ "(run in package dir)"
bundle.completion = function(args, index, cb) {
var getCompletions = require("./utils/completion/get-completions")
, subcmdList = npm.fullList.filter(function(c) {
return notAllowed.indexOf(c) === -1
}).concat(["destroy"])
, subcmd = args[0] || ""
if (subcmdList.indexOf(subcmd) !== -1) {
var subargs = args.slice(1)
npm.commands[npm.deref(subcmd)].completion(subargs, index - 1, cb)
} else if (index < 3) cb(null, getCompletions(subcmd, subcmdList))
}
function bundle (args, dir, cb_) {
if (typeof dir === "function") cb_ = dir, dir = process.cwd()
var location = path.join(dir, "node_modules")
, binRoot = path.join(dir, "node_modules", ".bin")
, pkg = dir
, cmd = args.shift()
function cb (er, data) {
conf.shift()
cb_(er, data)
}
mkdir(location, function (er) {
if (er) return cb(new Error(bundle.usage))
var c = { root : location
, binroot : binRoot
, manroot : null
, "must-install" : false
}
conf.unshift(c)
if (cmd && npm.commands[cmd]) {
cmd = npm.commands[cmd]
for (var i = 0, l = notAllowed.length; i < l; i ++) {
var na = notAllowed[i]
if (cmd === npm.commands[na]) {
if (na === "init") log.error(
"\nIt looks like you might be coming from ruby, where\n"+
"`bundle init` is a thing. The conceptual model is a bit\n"+
"different with npm. See `npm help developers`\n")
return cb(new Error(na + " command not allowed in bundle"))
}
}
if (cmd === npm.commands.ls) c.registry = null
}
if (cmd === "destroy") return rm(location, function (er) {
if (er) return cb(er)
log.info(location, "destroyed", cb)
})
if (cmd === npm.commands.install && args.length === 0) {
// `npm bundle install` --> npm bundle
// Don't try to install the cwd inside of ./node_modules
cmd = null
}
if (typeof cmd === "function") {
return cmd(args, cb)
}
// no command given, just install the local deps.
// just read the package.json from the directory to
// avoid adding the whole package to the cache
readJson(path.join(pkg, "package.json"), function (er, data) {
if (er) return log.er(cb, "Error reading "+pkg+"/package.json")(er)
install(data, location, cb)
})
})
}
function install (data, location, cb) {
if (typeof data.dependencies === 'undefined') {
return cb(new Error("Package has no dependencies"))
}
var depNames = Object.keys(data.dependencies)
, deps = depNames.map(function (d) {
var v = data.dependencies[d]
if (v === "*") v = ""
var u = url.parse(v)
if (u && u.protocol && u.host) {
return u.href
}
return v ? d + "@" + v : d
})
log.verbose(deps, "bundle deps")
npm.commands.install(deps, log.er(cb, "Some dependencies failed to bundle\n"
+ "Bundle them separately with\n"
+ " npm bundle install <pkg>"))
}