Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add configurable pack command #4021

Merged
merged 5 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/jsii-pacmak/bin/jsii-pacmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.',
Expand Down
4 changes: 3 additions & 1 deletion packages/jsii-pacmak/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
31 changes: 23 additions & 8 deletions packages/jsii-pacmak/lib/packaging.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as fs from 'fs-extra';
import type { Assembly, TypeSystem } from 'jsii-reflect';
import * as os from 'os';
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
Expand Down Expand Up @@ -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.
Expand All @@ -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)})`,
);
Expand Down
5 changes: 5 additions & 0 deletions packages/jsii-pacmak/test/build-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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