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

chore(cli): refactor cli.ts to use UserInput #32760

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/api/cxapp/cloud-executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class CloudExecutable {
* Return whether there is an app command from the configuration
*/
public get hasApp() {
return !!this.props.configuration.settings.get(['app']);
return !!this.props.configuration.settings.get(['globalOptions', 'app']);
}

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ export class CloudExecutable {
}

private get canLookup() {
return !!(this.props.configuration.settings.get(['lookups']) ?? true);
return !!(this.props.configuration.settings.get(['globalOptions', 'lookups']) ?? true);
}
}

Expand Down
47 changes: 38 additions & 9 deletions packages/aws-cdk/lib/api/cxapp/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs-extra';
import * as semver from 'semver';
import { Command } from '../../command';
import { debug, warning } from '../../logging';
import { Configuration, PROJECT_CONFIG, USER_DEFAULTS } from '../../settings';
import { ToolkitError } from '../../toolkit/error';
Expand All @@ -24,12 +25,12 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
const env = await prepareDefaultEnvironment(aws);
const context = await prepareContext(config, env);

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

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

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

const outdir = config.settings.get(['output']);
const outdir = config.settings.get(['globalOptions', 'output']);
if (!outdir) {
throw new ToolkitError('unexpected: --output is required');
}
Expand Down Expand Up @@ -189,39 +190,67 @@ export async function prepareDefaultEnvironment(aws: SdkProvider): Promise<{ [ke
export async function prepareContext(config: Configuration, env: { [key: string]: string | undefined}) {
const context = config.context.all;

const debugMode: boolean = config.settings.get(['debug']) ?? true;
const debugMode: boolean = config.settings.get(['globalOptions', 'debug']) ?? true;
if (debugMode) {
env.CDK_DEBUG = 'true';
}

const pathMetadata: boolean = config.settings.get(['pathMetadata']) ?? true;
const pathMetadata: boolean = config.settings.get(['globalOptions', 'pathMetadata']) ?? true;
if (pathMetadata) {
context[cxapi.PATH_METADATA_ENABLE_CONTEXT] = true;
}

const assetMetadata: boolean = config.settings.get(['assetMetadata']) ?? true;
const assetMetadata: boolean = config.settings.get(['globalOptions', 'assetMetadata']) ?? true;
if (assetMetadata) {
context[cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT] = true;
}

const versionReporting: boolean = config.settings.get(['versionReporting']) ?? true;
const versionReporting: boolean = config.settings.get(['globalOptions', 'versionReporting']) ?? true;
if (versionReporting) { context[cxapi.ANALYTICS_REPORTING_ENABLED_CONTEXT] = true; }
// We need to keep on doing this for framework version from before this flag was deprecated.
if (!versionReporting) { context['aws:cdk:disable-version-reporting'] = true; }

const stagingEnabled = config.settings.get(['staging']) ?? true;
const stagingEnabled = config.settings.get(['globalOptions', 'staging']) ?? true;
if (!stagingEnabled) {
context[cxapi.DISABLE_ASSET_STAGING_CONTEXT] = true;
}

const bundlingStacks = config.settings.get(['bundlingStacks']) ?? ['**'];
const bundlingStacks = setBundlingStacks(config);
context[cxapi.BUNDLING_STACKS] = bundlingStacks;

debug('context:', context);

return context;
}

function setBundlingStacks(config: Configuration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This works but is hard to parse for a human. In general I prefer this style:

// explain how this is even possible
if (!config.command) {
  return [];
}

const stackCommands = [...];

// command doesn't operate on a stack selection
if (!stackCommands.includes(config.command) {
  return [];
}

// If we deploy, diff, synth or watch a list of stacks exclusively we skip
// bundling for all other stacks.
if (config.settings.get(['exclusively']) && config.settings.get(['STACKS'])) {
  return config.settings.get(['STACKS']);
}

return ['**'];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed. i had just copied over the code from elsewhere. will update

// Command may not exist if we are not executing a CLI command
if (!config.command) {
return [];
}

const stackCommands = [
Command.DEPLOY,
Command.DIFF,
Command.SYNTH,
Command.SYNTHESIZE,
Command.WATCH,
];

// command doesn't operate on a stack selection
if (!stackCommands.includes(config.command)) {
return [];
}

// If we deploy, diff, synth or watch a list of stacks exclusively we skip
// bundling for all other stacks.
if (config.settings.get(['globalOptions', 'exclusively']) && config.settings.get([config.command, 'STACKS'])) {
return config.settings.get([config.command, 'STACKS']);
}

return ['**'];
}

/**
* Make sure the 'app' is an array
*
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export class CdkToolkit {
// 2. Any file whose name starts with a dot.
// 3. Any directory's content whose name starts with a dot.
// 4. Any node_modules and its content (even if it's not a JS/TS project, you might be using a local aws-cli package)
const outputDir = this.props.configuration.settings.get(['output']);
const outputDir = this.props.configuration.settings.get(['globalOptions', 'output']);
const watchExcludes = this.patternsArrayForWatch(watchSettings.exclude, {
rootDir,
returnRootDirIfEmpty: false,
Expand Down
Loading
Loading