diff --git a/packages/aws-cdk-api/.gitignore b/packages/aws-cdk-api/.gitignore index 958571d2bf173..0d3ddc5ed0b45 100644 --- a/packages/aws-cdk-api/.gitignore +++ b/packages/aws-cdk-api/.gitignore @@ -17,8 +17,6 @@ nyc.config.js .LAST_PACKAGE *.snk -!test/integ/run-wrappers/dist -!test/integ/cli/**/* assets.json npm-shrinkwrap.json !.eslintrc.js diff --git a/packages/aws-cdk-api/.npmignore b/packages/aws-cdk-api/.npmignore index d9f0d60e25639..56ec3f1d6e04a 100644 --- a/packages/aws-cdk-api/.npmignore +++ b/packages/aws-cdk-api/.npmignore @@ -11,10 +11,6 @@ dist .LAST_BUILD *.snk -!lib/init-templates/*/*/tsconfig.json -!test/integ/cli/**/*.js -!test/integ/run-wrappers/dist - *.tsbuildinfo tsconfig.json diff --git a/packages/aws-cdk-api/README.md b/packages/aws-cdk-api/README.md index cb42db5332e3b..845fd89e6e778 100644 --- a/packages/aws-cdk-api/README.md +++ b/packages/aws-cdk-api/README.md @@ -1,4 +1,4 @@ -# cdk-cli-wrapper +# AWS CDK API --- @@ -15,15 +15,11 @@ -CDK CLI Wrapper Library. - -Internal only for now. - ## Overview -This package provides a library which wraps the CDK CLI, allowing you to interact with the CLI programmatically. +This package provides an API to interact with the AWS CDK CLI programmatically. -Currently this package provides wrappers for: +Currently this package provides integrations for: - `cdk deploy` - `cdk synth` @@ -32,66 +28,58 @@ Currently this package provides wrappers for: ## Usage -First create a new `CdkCliWrapper` with the directory in which you want to execute commands, -and optionally any environment variables that you want to include in the execution environment. +First create a new `AwsCdkApi` construct from an existing `App` or `Stage`: ```ts -new CdkCliWrapper({ - directory: '/path/to/project', - env: { - KEY: 'value', - }, -}); +declare app: core.App; +const cdk = AwsCdkApi.fromApp(app); +``` + +```ts +declare stage: core.Stage; +const cdk = AwsCdkApi.fromStage(stage); ``` ### deploy ```ts -const cdk = new CdkCliWrapper({ - directory: '/project/path', -}); +declare app: core.App; +const cdk = AwsCdkApi.fromApp(app); cdk.deploy({ - app: 'node bin/my-app.js', - stacks: ['my-test-stack'], + stacks: ['MyTestStack'], }); ``` ### synth ```ts -const cdk = new CdkCliWrapper({ - directory: '/project/path', -}); +declare app: core.App; +const cdk = AwsCdkApi.fromApp(app); cdk.synth({ - app: 'node bin/my-app.js', - stacks: ['my-test-stack'], + stacks: ['MyTestStack'], }); ``` ### destroy ```ts -const cdk = new CdkCliWrapper({ - directory: '/project/path', -}); +declare app: core.App; +const cdk = AwsCdkApi.fromApp(app); cdk.destroy({ - app: 'node bin/my-app.js', - stacks: ['my-test-stack'], + stacks: ['MyTestStack'], }); ``` ### list ```ts -const cdk = new CdkCliWrapper({ - directory: '/project/path', -}); +declare app: core.App; +const cdk = AwsCdkApi.fromApp(app); cdk.list({ - app: 'node bin/my-app.js', stacks: ['*'], }); ``` diff --git a/packages/aws-cdk-api/lib/api.ts b/packages/aws-cdk-api/lib/api.ts index bb3bd4b3c1545..4bbf2442273d0 100644 --- a/packages/aws-cdk-api/lib/api.ts +++ b/packages/aws-cdk-api/lib/api.ts @@ -1,6 +1,6 @@ import * as core from '@aws-cdk/core'; +import { exec as runCli } from 'aws-cdk/lib'; import { Synthesizer } from 'aws-cdk/lib/api/cxapp/cloud-executable'; -import { initCommandLine as runCli } from 'aws-cdk/lib/cli'; import { Construct } from 'constructs'; import { SharedOptions, DeployOptions, DestroyOptions, SynthOptions, ListOptions, StackActivityProgress } from './commands'; @@ -30,8 +30,30 @@ export interface IAwsCdk { list(options: ListOptions): Promise; } -interface AwsCdkApiProps { - synthesizer: Synthesizer +/** + * Options to create an AWS CDK API from a directory + */ +export interface AwsCdkApiFromDirectoryProps { + /** + * the directory the AWS CDK app is in + * + * @default - current working directory + */ + readonly directory?: string + + /** + * command-line for executing your app or a cloud assembly directory + * e.g. "node bin/my-app.js" + * or + * "cdk.out" + * + * @default - read from cdk.json + */ + readonly app?: string +} + +interface AwsCdkApiProps extends AwsCdkApiFromDirectoryProps { + readonly synthesizer?: Synthesizer } /** @@ -39,6 +61,7 @@ interface AwsCdkApiProps { * wrapping the CLI with exec */ export class AwsCdkApi extends Construct implements IAwsCdk { + /** * Create an AwsCdkApi from an App */ @@ -56,6 +79,13 @@ export class AwsCdkApi extends Construct implements IAwsCdk { }); } + /** + * Create an AwsCdkApi from a directory + */ + public static fromDirectory(id?: string, props: AwsCdkApiFromDirectoryProps = {}) { + return new AwsCdkApi(undefined as any, id ?? 'AwsCdkApi', props); + } + private constructor(scope: Construct, id: string, private readonly props: AwsCdkApiProps) { super(scope, id); } @@ -135,7 +165,7 @@ export class AwsCdkApi extends Construct implements IAwsCdk { this.validateArgs(options); const stacks = options.stacks ?? []; return [ - ...options.app ? ['--app', options.app] : [], + ...this.defaultAppArgument(), ...renderBooleanArg('strict', options.strict), ...renderBooleanArg('trace', options.trace), ...renderBooleanArg('lookups', options.lookups), @@ -159,6 +189,14 @@ export class AwsCdkApi extends Construct implements IAwsCdk { ...options.all ? ['--all'] : [], ]; } + + private defaultAppArgument(): string[] { + if (this.props.synthesizer || !this.props.app) { + return []; + } + + return ['--app', this.props.app]; + } } function renderMapArrayArg(flag: string, parameters: { [name: string]: string | undefined }): string[] { diff --git a/packages/aws-cdk-api/lib/commands/common.ts b/packages/aws-cdk-api/lib/commands/common.ts index 80ce648e1eba0..8041e4d535bbd 100644 --- a/packages/aws-cdk-api/lib/commands/common.ts +++ b/packages/aws-cdk-api/lib/commands/common.ts @@ -40,17 +40,6 @@ export interface SharedOptions { */ readonly all?: boolean; - /** - * command-line for executing your app or a cloud assembly directory - * e.g. "node bin/my-app.js" - * or - * "cdk.out" - * - * @default - read from cdk.json - */ - readonly app?: string; - - /** * Role to pass to CloudFormation for deployment * diff --git a/packages/aws-cdk-api/lib/commands/deploy.ts b/packages/aws-cdk-api/lib/commands/deploy.ts index c65f4bd2ac862..a019bc6c04404 100644 --- a/packages/aws-cdk-api/lib/commands/deploy.ts +++ b/packages/aws-cdk-api/lib/commands/deploy.ts @@ -98,9 +98,8 @@ export interface DeployOptions extends SharedOptions { /** * Display mode for stack activity events * - * The default in the CLI is StackActivityProgress.BAR, but - * since the cli-wrapper will most likely be run in automation it makes - * more sense to set the default to StackActivityProgress.EVENTS + * The default in the CLI is StackActivityProgress.BAR. But since this is an API + * it makes more sense to set the default to StackActivityProgress.EVENTS * * @default StackActivityProgress.EVENTS */ diff --git a/packages/aws-cdk-api/package.json b/packages/aws-cdk-api/package.json index 26bd6291b5c9d..8317a0e0323f5 100644 --- a/packages/aws-cdk-api/package.json +++ b/packages/aws-cdk-api/package.json @@ -84,7 +84,7 @@ "BSD-2-Clause", "0BSD" ], - "dontAttribute": "^@aws-cdk/|^aws-cdk$|^cdk-assets$|^cdk-cli-wrapper$" + "dontAttribute": "^@aws-cdk/|^aws-cdk$|^cdk-assets$" } }, "pkglint": { diff --git a/packages/aws-cdk/lib/cli.ts b/packages/aws-cdk/lib/cli.ts index 3827a058a4ac5..f5d0e4f7bacb8 100644 --- a/packages/aws-cdk/lib/cli.ts +++ b/packages/aws-cdk/lib/cli.ts @@ -277,7 +277,7 @@ if (!process.stdout.isTTY) { process.env.FORCE_COLOR = '0'; } -export async function initCommandLine(args: string[], app?: Synthesizer) { +export async function exec(args: string[], app?: Synthesizer) { const argv = await parseCommandLineArguments(args); if (argv.verbose) { setLogLevel(argv.verbose); @@ -668,7 +668,7 @@ function yargsNegativeAlias { if (typeof value === 'number') { process.exitCode = value; diff --git a/packages/aws-cdk/lib/index.ts b/packages/aws-cdk/lib/index.ts index 57502a1035caf..a76f253b80ca6 100644 --- a/packages/aws-cdk/lib/index.ts +++ b/packages/aws-cdk/lib/index.ts @@ -1,2 +1,2 @@ export * from './api'; -export { cli } from './cli'; +export { cli, exec } from './cli';