Skip to content

Commit 4f8ecae

Browse files
authored
refactor(cli): generate yargs configuration from TS (#31850)
### Issue # (if applicable) N/A ### Reason for this change We'd like to create a programmatic interface to the CDK Toolkit. A bonus of this overhaul is moving to a single source of truth for both the programmatic interface to the CDK Toolkit and the command line interface to the CDK Toolkit. This PR generates the existing `yargs` configuration from a TS configuration. In the long term, we'd generate the `yargs` configuration purely from the programmatic interface to the Toolkit, but this is an improvement with less effort. ### Description of changes Creates a new package, `@aws-cdk/yargs-gen`, which generates our `yargs` configuration from a `CliConfig` defined in `aws-cdk/config.ts` using `@cdklabs/typewriter`. ### Description of how you validated changes N/A yet. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5a3a32f commit 4f8ecae

26 files changed

+1924
-335
lines changed

lerna.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"packages/@aws-cdk-testing/*",
1111
"packages/@aws-cdk/*/lambda-packages/*",
1212
"tools/@aws-cdk/cdk-build-tools",
13+
"tools/@aws-cdk/yargs-gen",
1314
"tools/@aws-cdk/cdk-release",
1415
"tools/@aws-cdk/node-bundle",
1516
"tools/@aws-cdk/pkglint",

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"packages/@aws-cdk-testing/*",
7979
"packages/@aws-cdk/*/lambda-packages/*",
8080
"tools/@aws-cdk/cdk-build-tools",
81+
"tools/@aws-cdk/yargs-gen",
8182
"tools/@aws-cdk/cdk-release",
8283
"tools/@aws-cdk/node-bundle",
8384
"tools/@aws-cdk/pkglint",

packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ The @aws-cdk/cli-lib-alpha package includes the following third-party software/l
207207

208208
----------------
209209

210-
** @jsii/check-node@1.103.1 - https://www.npmjs.com/package/@jsii/check-node/v/1.103.1 | Apache-2.0
210+
** @jsii/check-node@1.104.0 - https://www.npmjs.com/package/@jsii/check-node/v/1.104.0 | Apache-2.0
211211
jsii
212212
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
213213

@@ -3562,7 +3562,7 @@ THE SOFTWARE.
35623562

35633563
----------------
35643564

3565-
** tslib@2.7.0 - https://www.npmjs.com/package/tslib/v/2.7.0 | 0BSD
3565+
** tslib@2.8.0 - https://www.npmjs.com/package/tslib/v/2.8.0 | 0BSD
35663566
Copyright (c) Microsoft Corporation.
35673567

35683568
Permission to use, copy, modify, and/or distribute this software for any

packages/aws-cdk/CONTRIBUTING.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
## CLI Commands
2+
3+
All CDK CLI Commands are defined in `lib/config.ts`. This file is translated
4+
into a valid `yargs` configuration by `bin/yargs-gen`, which is generated by `@aws-cdk/yargs-gen`.
5+
The `yargs` configuration is generated into the function `parseCommandLineArguments()`,
6+
in `lib/parse-command-line-arguments.ts`, and is checked into git for readability and
7+
inspectability; do not edit this file by hand, as every subsequent `yarn build` will
8+
overwrite any manual edits. If you need to leverage a `yargs` feature not used by
9+
the CLI, you must add support for it to `@aws-cdk/yargs-gen`.
10+
11+
Note that `bin/yargs-gen` is executed by `ts-node`, which allows `config.ts` to
12+
reference functions and other identifiers defined in the CLI before the CLI is
13+
built.
14+
15+
### Dynamic Values
16+
17+
Some values, such as the user's platform, cannot be computed at build time.
18+
Some commands depend on these values, and thus `yargs-gen` must generate the
19+
code to compute these values at build time.
20+
21+
The only way to do this today is to reference a parameter with `DynamicValue.fromParameter`.
22+
The caller of `parseCommandLineArguments()` must pass the parameter.
23+
124
## Integration Tests
225

326
Unit tests are automatically run as part of the regular build. Integration tests

packages/aws-cdk/lib/cli.ts

+14-328
Large diffs are not rendered by default.

packages/aws-cdk/lib/config.ts

+419
Large diffs are not rendered by default.

packages/aws-cdk/lib/notices.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface NoticesProps {
2424
*
2525
* @default false
2626
*/
27-
readonly includeAcknowlegded?: boolean;
27+
readonly includeAcknowledged?: boolean;
2828

2929
}
3030

@@ -223,7 +223,7 @@ export class Notices {
223223
private constructor(props: NoticesProps) {
224224
this.configuration = props.configuration;
225225
this.acknowledgedIssueNumbers = new Set(this.configuration.context.get('acknowledged-issue-numbers') ?? []);
226-
this.includeAcknowlegded = props.includeAcknowlegded ?? false;
226+
this.includeAcknowlegded = props.includeAcknowledged ?? false;
227227
}
228228

229229
/**

0 commit comments

Comments
 (0)