Skip to content

Commit

Permalink
chore(cli): rename cli-args-gen into user-input-gen (aws#32821)
Browse files Browse the repository at this point in the history
This PR does not change CLI functionality because we are not using `CliArguments` yet. This PR includes the following related changes:

- `CliArguments` are renamed `UserInput` to reflect what the schema represents -- they are options available to be specified via CLI options or `cdk.json`.
- the tool previously known as `cli-arg-gen` is now named `user-input-gen` to reflect this change.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored Jan 10, 2025
1 parent aff160b commit a8ad62c
Show file tree
Hide file tree
Showing 33 changed files with 106 additions and 103 deletions.
2 changes: 1 addition & 1 deletion aws-cdk.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"name": "aws-custom-resource-sdk-adapter",
"rootPath": "packages/@aws-cdk/aws-custom-resource-sdk-adapter"
},
{ "name": "cli-args-gen", "rootPath": "tools/@aws-cdk/cli-args-gen" }
{ "name": "user-input-gen", "rootPath": "tools/@aws-cdk/user-input-gen" }
]
},
"extensions": {
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packages/@aws-cdk-testing/*",
"packages/@aws-cdk/*/lambda-packages/*",
"tools/@aws-cdk/cdk-build-tools",
"tools/@aws-cdk/cli-args-gen",
"tools/@aws-cdk/user-input-gen",
"tools/@aws-cdk/cdk-release",
"tools/@aws-cdk/node-bundle",
"tools/@aws-cdk/pkglint",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"packages/@aws-cdk-testing/*",
"packages/@aws-cdk/*/lambda-packages/*",
"tools/@aws-cdk/cdk-build-tools",
"tools/@aws-cdk/cli-args-gen",
"tools/@aws-cdk/user-input-gen",
"tools/@aws-cdk/cdk-release",
"tools/@aws-cdk/node-bundle",
"tools/@aws-cdk/pkglint",
Expand Down
8 changes: 4 additions & 4 deletions packages/aws-cdk/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
## CLI Commands

All CDK CLI Commands are defined in `lib/config.ts`. This file is translated
into a valid `yargs` configuration by `bin/cli-args-gen`, which is generated by `@aws-cdk/cli-args-gen`.
into a valid `yargs` configuration by `bin/user-input-gen`, which is generated by `@aws-cdk/user-input-gen`.
The `yargs` configuration is generated into the function `parseCommandLineArguments()`,
in `lib/parse-command-line-arguments.ts`, and is checked into git for readability and
inspectability; do not edit this file by hand, as every subsequent `yarn build` will
overwrite any manual edits. If you need to leverage a `yargs` feature not used by
the CLI, you must add support for it to `@aws-cdk/cli-args-gen`.
the CLI, you must add support for it to `@aws-cdk/user-input-gen`.

Note that `bin/cli-args-gen` is executed by `ts-node`, which allows `config.ts` to
Note that `bin/user-input-gen` is executed by `ts-node`, which allows `config.ts` to
reference functions and other identifiers defined in the CLI before the CLI is
built.

### Dynamic Values

Some values, such as the user's platform, cannot be computed at build time.
Some commands depend on these values, and thus `cli-args-gen` must generate the
Some commands depend on these values, and thus `user-input-gen` must generate the
code to compute these values at build time.

The only way to do this today is to reference a parameter with `DynamicValue.fromParameter`.
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const config = {
"<rootDir>/lib/api/aws-auth/sdk.ts",
// Files generated by cli-args-gen
"<rootDir>/lib/parse-command-line-arguments.ts",
"<rootDir>/lib/cli-arguments.ts",
"<rootDir>/lib/convert-to-cli-args.ts",
"<rootDir>/lib/user-input.ts",
"<rootDir>/lib/convert-to-user-input.ts",
],

// We have many tests here that commonly time out
Expand Down
9 changes: 6 additions & 3 deletions packages/aws-cdk/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { CliHelpers, type CliConfig } from '@aws-cdk/cli-args-gen';
import { CliHelpers, type CliConfig } from '@aws-cdk/user-input-gen';
import { StackActivityProgress } from './api/util/cloudformation/stack-activity-monitor';
import { MIGRATE_SUPPORTED_LANGUAGES } from './commands/migrate';
import { RequireApproval } from './diff';
Expand All @@ -8,8 +8,11 @@ import { availableInitLanguages } from './init';
export const YARGS_HELPERS = new CliHelpers('./util/yargs-helpers');

/**
* Source of truth for all CDK CLI commands. `cli-args-gen` translates this into the `yargs` definition
* in `lib/parse-command-line-arguments.ts`.
* Source of truth for all CDK CLI commands. `user-input-gen` translates this into:
*
* - the `yargs` definition in `lib/parse-command-line-arguments.ts`.
* - the `UserInput` type in `lib/user-input.ts`.
* - the `convertXxxToUserInput` functions in `lib/convert-to-user-input.ts`.
*/
export async function makeConfig(): Promise<CliConfig> {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Do not edit by hand; all changes will be overwritten at build time from the config file.
// -------------------------------------------------------------------------------------------
/* eslint-disable @stylistic/max-len */
import { CliArguments, GlobalOptions } from './cli-arguments';
import { Command } from './settings';
import { UserInput, GlobalOptions } from './user-input';

// @ts-ignore TS6133
export function convertYargsToCliArgs(args: any): CliArguments {
export function convertYargsToUserInput(args: any): UserInput {
const globalOptions: GlobalOptions = {
app: args.app,
build: args.build,
Expand Down Expand Up @@ -250,17 +250,17 @@ export function convertYargsToCliArgs(args: any): CliArguments {
commandOptions = {};
break;
}
const cliArguments: CliArguments = {
const userInput: UserInput = {
_: args._[0],
globalOptions,
[args._[0]]: commandOptions,
};

return cliArguments;
return userInput;
}

// @ts-ignore TS6133
export function convertConfigToCliArgs(config: any): CliArguments {
export function convertConfigToUserInput(config: any): UserInput {
const globalOptions: GlobalOptions = {
app: config.app,
build: config.build,
Expand Down Expand Up @@ -428,7 +428,7 @@ export function convertConfigToCliArgs(config: any): CliArguments {
browser: config.docs?.browser,
};
const doctorOptions = {};
const cliArguments: CliArguments = {
const userInput: UserInput = {
globalOptions,
list: listOptions,
synthesize: synthesizeOptions,
Expand All @@ -450,5 +450,5 @@ export function convertConfigToCliArgs(config: any): CliArguments {
doctor: doctorOptions,
};

return cliArguments;
return userInput;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import { Command } from './settings';

/**
* The structure of the CLI configuration, generated from packages/aws-cdk/lib/config.ts
* The structure of the user input -- either CLI options or cdk.json -- generated from packages/aws-cdk/lib/config.ts
*
* @struct
*/
export interface CliArguments {
export interface UserInput {
/**
* The CLI command name
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"scripts": {
"build": "cdk-build",
"cli-args-gen": "ts-node --preferTsExts scripts/cli-args-gen.ts",
"user-input-gen": "ts-node --preferTsExts scripts/user-input-gen.ts",
"watch": "cdk-watch",
"lint": "cdk-lint",
"pkglint": "pkglint -f",
Expand All @@ -29,7 +29,7 @@
},
"cdk-build": {
"pre": [
"yarn cli-args-gen"
"yarn user-input-gen"
],
"post": [
"cp ../../node_modules/cdk-from-cfn/index_bg.wasm ./lib/",
Expand Down Expand Up @@ -70,7 +70,7 @@
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/cli-plugin-contract": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@aws-cdk/cli-args-gen": "0.0.0",
"@aws-cdk/user-input-gen": "0.0.0",
"@octokit/rest": "^18.12.0",
"@types/archiver": "^5.3.4",
"@types/fs-extra": "^9.0.13",
Expand Down
2 changes: 0 additions & 2 deletions packages/aws-cdk/scripts/cli-args-gen

This file was deleted.

15 changes: 0 additions & 15 deletions packages/aws-cdk/scripts/cli-args-gen.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/aws-cdk/scripts/user-input-gen
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./user-input-gen.js');
16 changes: 16 additions & 0 deletions packages/aws-cdk/scripts/user-input-gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderYargs, renderUserInputType, renderUserInputFuncs } from '@aws-cdk/user-input-gen';
import { makeConfig, YARGS_HELPERS } from '../lib/config';

async function main() {
const config = await makeConfig();
fs.writeFileSync('./lib/parse-command-line-arguments.ts', await renderYargs(config, YARGS_HELPERS));
fs.writeFileSync('./lib/user-input.ts', await renderUserInputType(config));
fs.writeFileSync('./lib/convert-to-user-input.ts', await renderUserInputFuncs(config));
}

main().then(() => {
}).catch((e) => {
throw e;
});
10 changes: 5 additions & 5 deletions packages/aws-cdk/test/cli-arguments.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { convertConfigToCliArgs, convertYargsToCliArgs } from '../lib/convert-to-cli-args';
import { convertConfigToUserInput, convertYargsToUserInput } from '../lib/convert-to-user-input';
import { parseCommandLineArguments } from '../lib/parse-command-line-arguments';

describe('yargs', () => {
test('yargs object can be converted to cli arguments', async () => {
const input = await parseCommandLineArguments(['deploy', '-R', '-v', '--ci']);

const result = convertYargsToCliArgs(input);
const result = convertYargsToUserInput(input);

expect(result).toEqual({
_: 'deploy',
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('yargs', () => {
test('positional argument is correctly passed through -- variadic', async () => {
const input = await parseCommandLineArguments(['deploy', 'stack1', 'stack2', '-R', '-v', '--ci']);

const result = convertYargsToCliArgs(input);
const result = convertYargsToUserInput(input);

expect(result).toEqual({
_: 'deploy',
Expand All @@ -84,7 +84,7 @@ describe('yargs', () => {
test('positional argument is correctly passed through -- single', async () => {
const input = await parseCommandLineArguments(['acknowledge', 'id1', '-v', '--ci']);

const result = convertYargsToCliArgs(input);
const result = convertYargsToUserInput(input);

expect(result).toEqual({
_: 'acknowledge',
Expand All @@ -109,7 +109,7 @@ describe('config', () => {
},
};

const result = convertConfigToCliArgs(input);
const result = convertConfigToUserInput(input);

expect(result).toEqual({
globalOptions: expect.objectContaining({
Expand Down
4 changes: 0 additions & 4 deletions tools/@aws-cdk/cli-args-gen/lib/index.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# cli-args-gen
# user-input-gen

Generates CDK CLI configurations from the source of truth in `packages/aws-cdk/lib/config.ts`.
Currently generates the following files:

- `packages/aws-cdk/lib/parse-command-line-arguments.ts`: `yargs` config.
- `packages/aws-cdk-lib/cli-arguments.ts`: strongly typed `CliArguments` interface.
- `packages/aws-cdk-lib/convert-to-cli-args.ts`: converts the `any` returned by `yargs` to `CliArguments`.
- `packages/aws-cdk/lib/user-input.ts`: strongly typed `UserInput` interface.
- `packages/aws-cdk/lib/convert-to-user-inpu.ts`: converts input from the CLI or `cdk.json` into `UserInput`.

## Usage

```ts
import { renderYargs } from '@aws-cdk/cli-args-gen';
import { renderYargs } from '@aws-cdk/user-input-gen';

declare const config: CliConfig;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ import { CliAction, CliConfig } from './yargs-types';
const CLI_ARG_NAME = 'args';
const CONFIG_ARG_NAME = 'config';

export async function renderCliArgsFunc(config: CliConfig): Promise<string> {
export async function renderUserInputFuncs(config: CliConfig): Promise<string> {
const scope = new Module('aws-cdk');

scope.documentation.push( '-------------------------------------------------------------------------------------------');
scope.documentation.push('GENERATED FROM packages/aws-cdk/lib/config.ts.');
scope.documentation.push('Do not edit by hand; all changes will be overwritten at build time from the config file.');
scope.documentation.push('-------------------------------------------------------------------------------------------');

scope.addImport(new SelectiveModuleImport(scope, './cli-arguments', ['CliArguments', 'GlobalOptions']));
const cliArgType = Type.fromName(scope, 'CliArguments');

scope.addImport(new SelectiveModuleImport(scope, './settings', ['Command']));
scope.addImport(new SelectiveModuleImport(scope, './user-input', ['UserInput', 'GlobalOptions']));
const userInputType = Type.fromName(scope, 'UserInput');

const createCliArguments = new FreeFunction(scope, {
name: 'convertYargsToCliArgs',
const convertYargsToUserInput = new FreeFunction(scope, {
name: 'convertYargsToUserInput',
export: true,
returnType: cliArgType,
returnType: userInputType,
parameters: [
{ name: 'args', type: Type.ANY },
],
});
createCliArguments.addBody(code.expr.directCode(buildCliArgsFunction(config)));
convertYargsToUserInput.addBody(code.expr.directCode(buildYargsToUserInputFunction(config)));

const createConfigArguments = new FreeFunction(scope, {
name: 'convertConfigToCliArgs',
name: 'convertConfigToUserInput',
export: true,
returnType: cliArgType,
returnType: userInputType,
parameters: [
{ name: 'config', type: Type.ANY },
],
Expand All @@ -52,14 +51,14 @@ export async function renderCliArgsFunc(config: CliConfig): Promise<string> {
});
}

function buildCliArgsFunction(config: CliConfig): string {
function buildYargsToUserInputFunction(config: CliConfig): string {
const globalOptions = buildGlobalOptions(config, CLI_ARG_NAME);
const commandSwitch = buildCommandSwitch(config, CLI_ARG_NAME);
const cliArgs = buildCliArgs(CLI_ARG_NAME);
const userInput = buildUserInput(CLI_ARG_NAME);
return [
globalOptions,
commandSwitch,
cliArgs,
userInput,
].join('\n');
}

Expand All @@ -85,7 +84,7 @@ function buildGlobalOptions(config: CliConfig, argName: string): string {
}

function buildCommandsList(config: CliConfig, argName: string): string {
const commandOptions = [];
const commandOptions: string[] = [];
// Note: we are intentionally not including aliases for the default options that can be
// specified via `cdk.json`. These options must be specified by the command name
// i.e. acknowledge rather than ack.
Expand Down Expand Up @@ -140,27 +139,27 @@ function buildPositionalArguments(arg: { name: string; variadic: boolean }, argN
return `${arg.name}: ${argName}.${arg.name}`;
}

function buildCliArgs(argName: string): string {
function buildUserInput(argName: string): string {
return [
'const cliArguments: CliArguments = {',
'const userInput: UserInput = {',
`_: ${argName}._[0],`,
'globalOptions,',
`[${argName}._[0]]: commandOptions`,
'}',
'',
'return cliArguments',
'return userInput',
].join('\n');
}

function buildConfigArgs(config: CliConfig): string {
return [
'const cliArguments: CliArguments = {',
'const userInput: UserInput = {',
'globalOptions,',
...(Object.keys(config.commands).map((commandName) => {
return `'${commandName}': ${kebabToCamelCase(commandName)}Options,`;
})),
'}',
'',
'return cliArguments',
'return userInput',
].join('\n');
}
4 changes: 4 additions & 0 deletions tools/@aws-cdk/user-input-gen/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './yargs-gen';
export * from './yargs-types';
export * from './user-input-gen';
export * from './convert-to-user-input-gen';
Loading

0 comments on commit a8ad62c

Please sign in to comment.