• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5const getStream = require('get-stream')
6const pipeline = require('mississippi').pipeline
7const tar = require('tar-stream')
8const zlib = require('zlib')
9
10module.exports = makeTarball
11function makeTarball (files, opts) {
12  opts = opts || {}
13  const pack = tar.pack()
14  Object.keys(files).forEach(function (filename) {
15    const entry = files[filename]
16    pack.entry({
17      name: (opts.noPrefix ? '' : 'package/') + filename,
18      type: entry.type,
19      size: entry.size,
20      mode: entry.mode,
21      mtime: entry.mtime || new Date(0),
22      linkname: entry.linkname,
23      uid: entry.uid,
24      gid: entry.gid,
25      uname: entry.uname,
26      gname: entry.gname
27    }, typeof files[filename] === 'string'
28      ? files[filename]
29      : files[filename].data)
30  })
31  pack.finalize()
32  return BB.try(() => {
33    if (opts.stream && opts.gzip) {
34      return pipeline(pack, zlib.createGzip())
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