Skip to content

Commit

Permalink
feat(aws-cdk): filtering stacks on deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kedrzu committed Aug 7, 2023
1 parent a5d1a99 commit 3766f62
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
env: {
node: true,
},
extends: [require.resolve('@nzyme/eslint/vue')],
extends: [require.resolve('@nzyme/eslint/typescript')],
};
51 changes: 42 additions & 9 deletions packages/aws-cdk/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface AppOptions {
readonly sdkProvider: SdkProvider;
}

export interface AppStackParams {
stacks?: cdk.Stack[] | ((stack: cdk.Stack) => boolean);
}

export class App extends cdk.App {
private readonly sdkProvider: SdkProvider;
private readonly deployments: Deployments;
Expand Down Expand Up @@ -53,22 +57,29 @@ export class App extends cdk.App {
);
}

public async build() {
public async build(params: AppStackParams = {}) {
for (const stack of this.stacks) {
if (!stackMatches(stack, params)) {
continue;
}

await stack.$.execute();
}
}

public async deploy() {
await this.build();
public async deploy(params: AppStackParams = {}) {
await this.build(params);

const cloudAssembly = this.synth();
const stacks = this.stacks;

for (const artifact of cloudAssembly.artifacts) {
if (artifact instanceof CloudFormationStackArtifact) {
const stack = stacks.find(stack => stack.stackName === artifact.stackName);
const stackName = artifact.stackName;
const stack = this.stacks.find(stack => stack.stackName === stackName);

if (stack && !stackMatches(stack, params)) {
continue;
}

consola.info(`Deploying stack ${chalk.green(stackName)}`);
stack?.$.emit('deploy:start');
Expand All @@ -84,15 +95,18 @@ export class App extends cdk.App {
}
}

public async destroy() {
public async destroy(params: AppStackParams = {}) {
const cloudAssembly = this.synth();
const stacks = this.stacks;

// Destroy stacks in reverse order.
for (const artifact of arrayReverse(cloudAssembly.artifacts)) {
if (artifact instanceof CloudFormationStackArtifact) {
const stack = stacks.find(stack => stack.stackName === artifact.stackName);
const stackName = artifact.stackName;
const stack = this.stacks.find(stack => stack.stackName === stackName);

if (stack && !stackMatches(stack, params)) {
continue;
}

consola.info(`Destroying stack ${chalk.green(stackName)}`);
stack?.$.emit('destroy:start');
Expand All @@ -108,13 +122,20 @@ export class App extends cdk.App {
}
}

public async diff() {
public async diff(params: AppStackParams = {}) {
await this.build();

const cloudAssembly = this.synth();

for (const artifact of cloudAssembly.artifacts) {
if (artifact instanceof CloudFormationStackArtifact) {
const stackName = artifact.stackName;
const stack = this.stacks.find(stack => stack.stackName === stackName);

if (stack && !stackMatches(stack, params)) {
continue;
}

const currentTemplate = await this.deployments.readCurrentTemplateWithNestedStacks(
artifact,
);
Expand All @@ -133,3 +154,15 @@ export class App extends cdk.App {
}
}
}

function stackMatches(stack: cdk.Stack, params: AppStackParams) {
if (!params.stacks) {
return true;
}

if (Array.isArray(params.stacks)) {
return params.stacks.includes(stack);
}

return params.stacks(stack);
}
5 changes: 4 additions & 1 deletion packages/aws-cdk/src/Stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export class Stack extends cdk.Stack {

private deployResult: DeployStackResult | undefined;

constructor(app: App, public readonly options: StackOptions) {
constructor(
app: App,
public readonly options: StackOptions,
) {
super(app, options.name, { env: options.env });

this.on('deploy:start', () => {
Expand Down

0 comments on commit 3766f62

Please sign in to comment.