2020-10-02 17:52:19 -04:00
|
|
|
const util = require('util')
|
2017-05-28 21:04:08 -07:00
|
|
|
const log = require('npmlog')
|
2017-12-07 14:05:23 -08:00
|
|
|
const pacote = require('pacote')
|
2020-10-02 17:52:19 -04:00
|
|
|
const libpack = require('libnpmpack')
|
|
|
|
const npa = require('npm-package-arg')
|
2021-04-08 14:23:20 -04:00
|
|
|
const getWorkspaces = require('./workspaces/get-workspaces.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const { getContents, logTar } = require('./utils/tar.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const writeFile = util.promisify(require('fs').writeFile)
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
const BaseCommand = require('./base-command.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Pack extends BaseCommand {
|
2021-03-23 14:58:11 -04:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get description () {
|
|
|
|
return 'Create a tarball from a package'
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get name () {
|
|
|
|
return 'pack'
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-23 14:58:11 -04:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get params () {
|
2021-04-08 14:23:20 -04:00
|
|
|
return ['dry-run', 'workspace', 'workspaces']
|
2021-03-23 14:58:11 -04:00
|
|
|
}
|
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
2021-03-11 17:54:23 -05:00
|
|
|
static get usage () {
|
2021-03-23 14:58:11 -04:00
|
|
|
return ['[[<@scope>/]<pkg>...]']
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
exec (args, cb) {
|
|
|
|
this.pack(args).then(() => cb()).catch(cb)
|
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-04-08 14:23:20 -04:00
|
|
|
execWorkspaces (args, filters, cb) {
|
|
|
|
this.packWorkspaces(args, filters).then(() => cb()).catch(cb)
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
async pack (args) {
|
|
|
|
if (args.length === 0)
|
|
|
|
args = ['.']
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-23 14:58:11 -04:00
|
|
|
const unicode = this.npm.config.get('unicode')
|
2021-04-29 16:30:06 -04:00
|
|
|
const dryRun = this.npm.config.get('dry-run')
|
2017-05-09 14:46:02 -07:00
|
|
|
|
2021-04-29 16:30:06 -04:00
|
|
|
// Get the manifests and filenames first so we can bail early on manifest
|
|
|
|
// errors before making any tarballs
|
|
|
|
const manifests = []
|
|
|
|
for (const arg of args) {
|
2021-03-04 17:40:28 -05:00
|
|
|
const spec = npa(arg)
|
|
|
|
const manifest = await pacote.manifest(spec, this.npm.flatOptions)
|
2021-04-29 16:30:06 -04:00
|
|
|
if (!manifest._id)
|
|
|
|
throw new Error('Invalid package, must have name and version')
|
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const filename = `${manifest.name}-${manifest.version}.tgz`
|
|
|
|
.replace(/^@/, '').replace(/\//, '-')
|
2021-04-29 16:30:06 -04:00
|
|
|
manifests.push({ arg, filename, manifest })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load tarball names up for printing afterward to isolate from the
|
|
|
|
// noise generated during packing
|
|
|
|
const tarballs = []
|
|
|
|
for (const { arg, filename, manifest } of manifests) {
|
2021-03-04 17:40:28 -05:00
|
|
|
const tarballData = await libpack(arg, this.npm.flatOptions)
|
|
|
|
const pkgContents = await getContents(manifest, tarballData)
|
2017-05-28 21:04:08 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
if (!dryRun)
|
|
|
|
await writeFile(filename, tarballData)
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-04-29 16:30:06 -04:00
|
|
|
tarballs.push(pkgContents)
|
|
|
|
}
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
for (const tar of tarballs) {
|
|
|
|
logTar(tar, { log, unicode })
|
2021-03-11 17:54:23 -05:00
|
|
|
this.npm.output(tar.filename.replace(/^@/, '').replace(/\//, '-'))
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 14:23:20 -04:00
|
|
|
|
|
|
|
async packWorkspaces (args, filters) {
|
|
|
|
// If they either ask for nothing, or explicitly include '.' in the args,
|
|
|
|
// we effectively translate that into each workspace requested
|
|
|
|
|
|
|
|
const useWorkspaces = args.length === 0 || args.includes('.')
|
|
|
|
|
|
|
|
if (!useWorkspaces) {
|
|
|
|
this.npm.log.warn('Ignoring workspaces for specified package(s)')
|
|
|
|
return this.pack(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspaces =
|
|
|
|
await getWorkspaces(filters, { path: this.npm.localPrefix })
|
|
|
|
return this.pack([...workspaces.values(), ...args.filter(a => a !== '.')])
|
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
module.exports = Pack
|