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(cli): added build field to cdk.json #17176

Merged
merged 19 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ Some of the interesting keys that can be used in the JSON configuration files:
```json5
{
"app": "node bin/main.js", // Command to start the CDK app (--app='node bin/main.js')
"build": "./build.sh", // Specify pre-synth build (no command line option)
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
"context": { // Context entries (--context=key=value)
"key": "value"
},
Expand Down
11 changes: 8 additions & 3 deletions packages/aws-cdk/lib/api/cxapp/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
debug('context:', context);
env[cxapi.CONTEXT_ENV] = JSON.stringify(context);

const build = config.settings.get(['build']);
if (build) {
await exec(build);
}

const app = config.settings.get(['app']);
if (!app) {
throw new Error(`--app is required either in command-line, in ${PROJECT_CONFIG} or in ${USER_DEFAULTS}`);
Expand Down Expand Up @@ -74,7 +79,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom

debug('env:', env);

await exec();
await exec(commandLine[0], commandLine.slice(1));
comcalvi marked this conversation as resolved.
Show resolved Hide resolved

return createAssembly(outdir);

Expand All @@ -91,7 +96,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
}
}

async function exec() {
async function exec(command: string, args: string[] = []) {
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
return new Promise<void>((ok, fail) => {
// We use a slightly lower-level interface to:
//
Expand All @@ -103,7 +108,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
// anyway, and if the subprocess is printing to it for debugging purposes the
// user gets to see it sooner. Plus, capturing doesn't interact nicely with some
// processes like Maven.
const proc = childProcess.spawn(commandLine[0], commandLine.slice(1), {
const proc = childProcess.spawn(command, args, {
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
stdio: ['ignore', 'inherit', 'inherit'],
detached: false,
shell: true,
Expand Down
22 changes: 22 additions & 0 deletions packages/aws-cdk/test/api/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ test('application set in --app is `*.js` and executable', async () => {
await execProgram(sdkProvider, config);
});

test('cli throws when the `build` script fails', async () => {
config.settings.set(['build'], 'fake-command');
const expectedError = 'Subprocess exited with error 127';
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
mockSpawn({
commandLine: ['fake-command'],
exitCode: 127,
});

await expect(execProgram(sdkProvider, config)).rejects.toEqual(new Error(expectedError));
}, TEN_SECOND_TIMEOUT);

test('cli does not throw when the `build` script succeeds', async () => {
config.settings.set(['build'], 'real-command');
mockSpawn({
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
commandLine: ['real-command'],
exitCode: 0,
});

await expect(execProgram(sdkProvider, config)).resolves;
}, TEN_SECOND_TIMEOUT);


function writeOutputAssembly() {
const asm = testAssembly({
stacks: [],
Expand Down