Skip to content

Commit

Permalink
feat(cli): warn of non-existent stacks in cdk destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Dec 22, 2024
1 parent e8d8237 commit a3fde3b
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
import { minimatch } from 'minimatch';
import * as promptly from 'promptly';
import * as semver from 'semver';
import * as uuid from 'uuid';
import { DeploymentMethod, SuccessfulDeployStackResult } from './api';
import { SdkProvider } from './api/aws-auth';
Expand Down Expand Up @@ -53,6 +55,7 @@ import { validateSnsTopicArn } from './util/validate-notification-arn';
import { Concurrency, WorkGraph } from './util/work-graph';
import { WorkGraphBuilder } from './util/work-graph-builder';
import { AssetBuildNode, AssetPublishNode, StackNode } from './util/work-graph-types';
import { versionNumber } from './version';
import { environmentsFromDescriptors, globEnvironmentsFromStacks, looksLikeGlob } from '../lib/api/cxapp/environments';

// Must use a require() otherwise esbuild complains about calling a namespace
Expand Down Expand Up @@ -802,6 +805,9 @@ export class CdkToolkit {
stacks = stacks.reversed();

if (!options.force) {
if (stacks.stackArtifacts.length === 0) {
return;

Check warning on line 809 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L809

Added line #L809 was not covered by tests
}
// eslint-disable-next-line max-len
const confirmed = await promptly.confirm(
`Are you sure you want to delete: ${chalk.blue(stacks.stackArtifacts.map((s) => s.hierarchicalId).join(', '))} (y/n)?`,
Expand Down Expand Up @@ -1157,8 +1163,43 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const selectorWithoutPatterns: StackSelector = {
...selector,
allTopLevel: true,
patterns: [],
};

Check warning on line 1170 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1170

Added line #L1170 was not covered by tests
const stacksWithoutPatterns = await assembly.selectStacks(selectorWithoutPatterns, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
defaultBehavior: DefaultSelection.OnlySingle,
});

const patterns = selector.patterns.map(pattern => {

Check warning on line 1176 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1175-L1176

Added lines #L1175 - L1176 were not covered by tests
const notExist = !stacks.stackArtifacts.find(stack =>
minimatch(stack.hierarchicalId, pattern) || (stack.id === pattern && semver.major(versionNumber()) < 2),
);

const closelyMatched = notExist ? stacksWithoutPatterns.stackArtifacts.map(stack => {
if (minimatch(stack.hierarchicalId.toLowerCase(), pattern.toLowerCase())) {

Check warning on line 1182 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1182

Added line #L1182 was not covered by tests
return stack.hierarchicalId;
}
if (stack.id.toLowerCase() === pattern.toLowerCase() && semver.major(versionNumber()) < 2) {

Check warning on line 1185 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1185

Added line #L1185 was not covered by tests
return stack.id;
}
return;
}).filter((stack): stack is string => stack !== undefined) : [];

Check warning on line 1189 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1187-L1189

Added lines #L1187 - L1189 were not covered by tests
return {
pattern,
notExist,
closelyMatched,
};
});

patterns.forEach(pattern => {
if (pattern.notExist) {
const closelyMatched = pattern.closelyMatched.length > 0 ? ` Do you mean ${pattern.closelyMatched.join(', ')}?` : '';

Check warning on line 1199 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1199

Added line #L1199 was not covered by tests
warning(`${pattern.pattern} does not exist.${closelyMatched}`);
}
});

Check warning on line 1202 in packages/aws-cdk/lib/cdk-toolkit.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/cdk-toolkit.ts#L1202

Added line #L1202 was not covered by tests
return stacks;
}

Expand Down

0 comments on commit a3fde3b

Please sign in to comment.