Skip to content

Commit 6b2fd18

Browse files
authored
feat: add configurable pack command (#4021)
This PR adds support for configuring a custom pack command. This is needed when using other package managers like `pnpm`. In terms of usage, developers would configure pacmak to use pnpm for example as follows: ``` jsii-pacmak --pack-command='pnpm pack' ``` --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 0db8481 commit 6b2fd18

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

packages/jsii-pacmak/bin/jsii-pacmak.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as yargs from 'yargs';
66

77
import { pacmak, configureLogging, TargetName } from '../lib';
88
import { debug } from '../lib/logging';
9+
import { DEFAULT_PACK_COMMAND } from '../lib/packaging';
910
import { VERSION_DESC } from '../lib/version';
1011

1112
(async function main() {
@@ -153,6 +154,12 @@ import { VERSION_DESC } from '../lib/version';
153154
default: undefined,
154155
hidden: true,
155156
})
157+
.option('pack-command', {
158+
type: 'string',
159+
desc: 'Configure a custom command to create package tarballs. Command must output the name of the tarball.',
160+
default: DEFAULT_PACK_COMMAND,
161+
hidden: true,
162+
})
156163
.option('validate-assemblies', {
157164
type: 'boolean',
158165
desc: 'Whether jsii assemblies should be validated. This can be expensive and is skipped by default.',

packages/jsii-pacmak/lib/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export async function pacmak({
7474

7575
await timers.recordAsync('npm pack', () => {
7676
logging.info('Packaging NPM bundles');
77-
return Promise.all(modulesToPackageFlat.map((m) => m.npmPack()));
77+
return Promise.all(
78+
modulesToPackageFlat.map((m) => m.npmPack(argv['pack-command'])),
79+
);
7880
});
7981

8082
await timers.recordAsync('load jsii', () => {

packages/jsii-pacmak/lib/packaging.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import * as fs from 'fs-extra';
12
import type { Assembly, TypeSystem } from 'jsii-reflect';
23
import * as os from 'os';
34
import * as path from 'path';
45

56
import * as logging from '../lib/logging';
67
import { Scratch, shell } from './util';
78

9+
export const DEFAULT_PACK_COMMAND = 'npm pack';
10+
811
export interface JsiiModuleOptions {
912
/**
1013
* Name of the module
@@ -52,15 +55,27 @@ export class JsiiModule {
5255
/**
5356
* Prepare an NPM package from this source module
5457
*/
55-
public async npmPack() {
58+
public async npmPack(packCommand = DEFAULT_PACK_COMMAND) {
5659
this._tarball = await Scratch.make(async (tmpdir) => {
57-
// Quoting (JSON-stringifying) the module directory in order to avoid
58-
// problems if there are spaces or other special characters in the path.
59-
const args = ['pack', JSON.stringify(this.moduleDirectory)];
60-
if (logging.level >= logging.LEVEL_VERBOSE) {
61-
args.push('--loglevel=verbose');
60+
const args = [];
61+
62+
if (packCommand === DEFAULT_PACK_COMMAND) {
63+
// Quoting (JSON-stringifying) the module directory in order to avoid
64+
// problems if there are spaces or other special characters in the path.
65+
args.push(JSON.stringify(this.moduleDirectory));
66+
67+
if (logging.level >= logging.LEVEL_VERBOSE) {
68+
args.push('--loglevel=verbose');
69+
}
70+
} else {
71+
// Ensure module is copied to tmpdir to ensure parallel execution does not content on generated tarballs
72+
await fs.copy(this.moduleDirectory, tmpdir);
6273
}
63-
const out = await shell('npm', args, { cwd: tmpdir });
74+
75+
const out = await shell(packCommand, args, {
76+
cwd: tmpdir,
77+
});
78+
6479
// Take only the last line of npm pack which should contain the
6580
// tarball name. otherwise, there can be a lot of extra noise there
6681
// from scripts that emit to STDOUT.
@@ -69,7 +84,7 @@ export class JsiiModule {
6984

7085
if (!lastLine.endsWith('.tgz') && !lastLine.endsWith('.tar.gz')) {
7186
throw new Error(
72-
`npm pack did not produce tarball from ${
87+
`${packCommand} did not produce tarball from ${
7388
this.moduleDirectory
7489
} into ${tmpdir} (output was ${JSON.stringify(lines)})`,
7590
);

packages/jsii-pacmak/test/build-test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ done
8181
clean_dists
8282
echo "Testing ALL-AT-ONCE build."
8383
${pacmak} ${OPTS} -v --no-parallel $packagedirs
84+
85+
# Test custom pack command
86+
clean_dists
87+
echo "Testing yarn custom pack command."
88+
${pacmak} ${OPTS} -v --pack-command='yarn pack -f custom.tgz -s && echo custom.tgz' ${PWD}/../../@scope/jsii-calc-base-of-base

0 commit comments

Comments
 (0)