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-06-17 18:59:38 +00:00
|
|
|
const path = require('path')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-11-04 20:42:47 +00: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-11-04 20:42:47 +00: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-11-18 20:58:02 +00:00
|
|
|
static description = 'Create a tarball from a package'
|
|
|
|
static name = 'pack'
|
|
|
|
static params = [
|
|
|
|
'dry-run',
|
|
|
|
'json',
|
|
|
|
'pack-destination',
|
|
|
|
'workspace',
|
|
|
|
'workspaces',
|
|
|
|
'include-workspace-root',
|
|
|
|
]
|
|
|
|
|
|
|
|
static usage = ['[[<@scope>/]<pkg>...]']
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec (args) {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (args.length === 0) {
|
2021-03-04 17:40:28 -05:00
|
|
|
args = ['.']
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
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')
|
2021-05-20 15:54:50 -04:00
|
|
|
const json = this.npm.config.get('json')
|
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-11-18 20:58:02 +00:00
|
|
|
if (!manifest._id) {
|
2021-04-29 16:30:06 -04:00
|
|
|
throw new Error('Invalid package, must have name and version')
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-04-29 16:30:06 -04:00
|
|
|
|
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)
|
2021-06-17 18:59:38 +00:00
|
|
|
const tarballFilename = path.resolve(this.npm.config.get('pack-destination'), filename)
|
2017-05-28 21:04:08 -07:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!dryRun) {
|
2021-06-17 18:59:38 +00:00
|
|
|
await writeFile(tarballFilename, tarballData)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
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-05-20 15:54:50 -04:00
|
|
|
if (json) {
|
|
|
|
this.npm.output(JSON.stringify(tarballs, null, 2))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async execWorkspaces (args, filters) {
|
2021-04-08 14:23:20 -04:00
|
|
|
// 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)')
|
2021-11-04 20:42:47 +00:00
|
|
|
return this.exec(args)
|
2021-04-08 14:23:20 -04:00
|
|
|
}
|
|
|
|
|
2021-06-17 18:59:38 +00:00
|
|
|
await this.setWorkspaces(filters)
|
2021-11-04 20:42:47 +00:00
|
|
|
return this.exec([...this.workspacePaths, ...args.filter(a => a !== '.')])
|
2021-04-08 14:23:20 -04:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
module.exports = Pack
|