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 | exports = module.exports = cache
|
58 |
|
59 | cache.unpack = unpack
|
60 | cache.clean = clean
|
61 | cache.read = read
|
62 |
|
63 | var npm = require("./npm.js")
|
64 | , fs = require("graceful-fs")
|
65 | , writeFileAtomic = require("write-file-atomic")
|
66 | , assert = require("assert")
|
67 | , rm = require("./utils/gently-rm.js")
|
68 | , readJson = require("read-package-json")
|
69 | , log = require("npmlog")
|
70 | , path = require("path")
|
71 | , asyncMap = require("slide").asyncMap
|
72 | , tar = require("./utils/tar.js")
|
73 | , fileCompletion = require("./utils/completion/file-completion.js")
|
74 | , deprCheck = require("./utils/depr-check.js")
|
75 | , addNamed = require("./cache/add-named.js")
|
76 | , addLocal = require("./cache/add-local.js")
|
77 | , addRemoteTarball = require("./cache/add-remote-tarball.js")
|
78 | , addRemoteGit = require("./cache/add-remote-git.js")
|
79 | , maybeGithub = require("./cache/maybe-github.js")
|
80 | , inflight = require("inflight")
|
81 | , realizePackageSpecifier = require("realize-package-specifier")
|
82 | , npa = require("npm-package-arg")
|
83 | , getStat = require("./cache/get-stat.js")
|
84 | , cachedPackageRoot = require("./cache/cached-package-root.js")
|
85 |
|
86 | cache.usage = "npm cache add <tarball file>"
|
87 | + "\nnpm cache add <folder>"
|
88 | + "\nnpm cache add <tarball url>"
|
89 | + "\nnpm cache add <git url>"
|
90 | + "\nnpm cache add <name>@<version>"
|
91 | + "\nnpm cache ls [<path>]"
|
92 | + "\nnpm cache clean [<pkg>[@<version>]]"
|
93 |
|
94 | cache.completion = function (opts, cb) {
|
95 |
|
96 | var argv = opts.conf.argv.remain
|
97 | if (argv.length === 2) {
|
98 | return cb(null, ["add", "ls", "clean"])
|
99 | }
|
100 |
|
101 | switch (argv[2]) {
|
102 | case "clean":
|
103 | case "ls":
|
104 |
|
105 |
|
106 |
|
107 | var p = path.dirname(opts.partialWords.slice(3).join("/"))
|
108 | if (p === ".") p = ""
|
109 | return ls_(p, 2, cb)
|
110 | case "add":
|
111 |
|
112 | return npm.commands.install.completion(opts, cb)
|
113 | }
|
114 | }
|
115 |
|
116 | function cache (args, cb) {
|
117 | var cmd = args.shift()
|
118 | switch (cmd) {
|
119 | case "rm": case "clear": case "clean": return clean(args, cb)
|
120 | case "list": case "sl": case "ls": return ls(args, cb)
|
121 | case "add": return add(args, npm.prefix, cb)
|
122 | default: return cb("Usage: "+cache.usage)
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function read (name, ver, forceBypass, cb) {
|
130 | assert(typeof name === "string", "must include name of module to install")
|
131 | assert(typeof cb === "function", "must include callback")
|
132 |
|
133 | if (forceBypass === undefined || forceBypass === null) forceBypass = true
|
134 |
|
135 | var root = cachedPackageRoot({name : name, version : ver})
|
136 | function c (er, data) {
|
137 | log.silly("cache", "addNamed cb", name+"@"+ver)
|
138 | if (er) log.verbose("cache", "addNamed error for", name+"@"+ver, er)
|
139 |
|
140 | if (data) deprCheck(data)
|
141 |
|
142 | return cb(er, data)
|
143 | }
|
144 |
|
145 | if (forceBypass && npm.config.get("force")) {
|
146 | log.verbose("using force", "skipping cache")
|
147 | return addNamed(name, ver, null, c)
|
148 | }
|
149 |
|
150 | readJson(path.join(root, "package", "package.json"), function (er, data) {
|
151 | if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
|
152 |
|
153 | if (data) {
|
154 | if (!data.name) return cb(new Error("No name provided"))
|
155 | if (!data.version) return cb(new Error("No version provided"))
|
156 | }
|
157 |
|
158 | if (er) return addNamed(name, ver, null, c)
|
159 | else c(er, data)
|
160 | })
|
161 | }
|
162 |
|
163 | function normalize (args) {
|
164 | var normalized = ""
|
165 | if (args.length > 0) {
|
166 | var a = npa(args[0])
|
167 | if (a.name) normalized = a.name
|
168 | if (a.rawSpec) normalized = [normalized, a.rawSpec].join("/")
|
169 | if (args.length > 1) normalized = [normalized].concat(args.slice(1)).join("/")
|
170 | }
|
171 |
|
172 | if (normalized.substr(-1) === "/") {
|
173 | normalized = normalized.substr(0, normalized.length - 1)
|
174 | }
|
175 | log.silly("ls", "normalized", normalized)
|
176 |
|
177 | return normalized
|
178 | }
|
179 |
|
180 |
|
181 | function ls (args, cb) {
|
182 | var prefix = npm.config.get("cache")
|
183 | if (prefix.indexOf(process.env.HOME) === 0) {
|
184 | prefix = "~" + prefix.substr(process.env.HOME.length)
|
185 | }
|
186 | ls_(normalize(args), npm.config.get("depth"), function (er, files) {
|
187 | console.log(files.map(function (f) {
|
188 | return path.join(prefix, f)
|
189 | }).join("\n").trim())
|
190 | cb(er, files)
|
191 | })
|
192 | }
|
193 |
|
194 |
|
195 | function ls_ (req, depth, cb) {
|
196 | return fileCompletion(npm.cache, req, depth, cb)
|
197 | }
|
198 |
|
199 |
|
200 | function clean (args, cb) {
|
201 | assert(typeof cb === "function", "must include callback")
|
202 |
|
203 | if (!args) args = []
|
204 |
|
205 | var f = path.join(npm.cache, path.normalize(normalize(args)))
|
206 | if (f === npm.cache) {
|
207 | fs.readdir(npm.cache, function (er, files) {
|
208 | if (er) return cb()
|
209 | asyncMap( files.filter(function (f) {
|
210 | return npm.config.get("force") || f !== "-"
|
211 | }).map(function (f) {
|
212 | return path.join(npm.cache, f)
|
213 | })
|
214 | , rm, cb )
|
215 | })
|
216 | } else rm(path.join(npm.cache, path.normalize(normalize(args))), cb)
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | cache.add = function (pkg, ver, where, scrub, cb) {
|
224 | assert(typeof pkg === "string", "must include name of package to install")
|
225 | assert(typeof cb === "function", "must include callback")
|
226 |
|
227 | if (scrub) {
|
228 | return clean([], function (er) {
|
229 | if (er) return cb(er)
|
230 | add([pkg, ver], where, cb)
|
231 | })
|
232 | }
|
233 | return add([pkg, ver], where, cb)
|
234 | }
|
235 |
|
236 |
|
237 | var adding = 0
|
238 | function add (args, where, cb) {
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | var usage = "Usage:\n"
|
250 | + " npm cache add <tarball-url>\n"
|
251 | + " npm cache add <pkg>@<ver>\n"
|
252 | + " npm cache add <tarball>\n"
|
253 | + " npm cache add <folder>\n"
|
254 | , spec
|
255 |
|
256 | log.silly("cache add", "args", args)
|
257 |
|
258 | if (args[1] === undefined) args[1] = null
|
259 |
|
260 |
|
261 | if (args[1] !== null) {
|
262 | spec = args[0]+"@"+args[1]
|
263 | } else if (args.length === 2) {
|
264 | spec = args[0]
|
265 | }
|
266 |
|
267 | log.verbose("cache add", "spec", spec)
|
268 |
|
269 | if (!spec) return cb(usage)
|
270 |
|
271 | if (adding <= 0) {
|
272 | npm.spinner.start()
|
273 | }
|
274 | adding++
|
275 | cb = afterAdd(cb)
|
276 |
|
277 | realizePackageSpecifier(spec, where, function (err, p) {
|
278 | if (err) return cb(err)
|
279 |
|
280 | log.silly("cache add", "parsed spec", p)
|
281 |
|
282 | switch (p.type) {
|
283 | case "local":
|
284 | case "directory":
|
285 | addLocal(p, null, cb)
|
286 | break
|
287 | case "remote":
|
288 | addRemoteTarball(p.spec, {name : p.name}, null, cb)
|
289 | break
|
290 | case "git":
|
291 | addRemoteGit(p.spec, false, cb)
|
292 | break
|
293 | case "github":
|
294 | maybeGithub(p.spec, cb)
|
295 | break
|
296 | default:
|
297 | if (p.name) return addNamed(p.name, p.spec, null, cb)
|
298 |
|
299 | cb(new Error("couldn't figure out how to install " + spec))
|
300 | }
|
301 | })
|
302 | }
|
303 |
|
304 | function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) {
|
305 | if (typeof cb !== "function") cb = gid, gid = null
|
306 | if (typeof cb !== "function") cb = uid, uid = null
|
307 | if (typeof cb !== "function") cb = fMode, fMode = null
|
308 | if (typeof cb !== "function") cb = dMode, dMode = null
|
309 |
|
310 | read(pkg, ver, false, function (er) {
|
311 | if (er) {
|
312 | log.error("unpack", "Could not read data for %s", pkg + "@" + ver)
|
313 | return cb(er)
|
314 | }
|
315 | npm.commands.unbuild([unpackTarget], true, function (er) {
|
316 | if (er) return cb(er)
|
317 | tar.unpack( path.join(cachedPackageRoot({name : pkg, version : ver}), "package.tgz")
|
318 | , unpackTarget
|
319 | , dMode, fMode
|
320 | , uid, gid
|
321 | , cb )
|
322 | })
|
323 | })
|
324 | }
|
325 |
|
326 | function afterAdd (cb) { return function (er, data) {
|
327 | adding--
|
328 | if (adding <= 0) npm.spinner.stop()
|
329 |
|
330 | if (er || !data || !data.name || !data.version) return cb(er, data)
|
331 | log.silly("cache", "afterAdd", data.name+"@"+data.version)
|
332 |
|
333 |
|
334 |
|
335 | var pj = path.join(cachedPackageRoot(data), "package", "package.json")
|
336 |
|
337 | var done = inflight(pj, cb)
|
338 | if (!done) return log.verbose("afterAdd", pj, "already in flight; not writing")
|
339 | log.verbose("afterAdd", pj, "not in flight; writing")
|
340 |
|
341 | getStat(function (er, cs) {
|
342 | if (er) return done(er)
|
343 | writeFileAtomic(pj, JSON.stringify(data), {chown : cs}, function (er) {
|
344 | if (!er) log.verbose("afterAdd", pj, "written")
|
345 | return done(er, data)
|
346 | })
|
347 | })
|
348 | }}
|