Skip to content

Commit

Permalink
feat(cdk-build-tools): Allow configuring jsii, jsii-pacmak and tsc (#649
Browse files Browse the repository at this point in the history
)

Allows additional options to be used to customize the behavior of `cdk-build-tools` commands, making
it significantly easier to test a new version of `jsii`, `jsii-pacmak` or `tsc` without having to make complex
changes in the codebase or using `npm link`.

Permits using environment variables to specify those options. The environment variables are using the
`TRUMP_CASED` name of the command and option, separated with an `_` character. For example,
one can use `CDK_BUILD_JSII` to configure a different `jsii` executable to the `cdk-build` command.

The default behavior is unchanged from the current (not setting any of those will use the commands
provided by the dependency closure of the package).
  • Loading branch information
RomainMuller authored and Elad Ben-Israel committed Sep 5, 2018
1 parent 85c4e64 commit 29dc3c2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 17 deletions.
23 changes: 20 additions & 3 deletions tools/cdk-build-tools/bin/cdk-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@ import { shell } from '../lib/os';
import { cdkBuildOptions } from '../lib/package-info';
import { Timers } from '../lib/timer';

interface Arguments extends yargs.Arguments {
force?: boolean;
jsii?: string;
tsc?: string;
}

async function main() {
const args = yargs
const args: Arguments = yargs
.env('CDK_BUILD')
.usage('Usage: cdk-build')
.option('force', { type: 'boolean', alias: 'f', desc: 'Force a rebuild' })
.argv;
.option('jsii', {
type: 'string',
desc: 'Specify a different jsii executable',
defaultDescription: 'jsii provided by node dependencies'
})
.option('tsc', {
type: 'string',
desc: 'Specify a different tsc executable',
defaultDescription: 'tsc provided by node dependencies'
})
.argv as any;

const options = cdkBuildOptions();

Expand All @@ -21,7 +38,7 @@ async function main() {
await shell(['cfn2ts', `--scope=${options.cloudformation}`], timers);
}

await compileCurrentPackage(timers, args.force);
await compileCurrentPackage(timers, { jsii: args.jsii, tsc: args.tsc }, args.force);
}

const timers = new Timers();
Expand Down
21 changes: 16 additions & 5 deletions tools/cdk-build-tools/bin/cdk-package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs = require('fs-extra');
import { ChangeDetector } from "merkle-build";
import { ChangeDetector } from 'merkle-build';
import path = require('path');
import yargs = require('yargs');
import ignoreList = require('../lib/ignore-list');
Expand All @@ -9,11 +9,23 @@ import { Timers } from '../lib/timer';
const timers = new Timers();
const buildTimer = timers.start('Total time');

interface Arguments extends yargs.Arguments {
verbose: boolean;
jsiiPacmak: string;
}

async function main() {
const args = yargs
const args: Arguments = yargs
.env('CDK_PACKAGE')
.usage('Usage: cdk-package')
.option('verbose', { type: 'boolean', default: false, alias: 'v', desc: 'verbose output' })
.argv;
.option('jsii-pacmak', {
type: 'string',
desc: 'Specify a different jsii-pacmak executable',
default: require.resolve('jsii-pacmak/bin/jsii-pacmak'),
defaultDescription: 'jsii-pacmak provided by node dependencies'
})
.argv as any;

const detector = new ChangeDetector('.', {
ignore: ignoreList,
Expand All @@ -36,8 +48,7 @@ async function main() {
}

if (pkg.jsii) {
const pacmak = require.resolve('jsii-pacmak/bin/jsii-pacmak');
await shell([ pacmak, args.verbose ? '-vvv' : '-v', '-o', outdir ], timers);
await shell([ args.jsiiPacmak, args.verbose ? '-vvv' : '-v', '-o', outdir ], timers);
} else {
// just "npm pack" and deploy to "outdir"
const tarball = (await shell([ 'npm', 'pack' ], timers)).trim();
Expand Down
25 changes: 24 additions & 1 deletion tools/cdk-build-tools/bin/cdk-test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import fs = require('fs');
import util = require('util');
import yargs = require('yargs');
import { compileCurrentPackage } from '../lib/compile';
import { shell } from '../lib/os';
import { configFilePath, hasIntegTests, hasOnlyAutogeneratedTests, unitTestFiles } from '../lib/package-info';
import { Timers } from '../lib/timer';

interface Arguments extends yargs.Arguments {
force?: boolean;
jsii?: string;
tsc?: string;
}

async function main() {
const args: Arguments = yargs
.env('CDK_TEST')
.usage('Usage: cdk-test')
.option('force', { type: 'boolean', alias: 'f', desc: 'Force a rebuild' })
.option('jsii', {
type: 'string',
desc: 'Specify a different jsii executable',
defaultDescription: 'jsii provided by node dependencies'
})
.option('tsc', {
type: 'string',
desc: 'Specify a different tsc executable',
defaultDescription: 'tsc provided by node dependencies'
})
.argv as any;

// Always recompile before running tests, so it's impossible to forget.
// During a normal build, this means we'll compile twice, but the
// hash calculation makes that cheaper on CPU (if not on disk).
await compileCurrentPackage(timers);
await compileCurrentPackage(timers, { jsii: args.jsii, tsc: args.tsc });

const testFiles = await unitTestFiles();
if (testFiles.length > 0) {
Expand Down
25 changes: 23 additions & 2 deletions tools/cdk-build-tools/bin/cdk-watch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import yargs = require('yargs');
import { shell } from '../lib/os';
import { packageCompiler } from '../lib/package-info';

interface Arguments extends yargs.Arguments {
jsii?: string;
tsc?: string;
}

async function main() {
await shell([packageCompiler(), '-w']);
const args: Arguments = yargs
.env('CDK_WATCH')
.usage('Usage: cdk-watch')
.option('jsii', {
type: 'string',
desc: 'Specify a different jsii executable',
defaultDescription: 'jsii provided by node dependencies'
})
.option('tsc', {
type: 'string',
desc: 'Specify a different tsc executable',
defaultDescription: 'tsc provided by node dependencies'
})
.argv as any;

await shell([packageCompiler({ jsii: args.jsii, tsc: args.tsc }), '-w']);
}

main().catch(e => {
process.stderr.write(`${e.toString()}\n`);
process.exit(1);
});
});
6 changes: 3 additions & 3 deletions tools/cdk-build-tools/lib/compile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ChangeDetector } from "merkle-build";
import ignoreList = require('./ignore-list');
import { makeExecutable, shell } from "./os";
import { currentPackageJson, packageCompiler } from "./package-info";
import { CompilerOverrides, currentPackageJson, packageCompiler } from "./package-info";
import { Timers } from "./timer";

/**
* Run the compiler on the current package
*/
export async function compileCurrentPackage(timers: Timers, force?: boolean): Promise<void> {
export async function compileCurrentPackage(timers: Timers, compilers: CompilerOverrides = {}, force?: boolean): Promise<void> {
// We don't need to do the rest if the folder hash didn't change
// NOTE: This happens post-cfn2ts on purpose, since a change in cfn2ts or the spec might lead
// to different generated sources, in which case we DO need to recompile.
Expand All @@ -25,7 +25,7 @@ export async function compileCurrentPackage(timers: Timers, force?: boolean): Pr
return;
}

await shell([packageCompiler()], timers);
await shell([packageCompiler(compilers)], timers);

// Find files in bin/ that look like they should be executable, and make them so.
const scripts = currentPackageJson().bin || {};
Expand Down
11 changes: 8 additions & 3 deletions tools/cdk-build-tools/lib/package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ export async function hasIntegTests(): Promise<boolean> {
return files.length > 0;
}

export interface CompilerOverrides {
jsii?: string;
tsc?: string;
}

/**
* Return the compiler for this package (either tsc or jsii)
*/
export function packageCompiler() {
return isJsii() ? require.resolve(`jsii/bin/jsii`)
: require.resolve(`typescript/bin/tsc`);
export function packageCompiler(compilers: CompilerOverrides) {
return isJsii() ? compilers.jsii || require.resolve('jsii/bin/jsii')
: compilers.tsc || require.resolve('typescript/bin/tsc');
}

export interface CDKBuildOptions {
Expand Down

0 comments on commit 29dc3c2

Please sign in to comment.