1'use strict' 2 3const BB = require('bluebird') 4 5const cacache = require('cacache') 6const cacheKey = require('./cache-key') 7const optCheck = require('./opt-check') 8const packlist = require('npm-packlist') 9const pipe = BB.promisify(require('mississippi').pipe) 10const tar = require('tar') 11 12module.exports = packDir 13function packDir (manifest, label, dir, target, opts) { 14 opts = optCheck(opts) 15 16 const packer = opts.dirPacker 17 ? BB.resolve(opts.dirPacker(manifest, dir)) 18 : mkPacker(dir) 19 20 if (!opts.cache) { 21 return packer.then(packer => pipe(packer, target)) 22 } else { 23 const cacher = cacache.put.stream( 24 opts.cache, cacheKey('packed-dir', label), opts 25 ).on('integrity', i => { 26 target.emit('integrity', i) 27 }) 28 return packer.then(packer => BB.all([ 29 pipe(packer, cacher), 30 pipe(packer, target) 31 ])) 32 } 33} 34 35function mkPacker (dir) { 36 return packlist({ path: dir }).then(files => { 37 return tar.c({ 38 cwd: dir, 39 gzip: true, 40 portable: true, 41 prefix: 'package/' 42 }, files) 43 }) 44} 45