Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): rename cli-args-gen into user-input-gen #32821

Merged
merged 12 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 convertToCliArgs(args: any): CliArguments {
export function convertYargsToUserInput(args: any): UserInput {
const globalOptions: GlobalOptions = {
app: args.app,
build: args.build,
Expand Down Expand Up @@ -246,11 +246,11 @@ export function convertToCliArgs(args: any): CliArguments {
commandOptions = {};
break;
}
const cliArguments: CliArguments = {
const userInput: UserInput = {
_: args._[0],
globalOptions,
[args._[0]]: commandOptions,
};

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;
});
4 changes: 0 additions & 4 deletions tools/@aws-cdk/cli-args-gen/lib/index.ts

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ import * as prettier from 'prettier';
import { kebabToCamelCase } from './util';
import { CliAction, CliConfig } from './yargs-types';

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: 'convertToCliArgs',
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 ts = new TypeScriptRenderer({
disabledEsLintRules: [EsLintRules.MAX_LEN], // the default disabled rules result in 'Definition for rule 'prettier/prettier' was not found'
Expand All @@ -39,14 +38,14 @@ export async function renderCliArgsFunc(config: CliConfig): Promise<string> {
});
}

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

Expand Down Expand Up @@ -92,14 +91,14 @@ function buildPositionalArguments(arg: { name: string; variadic: boolean }): str
return `${arg.name}: args.${arg.name}`;
}

function buildCliArgs(): string {
function buildUserInput(): string {
return [
'const cliArguments: CliArguments = {',
'const userInput: UserInput = {',
'_: args._[0],',
'globalOptions,',
'[args._[0]]: commandOptions',
'}',
'',
'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';
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import * as prettier from 'prettier';
import { generateDefault, kebabToCamelCase, kebabToPascal } from './util';
import { CliConfig } from './yargs-types';

export async function renderCliArgsType(config: CliConfig): Promise<string> {
export async function renderUserInputType(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('-------------------------------------------------------------------------------------------');

const cliArgType = new StructType(scope, {
const userInputType = new StructType(scope, {
export: true,
name: 'CliArguments',
name: 'UserInput',
docs: {
summary: 'The structure of the CLI configuration, generated from packages/aws-cdk/lib/config.ts',
summary: 'The structure of the user input -- either CLI options or cdk.json -- generated from packages/aws-cdk/lib/config.ts',
},
});

// add required command
scope.addImport(new SelectiveModuleImport(scope, './settings', ['Command']));
const commandEnum = Type.fromName(scope, 'Command');

cliArgType.addProperty({
userInputType.addProperty({
name: '_',
type: commandEnum,
docs: {
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function renderCliArgsType(config: CliConfig): Promise<string> {
});
}

cliArgType.addProperty({
userInputType.addProperty({
name: 'globalOptions',
type: Type.fromName(scope, globalOptionType.name),
docs: {
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function renderCliArgsType(config: CliConfig): Promise<string> {
});
}

cliArgType.addProperty({
userInputType.addProperty({
name: kebabToCamelCase(commandName),
type: Type.fromName(scope, commandType.name),
docs: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@aws-cdk/cli-args-gen",
"name": "@aws-cdk/user-input-gen",
"private": true,
"version": "0.0.0",
"description": "Generate CLI arguments",
"description": "Generate User Inputs",
"repository": {
"type": "git",
"url": "https://github.com/aws/aws-cdk.git",
"directory": "tools/@aws-cdk/cli-args-gen"
"directory": "tools/@aws-cdk/user-input-gen"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CliConfig, renderCliArgsFunc } from '../lib';
import { CliConfig, renderUserInputFuncs } from '../lib';

describe('render', () => {
test('can generate conversion function', async () => {
Expand Down Expand Up @@ -42,17 +42,17 @@ describe('render', () => {
},
};

expect(await renderCliArgsFunc(config)).toMatchInlineSnapshot(`
expect(await renderUserInputFuncs(config)).toMatchInlineSnapshot(`
"// -------------------------------------------------------------------------------------------
// GENERATED FROM packages/aws-cdk/lib/config.ts.
// 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 convertToCliArgs(args: any): CliArguments {
export function convertYargsToUserInput(args: any): UserInput {
const globalOptions: GlobalOptions = {
app: args.app,
debug: args.debug,
Expand All @@ -68,13 +68,13 @@ describe('render', () => {
};
break;
}
const cliArguments: CliArguments = {
const userInput: UserInput = {
_: args._[0],
globalOptions,
[args._[0]]: commandOptions,
};

return cliArguments;
return userInput;
}
"
`);
Expand Down
Loading
Loading