1'use strict' 2 3const pacote = require('pacote') 4const npa = require('npm-package-arg') 5const runScript = require('@npmcli/run-script') 6const path = require('path') 7const util = require('util') 8const Arborist = require('@npmcli/arborist') 9const writeFile = util.promisify(require('fs').writeFile) 10 11module.exports = pack 12async function pack (spec = 'file:.', opts = {}) { 13 // gets spec 14 spec = npa(spec) 15 16 const manifest = await pacote.manifest(spec, opts) 17 18 // Default to true if no log options passed, set to false if we're in silent 19 // mode 20 const banner = !opts.silent 21 22 const stdio = opts.foregroundScripts ? 'inherit' : 'pipe' 23 24 if (spec.type === 'directory' && !opts.ignoreScripts) { 25 // prepack 26 await runScript({ 27 ...opts, 28 event: 'prepack', 29 path: spec.fetchSpec, 30 stdio, 31 pkg: manifest, 32 banner, 33 }) 34 } 35 36 // packs tarball 37 const tarball = await pacote.tarball(manifest._resolved, { 38 ...opts, 39 Arborist, 40 integrity: manifest._integrity, 41 }) 42 43 // check for explicit `false` so the default behavior is to skip writing to disk 44 if (opts.dryRun === false) { 45 const filename = `${manifest.name}-${manifest.version}.tgz` 46 .replace(/^@/, '').replace(/\//, '-') 47 const destination = path.resolve(opts.packDestination, filename) 48 await writeFile(destination, tarball) 49 } 50 51 if (spec.type === 'directory' && !opts.ignoreScripts) { 52 // postpack 53 await runScript({ 54 ...opts, 55 event: 'postpack', 56 path: spec.fetchSpec, 57 stdio, 58 pkg: manifest, 59 banner, 60 env: { 61 npm_package_from: tarball.from, 62 npm_package_resolved: tarball.resolved, 63 npm_package_integrity: tarball.integrity, 64 }, 65 }) 66 } 67 68 return tarball 69} 70