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(cdk-build-tools): Allow configuring jsii, jsii-pacmak and tsc #649

Merged
merged 1 commit into from
Sep 5, 2018
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
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"different" => "custom"/"non-default"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. Might be much more useful to allow setting those via env variables (CDK_BUILD_JSII=xxx) - I think that should be sufficient for the use cases you are after and less messy on the command line surface area

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