Skip to content

Commit

Permalink
suggestStacks
Browse files Browse the repository at this point in the history
change order for calling methods

foreach to for
  • Loading branch information
go-to-k committed Dec 24, 2024
1 parent 17a9254 commit fd245af
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,16 +797,19 @@ export class CdkToolkit {
}

public async destroy(options: DestroyOptions) {
let stacks = await this.selectStacksForDestroy(options.selector, options.exclusively);
const assembly = await this.assembly();
let stacks = await this.selectStacksForDestroy(options.selector, assembly, options.exclusively);

await this.suggestStacks(options.selector, assembly, stacks, options.exclusively);
if (stacks.stackArtifacts.length === 0) {
warning(`No stacks match the name(s): ${chalk.red(options.selector.patterns.join(', '))}`);
return;
}

// The stacks will have been ordered for deployment, so reverse them for deletion.
stacks = stacks.reversed();

if (!options.force) {
if (stacks.stackArtifacts.length === 0) {
warning(`No stacks match the name(s): ${chalk.red(options.selector.patterns.join(', '))}`);
return;
}
// 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 @@ -1154,13 +1157,16 @@ export class CdkToolkit {
return selectedForDiff;
}

private async selectStacksForDestroy(selector: StackSelector, exclusively?: boolean) {
const assembly = await this.assembly();
private async selectStacksForDestroy(selector: StackSelector, assembly: CloudAssembly, exclusively?: boolean) {
const stacks = await assembly.selectStacks(selector, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
defaultBehavior: DefaultSelection.OnlySingle,
});

return stacks;
}

private async suggestStacks(selector: StackSelector, assembly: CloudAssembly, stacks: StackCollection, exclusively?: boolean) {
const selectorWithoutPatterns: StackSelector = {
...selector,
allTopLevel: true,
Expand All @@ -1182,20 +1188,20 @@ export class CdkToolkit {
}
return;
}).filter((stack): stack is string => stack !== undefined) : [];

return {
pattern,
notExist,
closelyMatched,
};
});

patterns.forEach(pattern => {
for (const pattern of patterns) {
if (pattern.notExist) {
const closelyMatched = pattern.closelyMatched.length > 0 ? ` Do you mean ${chalk.blue(pattern.closelyMatched.join(', '))}?` : '';
warning(`${chalk.red(pattern.pattern)} does not exist.${closelyMatched}`);
}
});
return stacks;
};
}

/**
Expand Down

0 comments on commit fd245af

Please sign in to comment.