forked from prebuild/prebuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.js
45 lines (37 loc) · 1.13 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
var async = require('async')
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var tar = require('tar-stream')
var zlib = require('zlib')
function mode (octal) {
return parseInt(octal, 8)
}
function pack (filenames, tarPath, cb) {
mkdirp(path.dirname(tarPath), function () {
if (!Array.isArray(filenames)) {
filenames = [filenames]
}
var tarStream = tar.pack()
var ws = fs.createWriteStream(tarPath)
tarStream.pipe(zlib.createGzip({ level: 9 })).pipe(ws)
async.eachSeries(filenames, function processFile (filename, nextFile) {
fs.stat(filename, function (err, st) {
if (err) return nextFile(err)
var stream = tarStream.entry({
name: filename.replace(/\\/g, '/').replace(/:/g, '_'),
size: st.size,
mode: st.mode | mode('444') | mode('222'),
gid: st.gid,
uid: st.uid
})
fs.createReadStream(filename).pipe(stream).on('finish', nextFile)
stream.on('error', nextFile)
})
}, function allFilesProcessed (err) {
tarStream.finalize()
cb(err)
})
})
}
module.exports = pack