diff --git a/packages/jsii-pacmak/bin/jsii-pacmak.ts b/packages/jsii-pacmak/bin/jsii-pacmak.ts index 87ca41e2d8..ac65244b45 100644 --- a/packages/jsii-pacmak/bin/jsii-pacmak.ts +++ b/packages/jsii-pacmak/bin/jsii-pacmak.ts @@ -6,6 +6,7 @@ import * as yargs from 'yargs'; import { pacmak, configureLogging, TargetName } from '../lib'; import { debug } from '../lib/logging'; +import { DEFAULT_PACK_COMMAND } from '../lib/packaging'; import { VERSION_DESC } from '../lib/version'; (async function main() { @@ -153,6 +154,12 @@ import { VERSION_DESC } from '../lib/version'; default: undefined, hidden: true, }) + .option('pack-command', { + type: 'string', + desc: 'Configure a custom command to create package tarballs. Command must output the name of the tarball.', + default: DEFAULT_PACK_COMMAND, + hidden: true, + }) .option('validate-assemblies', { type: 'boolean', desc: 'Whether jsii assemblies should be validated. This can be expensive and is skipped by default.', diff --git a/packages/jsii-pacmak/lib/index.ts b/packages/jsii-pacmak/lib/index.ts index 035e4d83b5..b1831d1267 100644 --- a/packages/jsii-pacmak/lib/index.ts +++ b/packages/jsii-pacmak/lib/index.ts @@ -74,7 +74,9 @@ export async function pacmak({ await timers.recordAsync('npm pack', () => { logging.info('Packaging NPM bundles'); - return Promise.all(modulesToPackageFlat.map((m) => m.npmPack())); + return Promise.all( + modulesToPackageFlat.map((m) => m.npmPack(argv['pack-command'])), + ); }); await timers.recordAsync('load jsii', () => { diff --git a/packages/jsii-pacmak/lib/packaging.ts b/packages/jsii-pacmak/lib/packaging.ts index d8d5fa2b9d..1d75d8a8ea 100644 --- a/packages/jsii-pacmak/lib/packaging.ts +++ b/packages/jsii-pacmak/lib/packaging.ts @@ -1,3 +1,4 @@ +import * as fs from 'fs-extra'; import type { Assembly, TypeSystem } from 'jsii-reflect'; import * as os from 'os'; import * as path from 'path'; @@ -5,6 +6,8 @@ import * as path from 'path'; import * as logging from '../lib/logging'; import { Scratch, shell } from './util'; +export const DEFAULT_PACK_COMMAND = 'npm pack'; + export interface JsiiModuleOptions { /** * Name of the module @@ -52,15 +55,27 @@ export class JsiiModule { /** * Prepare an NPM package from this source module */ - public async npmPack() { + public async npmPack(packCommand = DEFAULT_PACK_COMMAND) { this._tarball = await Scratch.make(async (tmpdir) => { - // Quoting (JSON-stringifying) the module directory in order to avoid - // problems if there are spaces or other special characters in the path. - const args = ['pack', JSON.stringify(this.moduleDirectory)]; - if (logging.level >= logging.LEVEL_VERBOSE) { - args.push('--loglevel=verbose'); + const args = []; + + if (packCommand === DEFAULT_PACK_COMMAND) { + // Quoting (JSON-stringifying) the module directory in order to avoid + // problems if there are spaces or other special characters in the path. + args.push(JSON.stringify(this.moduleDirectory)); + + if (logging.level >= logging.LEVEL_VERBOSE) { + args.push('--loglevel=verbose'); + } + } else { + // Ensure module is copied to tmpdir to ensure parallel execution does not content on generated tarballs + await fs.copy(this.moduleDirectory, tmpdir); } - const out = await shell('npm', args, { cwd: tmpdir }); + + const out = await shell(packCommand, args, { + cwd: tmpdir, + }); + // Take only the last line of npm pack which should contain the // tarball name. otherwise, there can be a lot of extra noise there // from scripts that emit to STDOUT. @@ -69,7 +84,7 @@ export class JsiiModule { if (!lastLine.endsWith('.tgz') && !lastLine.endsWith('.tar.gz')) { throw new Error( - `npm pack did not produce tarball from ${ + `${packCommand} did not produce tarball from ${ this.moduleDirectory } into ${tmpdir} (output was ${JSON.stringify(lines)})`, ); diff --git a/packages/jsii-pacmak/test/build-test.sh b/packages/jsii-pacmak/test/build-test.sh index b9026cec3c..56da0b55b6 100755 --- a/packages/jsii-pacmak/test/build-test.sh +++ b/packages/jsii-pacmak/test/build-test.sh @@ -81,3 +81,8 @@ done clean_dists echo "Testing ALL-AT-ONCE build." ${pacmak} ${OPTS} -v --no-parallel $packagedirs + +# Test custom pack command +clean_dists +echo "Testing yarn custom pack command." +${pacmak} ${OPTS} -v --pack-command='yarn pack -f custom.tgz -s && echo custom.tgz' ${PWD}/../../@scope/jsii-calc-base-of-base \ No newline at end of file