2020-10-02 17:52:19 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const pacote = require('pacote')
|
|
|
|
const npa = require('npm-package-arg')
|
|
|
|
const runScript = require('@npmcli/run-script')
|
2024-05-30 04:21:05 -07:00
|
|
|
const path = require('node:path')
|
2022-12-06 22:18:33 -05:00
|
|
|
const Arborist = require('@npmcli/arborist')
|
2024-05-30 04:21:05 -07:00
|
|
|
const { writeFile } = require('node:fs/promises')
|
2020-10-02 17:52:19 -04:00
|
|
|
|
|
|
|
module.exports = pack
|
|
|
|
async function pack (spec = 'file:.', opts = {}) {
|
|
|
|
// gets spec
|
|
|
|
spec = npa(spec)
|
|
|
|
|
2025-01-10 08:20:27 -08:00
|
|
|
const manifest = await pacote.manifest(spec, { ...opts, Arborist })
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'
|
|
|
|
|
|
|
|
if (spec.type === 'directory' && !opts.ignoreScripts) {
|
2020-10-02 17:52:19 -04:00
|
|
|
// prepack
|
|
|
|
await runScript({
|
|
|
|
...opts,
|
|
|
|
event: 'prepack',
|
|
|
|
path: spec.fetchSpec,
|
2022-12-06 22:18:33 -05:00
|
|
|
stdio,
|
2021-02-23 17:29:16 -05:00
|
|
|
pkg: manifest,
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// packs tarball
|
|
|
|
const tarball = await pacote.tarball(manifest._resolved, {
|
|
|
|
...opts,
|
2022-12-06 22:18:33 -05:00
|
|
|
Arborist,
|
2022-01-14 19:42:48 +02:00
|
|
|
integrity: manifest._integrity,
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
|
2022-02-11 11:51:11 +02:00
|
|
|
// check for explicit `false` so the default behavior is to skip writing to disk
|
|
|
|
if (opts.dryRun === false) {
|
|
|
|
const filename = `${manifest.name}-${manifest.version}.tgz`
|
|
|
|
.replace(/^@/, '').replace(/\//, '-')
|
|
|
|
const destination = path.resolve(opts.packDestination, filename)
|
|
|
|
await writeFile(destination, tarball)
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
if (spec.type === 'directory' && !opts.ignoreScripts) {
|
2020-10-02 17:52:19 -04:00
|
|
|
// postpack
|
|
|
|
await runScript({
|
|
|
|
...opts,
|
|
|
|
event: 'postpack',
|
|
|
|
path: spec.fetchSpec,
|
2022-12-06 22:18:33 -05:00
|
|
|
stdio,
|
2020-10-02 17:52:19 -04:00
|
|
|
pkg: manifest,
|
|
|
|
env: {
|
|
|
|
npm_package_from: tarball.from,
|
|
|
|
npm_package_resolved: tarball.resolved,
|
2022-01-14 19:42:48 +02:00
|
|
|
npm_package_integrity: tarball.integrity,
|
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tarball
|
|
|
|
}
|