Skip to content

Commit

Permalink
feat: add configurable pack command (#4021)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
agdimech committed Mar 22, 2023
1 parent 0db8481 commit 6b2fd18
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
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

0 comments on commit 6b2fd18

Please sign in to comment.