2 // Packs the specified package into a .tgz file, which can then
7 var 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")
14 , writeStreamAtomic = require('fs-write-stream-atomic')
15 , cachedPackageRoot = require("./cache/cached-package-root.js")
17 pack.usage = "npm pack <pkg>"
19 // if it can be installed, it can be packed.
20 pack.completion = install.completion
22 function pack (args, silent, cb) {
23 if (typeof cb !== "function") cb = silent, silent = false
25 if (args.length === 0) args = ["."]
27 chain(args.map(function (arg) { return function (cb) {
29 }}), function (er, files) {
30 if (er || silent) return cb(er, files)
35 function printFiles (files, cb) {
36 files = files.map(function (file) {
37 return path.relative(cwd, file)
39 console.log(files.join("\n"))
43 // add to cache, then cp to the cwd
44 function pack_ (pkg, cb) {
45 cache.add(pkg, null, null, false, function (er, data) {
48 // scoped packages get special treatment
50 if (name[0] === "@") name = name.substr(1).replace(/\//g, "-")
51 var fname = name + "-" + data.version + ".tgz"
53 var cached = path.join(cachedPackageRoot(data), "package.tgz")
54 , from = fs.createReadStream(cached)
55 , to = writeStreamAtomic(fname)
65 if (er) return cb(errState = er)