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 9 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
7 changes: 7 additions & 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": "mvn package", // Specify pre-synth build (no command line option)
"context": { // Context entries (--context=key=value)
"key": "value"
},
Expand All @@ -473,6 +474,12 @@ Some of the interesting keys that can be used in the JSON configuration files:
}
```

If specified, the command in the `build` key will be executed immediately before synthesis.
This can be used to build Lambda Functions, CDK Application code, or other assets.
`build` cannot be specified on the command line or in the User configuration,
and must be specified in the Project configuration. The command specified
in `build` will be executed by the "watch" process before deployment.

### Environment

The following environment variables affect aws-cdk:
Expand Down
21 changes: 14 additions & 7 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(commandToArray(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 All @@ -57,7 +62,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
return createAssembly(app);
}

const commandLine = await guessExecutable(appToArray(app));
const commandLine = await guessExecutable(commandToArray(app));
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure this is better TBH...

What happens if the command has quotes in it?

For example "mvn package"?

Bottom line, I think we should simply spawn this command without splitting it into arguments and with shell:true. There's a variant of spawn I believe that just accepts a single string and passes it to the shell.

There are also intricacies related to Windows/POSIX here that can blow up in 5,000 ways, so I rather we avoid any parsing of the command line if possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

@eladb just to be clear - you also want to change how we handle the "app" key, right? And no longer do any splitting there?


const outdir = config.settings.get(['output']);
if (!outdir) {
Expand All @@ -74,7 +79,7 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom

debug('env:', env);

await exec();
await exec(commandLine);

return createAssembly(outdir);

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

async function exec() {
async function exec(commandAndArgs: string[]) {
return new Promise<void>((ok, fail) => {
// We use a slightly lower-level interface to:
//
Expand All @@ -103,7 +108,9 @@ 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 command = commandAndArgs[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if commandAndArgs[0] is mvn package

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we just need to accept a single string and pass it down to spawn(). Since you use shell:true this should just work without splitting to arguments imho

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to split the arguments because of the other usage of exec(), namely this line:

await exec(commandLine);

commandLine is defined by

const commandLine = await guessExecutable(appToArray(app));

guessExecutable() needs a string[], not a string. Do you want me to rework this so that we don't need to have any string[]s passed to exec(), and instead make exec() take just a string? exec() previously operated on a string[].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eladb if commandAndArgs[0] is mvn package then spawn() will still correctly start the process.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a bit messy but if this works as is i am okay with that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eladb just made this cleaner, now mvn package will be split into ["mvn", "package"] instead of the previous ["mvn package"]. Would appreciate another pass on the PR!

const args = commandAndArgs.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 Expand Up @@ -150,12 +157,12 @@ async function populateDefaultEnvironmentIfNeeded(aws: SdkProvider, env: { [key:
}

/**
* Make sure the 'app' is an array
* Make sure the 'command' is an array
*
* If it's a string, split on spaces as a trivial way of tokenizing the command line.
*/
function appToArray(app: any) {
return typeof app === 'string' ? app.split(' ') : app;
function commandToArray(command: any) {
return typeof command === 'string' ? command.split(' ') : command;
}

type CommandGenerator = (file: string) => string[];
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class Configuration {

const readUserContext = this.props.readUserContext ?? true;

if (userConfig.get(['build'])) {
throw new Error('The `build` key cannot be specified in the user config (~/.cdk.json), specify it in the project config (cdk.json) instead');
}

const contextSources = [
this.commandLineContext,
this.projectConfig.subSettings([CONTEXT_KEY]).makeReadOnly(),
Expand Down
30 changes: 30 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,36 @@ test('application set in --app is `*.js` and executable', async () => {
await execProgram(sdkProvider, config);
});

test('cli throws when the `build` script fails', async () => {
// GIVEN
config.settings.set(['build'], 'fake-command');
mockSpawn({
commandLine: ['fake-command'],
exitCode: 127,
});

// WHEN
await expect(execProgram(sdkProvider, config)).rejects.toEqual(new Error('Subprocess exited with error 127'));
}, TEN_SECOND_TIMEOUT);

test('cli does not throw when the `build` script succeeds', async () => {
// GIVEN
config.settings.set(['build'], 'real command');
config.settings.set(['app'], 'executable-app.js');
mockSpawn({
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
commandLine: ['real', 'command'], // `build` key is split on whitespace
exitCode: 0,
},
{
commandLine: ['executable-app.js'],
sideEffect: () => writeOutputAssembly(),
});

// WHEN
await execProgram(sdkProvider, config);
}, TEN_SECOND_TIMEOUT);


function writeOutputAssembly() {
const asm = testAssembly({
stacks: [],
Expand Down
20 changes: 20 additions & 0 deletions packages/aws-cdk/test/usersettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ test('load context from all 3 files if available', async () => {
expect(config.context.get('project')).toBe('foobar');
expect(config.context.get('foo')).toBe('bar');
expect(config.context.get('test')).toBe('bar');
});

test('throws an error if the `build` key is specified in the user config', async () => {
// GIVEN
const GIVEN_CONFIG: Map<string, any> = new Map([
[USER_CONFIG, {
build: 'foobar',
}],
]);

// WHEN
mockedFs.pathExists.mockImplementation(path => {
return GIVEN_CONFIG.has(path);
});
mockedFs.readJSON.mockImplementation(path => {
return GIVEN_CONFIG.get(path);
});

// THEN
await expect(new Configuration().load()).rejects.toEqual(new Error('The `build` key cannot be specified in the user config (~/.cdk.json), specify it in the project config (cdk.json) instead'));
});