summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/lib/util/pack-dir.js
blob: 157a9a82f8af8344cae2dcdce3be09fddeabf6e6 (plain)
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
'use strict'

const BB = require('bluebird')

const cacache = require('cacache')
const cacheKey = require('./cache-key')
const optCheck = require('./opt-check')
const packlist = require('npm-packlist')
const pipe = BB.promisify(require('mississippi').pipe)
const tar = require('tar')

module.exports = packDir
function packDir (manifest, label, dir, target, opts) {
  opts = optCheck(opts)

  const packer = opts.dirPacker
    ? BB.resolve(opts.dirPacker(manifest, dir))
    : mkPacker(dir)

  if (!opts.cache) {
    return packer.then(packer => pipe(packer, target))
  } else {
    const cacher = cacache.put.stream(
      opts.cache, cacheKey('packed-dir', label), opts
    ).on('integrity', i => {
      target.emit('integrity', i)
    })
    return packer.then(packer => BB.all([
      pipe(packer, cacher),
      pipe(packer, target)
    ]))
  }
}

function mkPacker (dir) {
  return packlist({ path: dir }).then(files => {
    return tar.c({
      cwd: dir,
      gzip: true,
      portable: true,
      prefix: 'package/'
    }, files)
  })
}