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): watch is fully configurable from CLI #32903

Open
wants to merge 5 commits into
base: conroy/refact
Choose a base branch
from
Open
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
33 changes: 26 additions & 7 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,7 @@ export class CdkToolkit {
const rootDir = path.dirname(path.resolve(PROJECT_CONFIG));
debug("root directory used for 'watch' is: %s", rootDir);

const watchSettings: { include?: string | string[]; exclude: string | string[] } | undefined =
this.props.configuration.settings.get(['watch']);
if (!watchSettings) {
if (options.include === undefined && options.exclude === undefined) {
throw new ToolkitError(
"Cannot use the 'watch' command without specifying at least one directory to monitor. " +
'Make sure to add a "watch" key to your cdk.json',
Expand All @@ -629,7 +627,7 @@ export class CdkToolkit {
// 2. "watch" setting without an "include" key? We default to observing "./**".
// 3. "watch" setting with an empty "include" key? We default to observing "./**".
// 4. Non-empty "include" key? Just use the "include" key.
const watchIncludes = this.patternsArrayForWatch(watchSettings.include, {
const watchIncludes = this.patternsArrayForWatch(options.include, {
rootDir,
returnRootDirIfEmpty: true,
});
Expand All @@ -641,11 +639,11 @@ 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(['globalOptions', 'output']);
const watchExcludes = this.patternsArrayForWatch(watchSettings.exclude, {
const output = options.output ?? 'cdk.out';
const watchExcludes = this.patternsArrayForWatch(options.exclude, {
rootDir,
returnRootDirIfEmpty: false,
}).concat(`${outputDir}/**`, '**/.*', '**/.*/**', '**/node_modules/**');
}).concat(`${output}/**`, '**/.*', '**/.*/**', '**/node_modules/**');
debug("'exclude' patterns for 'watch': %s", watchExcludes);

// Since 'cdk deploy' is a relatively slow operation for a 'watch' process,
Expand Down Expand Up @@ -1522,6 +1520,27 @@ interface WatchOptions extends Omit<CfnDeployOptions, 'execute'> {
* @default 1
*/
readonly concurrency?: number;

/**
* Path where the CloudAssembly is located
*
* @default 'cdk.out'
*/
readonly output?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure we can just get that from the assembly itself.


/**
* Include these patterns in watch
*
* @default []
*/
readonly include?: string[];

/**
* Exclude these patterns from watch
*
* @default []
*/
readonly exclude?: string[];
}

export interface DeployOptions extends CfnDeployOptions, WatchOptions {
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
? AssetBuildTime.ALL_BEFORE_DEPLOY
: AssetBuildTime.JUST_IN_TIME,
ignoreNoStacks: deployOptions.ignoreNoStacks,
include: deployOptions.include,
exclude: deployOptions.exclude,
});

case 'rollback':
Expand Down Expand Up @@ -390,6 +392,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
return cli.watch({
selector: createSelector(watchOptions.STACKS),
exclusively: watchOptions.exclusively,
output: globalOptions.output,
toolkitStackName,
roleArn: globalOptions.roleArn,
reuseAssets: watchOptions.buildExclude,
Expand All @@ -403,6 +406,8 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
hotswap: determineHotswapMode(watchOptions.hotswap, watchOptions.hotswapFallback, true),
traceLogs: watchOptions.logs,
concurrency: watchOptions.concurrency,
include: watchOptions.include,
exclude: watchOptions.exclude,
});

case 'destroy':
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export async function makeConfig(): Promise<CliConfig> {
'and deploy the given stack(s) automatically when changes are detected. ' +
'Implies --hotswap by default',
},
'include': { type: 'array', desc: 'Watch files with these patterns', default: [] },
'exclude': { type: 'array', desc: 'Do not watch files with these patterns', default: [] },
'logs': {
type: 'boolean',
default: true,
Expand Down Expand Up @@ -249,6 +251,8 @@ export async function makeConfig(): Promise<CliConfig> {
variadic: true,
},
options: {
'include': { type: 'array', desc: 'Include files with these patterns', default: [] },
'exclude': { type: 'array', desc: 'Exclude files with these patterns', default: [] },
'build-exclude': { type: 'array', alias: 'E', desc: 'Do not rebuild asset with the given ID. Can be specified multiple times', default: [] },
'exclusively': { type: 'boolean', alias: 'e', desc: 'Only deploy requested stacks, don\'t include dependencies' },
'change-set-name': { type: 'string', desc: 'Name of the CloudFormation change set to create' },
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk/lib/convert-to-user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export function convertYargsToUserInput(args: any): UserInput {
hotswap: args.hotswap,
hotswapFallback: args.hotswapFallback,
watch: args.watch,
include: args.include,
exclude: args.exclude,
logs: args.logs,
concurrency: args.concurrency,
assetParallelism: args.assetParallelism,
Expand Down Expand Up @@ -149,6 +151,8 @@ export function convertYargsToUserInput(args: any): UserInput {

case 'watch':
commandOptions = {
include: args.include,
exclude: args.exclude,
buildExclude: args.buildExclude,
exclusively: args.exclusively,
changeSetName: args.changeSetName,
Expand Down Expand Up @@ -346,6 +350,8 @@ export function convertConfigToUserInput(config: any): UserInput {
hotswap: config.deploy?.hotswap,
hotswapFallback: config.deploy?.hotswapFallback,
watch: config.deploy?.watch,
include: Array.isArray(config.deploy?.include) ? config.deploy?.include : [config.deploy?.include],
exclude: Array.isArray(config.deploy?.exclude) ? config.deploy?.exclude : [config.deploy?.exclude],
logs: config.deploy?.logs,
concurrency: config.deploy?.concurrency,
assetParallelism: config.deploy?.assetParallelism,
Expand All @@ -369,6 +375,8 @@ export function convertConfigToUserInput(config: any): UserInput {
resourceMapping: config.import?.resourceMapping,
};
const watchOptions = {
include: Array.isArray(config.watch?.include) ? config.watch?.include : [config.watch?.include],
exclude: Array.isArray(config.watch?.exclude) ? config.watch?.exclude : [config.watch?.exclude],
buildExclude: config.watch?.buildExclude,
exclusively: config.watch?.exclusively,
changeSetName: config.watch?.changeSetName,
Expand Down
28 changes: 28 additions & 0 deletions packages/aws-cdk/lib/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,20 @@ export function parseCommandLineArguments(args: Array<string>): any {
type: 'boolean',
desc: 'Continuously observe the project files, and deploy the given stack(s) automatically when changes are detected. Implies --hotswap by default',
})
.option('include', {
default: [],
type: 'array',
desc: 'Watch files with these patterns',
nargs: 1,
requiresArg: true,
})
.option('exclude', {
default: [],
type: 'array',
desc: 'Do not watch files with these patterns',
nargs: 1,
requiresArg: true,
})
.option('logs', {
default: true,
type: 'boolean',
Expand Down Expand Up @@ -570,6 +584,20 @@ export function parseCommandLineArguments(args: Array<string>): any {
)
.command('watch [STACKS..]', "Shortcut for 'deploy --watch'", (yargs: Argv) =>
yargs
.option('include', {
default: [],
type: 'array',
desc: 'Include files with these patterns',
nargs: 1,
requiresArg: true,
})
.option('exclude', {
default: [],
type: 'array',
desc: 'Exclude files with these patterns',
nargs: 1,
requiresArg: true,
})
.option('build-exclude', {
default: [],
type: 'array',
Expand Down
28 changes: 28 additions & 0 deletions packages/aws-cdk/lib/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,20 @@ export interface DeployOptions {
*/
readonly watch?: boolean;

/**
* Watch files with these patterns
*
* @default - []
*/
readonly include?: Array<string>;

/**
* Do not watch files with these patterns
*
* @default - []
*/
readonly exclude?: Array<string>;

/**
* Show CloudWatch log events from all resources in the selected Stacks in the terminal. 'true' by default, use --no-logs to turn off. Only in effect if specified alongside the '--watch' option
*
Expand Down Expand Up @@ -897,6 +911,20 @@ export interface ImportOptions {
* @struct
*/
export interface WatchOptions {
/**
* Include files with these patterns
*
* @default - []
*/
readonly include?: Array<string>;

/**
* Exclude files with these patterns
*
* @default - []
*/
readonly exclude?: Array<string>;

/**
* Do not rebuild asset with the given ID. Can be specified multiple times
*
Expand Down
Loading
Loading