UNPKG

1.63 kBJavaScriptView Raw
1// npm pack <pkg>
2// Packs the specified package into a .tgz file, which can then
3// be installed.
4
5module.exports = pack
6
7var npm = require("./npm.js")
8 , install = require("./install.js")
9 , cache = require("./cache.js")
10 , fs = require("graceful-fs")
11 , chain = require("slide").chain
12 , path = require("path")
13 , cwd = process.cwd()
14
15pack.usage = "npm pack <pkg>"
16
17// if it can be installed, it can be packed.
18pack.completion = install.completion
19
20function pack (args, silent, cb) {
21 if (typeof cb !== "function") cb = silent, silent = false
22
23 if (args.length === 0) args = ["."]
24
25 chain(args.map(function (arg) { return function (cb) {
26 pack_(arg, cb)
27 }}), function (er, files) {
28 if (er || silent) return cb(er, files)
29 printFiles(files, cb)
30 })
31}
32
33function printFiles (files, cb) {
34 files = files.map(function (file) {
35 return path.relative(cwd, file)
36 })
37 console.log(files.join("\n"))
38 cb()
39}
40
41// add to cache, then cp to the cwd
42function pack_ (pkg, cb) {
43 cache.add(pkg, function (er, data) {
44 if (er) return cb(er)
45 var fname = path.resolve(data._id.replace(/@/g, "-") + ".tgz")
46 , cached = path.resolve( npm.cache
47 , data.name
48 , data.version
49 , "package.tgz" )
50 , from = fs.createReadStream(cached)
51 , to = fs.createWriteStream(fname)
52 , errState = null
53
54 from.on("error", cb_)
55 to.on("error", cb_)
56 to.on("close", cb_)
57 from.pipe(to)
58
59 function cb_ (er) {
60 if (errState) return
61 if (er) return cb(errState = er)
62 cb(null, fname)
63 }
64 })
65}