• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5const getStream = require('get-stream')
6const tar = require('tar-stream')
7const zlib = require('zlib')
8
9module.exports = makeTarball
10function makeTarball (files, opts) {
11  opts = opts || {}
12  const pack = tar.pack()
13  Object.keys(files).forEach(function (filename) {
14    const entry = files[filename]
15    pack.entry({
16      name: (opts.noPrefix ? '' : 'package/') + filename,
17      type: entry.type,
18      size: entry.size,
19      mode: entry.mode,
20      mtime: entry.mtime || new Date(0),
21      linkname: entry.linkname,
22      uid: entry.uid,
23      gid: entry.gid,
24      uname: entry.uname,
25      gname: entry.gname
26    }, typeof files[filename] === 'string'
27      ? files[filename]
28      : files[filename].data)
29  })
30  pack.finalize()
31  return BB.try(() => {
32    if (opts.stream && opts.gzip) {
33      const gz = zlib.createGzip()
34      pack.on('error', err => gz.emit('error', err)).pipe(gz)
35    } else if (opts.stream) {
36      return pack
37    } else {
38      return getStream.buffer(pack).then(ret => {
39        if (opts.gzip) {
40          return BB.fromNode(cb => zlib.gzip(ret, cb))
41        } else {
42          return ret
43        }
44      })
45    }
46  })
47}
48