Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"app": "node refactoring.js",
"versionReporting": false,
"context": {
"aws-cdk:enableDiffNoFail": "true"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const cdk = require('aws-cdk-lib');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Creating a new app, to avoid cluttering the existing one, used for other refactoring scenarios.

const s3 = require('aws-cdk-lib/aws-s3');

const BUCKET_ID = process.env.BUCKET_ID ?? 'OldName';
const stackPrefix = process.env.STACK_NAME_PREFIX;

const app = new cdk.App();

let gamma = {
region: 'eu-central-1',
};
let prod = {
region: 'us-east-1',
};

class MyStack extends cdk.Stack {
constructor(scope, id) {
super(scope, id);
new s3.Bucket(this, BUCKET_ID);
}
}

new MyStack(app, `${stackPrefix}-gamma-stack`, { env: gamma });
new MyStack(app, `${stackPrefix}-prod-stack`, { env: prod });

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ integTest(
}),
);

integTest(
'cdk refactor - filters stacks by pattern',
withSpecificFixture('refactoring-multiple-envs', async (fixture) => {
// First, deploy the stacks
await fixture.cdkDeploy('gamma-stack', {
modEnv: {
BUCKET_ID: 'OldName',
},
});
await fixture.cdkDeploy('prod-stack', {
modEnv: {
BUCKET_ID: 'OldName',
},
});

// Then see if the refactoring tool detects the change
const stdErr = await fixture.cdkRefactor({
options: ['*-gamma-stack', '--dry-run', '--unstable=refactor'],
allowErrExit: true,
captureStderr: true,
// Making sure the synthesized stack has a queue with
// the new name so that a refactor is detected
modEnv: {
BUCKET_ID: 'NewName',
},
});

const numberOfEnvironments = (stdErr.match(/Resource Type/g) || []).length;
expect(numberOfEnvironments).toEqual(1);
}),
);

function removeColor(str: string): string {
return str.replace(/\x1B[[(?);]{0,2}(;?\d)*./g, '');
}
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ export async function makeConfig(): Promise<CliConfig> {
desc: 'Whether to do the refactor without asking for confirmation',
},
},
arg: {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the actual fix. The rest follows from it.

name: 'STACKS',
variadic: true,
},
},
'cli-telemetry': {
description: 'Enable or disable anonymous telemetry',
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/cli/cli-type-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@
"default": false,
"desc": "Whether to do the refactor without asking for confirmation"
}
},
"arg": {
"name": "STACKS",
"variadic": true
}
},
"cli-telemetry": {
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/convert-to-user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export function convertYargsToUserInput(args: any): UserInput {
overrideFile: args.overrideFile,
revert: args.revert,
force: args.force,
STACKS: args.STACKS,
};
break;

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/cli/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
}),
)
.command('doctor', 'Check your set-up for potential problems')
.command('refactor', 'Moves resources between stacks or within the same stack', (yargs: Argv) =>
.command('refactor [STACKS..]', 'Moves resources between stacks or within the same stack', (yargs: Argv) =>
yargs
.option('additional-stack-name', {
type: 'array',
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk/lib/cli/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,11 @@ export interface RefactorOptions {
* @default - false
*/
readonly force?: boolean;

/**
* Positional argument for refactor
*/
readonly STACKS?: Array<string>;
}

/**
Expand Down
Loading