forked from ritthisakdi/npm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.js
72 lines (59 loc) · 1.74 KB
/
pack.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
// npm pack <pkg>
// Packs the specified package into a .tgz file, which can then
// be installed.
module.exports = pack
var install = require('./install.js')
var cache = require('./cache.js')
var fs = require('graceful-fs')
var chain = require('slide').chain
var path = require('path')
var cwd = process.cwd()
var writeStream = require('fs-write-stream-atomic')
var cachedPackageRoot = require('./cache/cached-package-root.js')
pack.usage = 'npm pack [[<@scope>/]<pkg>...]'
// if it can be installed, it can be packed.
pack.completion = install.completion
function pack (args, silent, cb) {
if (typeof cb !== 'function') {
cb = silent
silent = false
}
if (args.length === 0) args = ['.']
chain(
args.map(function (arg) { return function (cb) { pack_(arg, cb) } }),
function (er, files) {
if (er || silent) return cb(er, files)
printFiles(files, cb)
}
)
}
function printFiles (files, cb) {
files = files.map(function (file) {
return path.relative(cwd, file)
})
console.log(files.join('\n'))
cb()
}
// add to cache, then cp to the cwd
function pack_ (pkg, cb) {
cache.add(pkg, null, null, false, function (er, data) {
if (er) return cb(er)
// scoped packages get special treatment
var name = data.name
if (name[0] === '@') name = name.substr(1).replace(/\//g, '-')
var fname = name + '-' + data.version + '.tgz'
var cached = path.join(cachedPackageRoot(data), 'package.tgz')
var from = fs.createReadStream(cached)
var to = writeStream(fname)
var errState = null
from.on('error', cb_)
to.on('error', cb_)
to.on('close', cb_)
from.pipe(to)
function cb_ (er) {
if (errState) return
if (er) return cb(errState = er)
cb(null, fname)
}
})
}