11cb0ef41Sopenharmony_ciconst pacote = require('pacote') 21cb0ef41Sopenharmony_ciconst libpack = require('libnpmpack') 31cb0ef41Sopenharmony_ciconst npa = require('npm-package-arg') 41cb0ef41Sopenharmony_ciconst log = require('../utils/log-shim') 51cb0ef41Sopenharmony_ciconst { getContents, logTar } = require('../utils/tar.js') 61cb0ef41Sopenharmony_ciconst BaseCommand = require('../base-command.js') 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciclass Pack extends BaseCommand { 91cb0ef41Sopenharmony_ci static description = 'Create a tarball from a package' 101cb0ef41Sopenharmony_ci static name = 'pack' 111cb0ef41Sopenharmony_ci static params = [ 121cb0ef41Sopenharmony_ci 'dry-run', 131cb0ef41Sopenharmony_ci 'json', 141cb0ef41Sopenharmony_ci 'pack-destination', 151cb0ef41Sopenharmony_ci 'workspace', 161cb0ef41Sopenharmony_ci 'workspaces', 171cb0ef41Sopenharmony_ci 'include-workspace-root', 181cb0ef41Sopenharmony_ci ] 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci static usage = ['<package-spec>'] 211cb0ef41Sopenharmony_ci static workspaces = true 221cb0ef41Sopenharmony_ci static ignoreImplicitWorkspace = false 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci async exec (args) { 251cb0ef41Sopenharmony_ci if (args.length === 0) { 261cb0ef41Sopenharmony_ci args = ['.'] 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci const unicode = this.npm.config.get('unicode') 301cb0ef41Sopenharmony_ci const json = this.npm.config.get('json') 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci // Get the manifests and filenames first so we can bail early on manifest 331cb0ef41Sopenharmony_ci // errors before making any tarballs 341cb0ef41Sopenharmony_ci const manifests = [] 351cb0ef41Sopenharmony_ci for (const arg of args) { 361cb0ef41Sopenharmony_ci const spec = npa(arg) 371cb0ef41Sopenharmony_ci const manifest = await pacote.manifest(spec, this.npm.flatOptions) 381cb0ef41Sopenharmony_ci if (!manifest._id) { 391cb0ef41Sopenharmony_ci throw new Error('Invalid package, must have name and version') 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci manifests.push({ arg, manifest }) 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci // Load tarball names up for printing afterward to isolate from the 451cb0ef41Sopenharmony_ci // noise generated during packing 461cb0ef41Sopenharmony_ci const tarballs = [] 471cb0ef41Sopenharmony_ci for (const { arg, manifest } of manifests) { 481cb0ef41Sopenharmony_ci const tarballData = await libpack(arg, { 491cb0ef41Sopenharmony_ci ...this.npm.flatOptions, 501cb0ef41Sopenharmony_ci foregroundScripts: this.npm.config.isDefault('foreground-scripts') 511cb0ef41Sopenharmony_ci ? true 521cb0ef41Sopenharmony_ci : this.npm.config.get('foreground-scripts'), 531cb0ef41Sopenharmony_ci prefix: this.npm.localPrefix, 541cb0ef41Sopenharmony_ci workspaces: this.workspacePaths, 551cb0ef41Sopenharmony_ci }) 561cb0ef41Sopenharmony_ci const pkgContents = await getContents(manifest, tarballData) 571cb0ef41Sopenharmony_ci tarballs.push(pkgContents) 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci if (json) { 611cb0ef41Sopenharmony_ci this.npm.output(JSON.stringify(tarballs, null, 2)) 621cb0ef41Sopenharmony_ci return 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci for (const tar of tarballs) { 661cb0ef41Sopenharmony_ci logTar(tar, { unicode }) 671cb0ef41Sopenharmony_ci this.npm.output(tar.filename.replace(/^@/, '').replace(/\//, '-')) 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci async execWorkspaces (args) { 721cb0ef41Sopenharmony_ci // If they either ask for nothing, or explicitly include '.' in the args, 731cb0ef41Sopenharmony_ci // we effectively translate that into each workspace requested 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci const useWorkspaces = args.length === 0 || args.includes('.') 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci if (!useWorkspaces) { 781cb0ef41Sopenharmony_ci log.warn('Ignoring workspaces for specified package(s)') 791cb0ef41Sopenharmony_ci return this.exec(args) 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci await this.setWorkspaces() 831cb0ef41Sopenharmony_ci return this.exec([...this.workspacePaths, ...args.filter(a => a !== '.')]) 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci} 861cb0ef41Sopenharmony_cimodule.exports = Pack 87