Skip to content

Commit

Permalink
support app command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Nov 7, 2022
1 parent a74a5eb commit 4e04c25
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 62 deletions.
2 changes: 0 additions & 2 deletions packages/aws-cdk-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions packages/aws-cdk-api/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 22 additions & 34 deletions packages/aws-cdk-api/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cdk-cli-wrapper
# AWS CDK API
<!--BEGIN STABILITY BANNER-->

---
Expand All @@ -15,15 +15,11 @@

<!--END STABILITY BANNER-->

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`
Expand All @@ -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: ['*'],
});
```
46 changes: 42 additions & 4 deletions packages/aws-cdk-api/lib/api.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -30,15 +30,38 @@ export interface IAwsCdk {
list(options: ListOptions): Promise<void>;
}

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
}

/**
* Provides a programmatic interface for interacting with the CDK CLI by
* wrapping the CLI with exec
*/
export class AwsCdkApi extends Construct implements IAwsCdk {

/**
* Create an AwsCdkApi from an App
*/
Expand All @@ -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);
}
Expand Down Expand Up @@ -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),
Expand All @@ -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[] {
Expand Down
11 changes: 0 additions & 11 deletions packages/aws-cdk-api/lib/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 2 additions & 3 deletions packages/aws-cdk-api/lib/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -668,7 +668,7 @@ function yargsNegativeAlias<T extends { [x in S | L ]: boolean | undefined }, S
}

export function cli(args: string[] = process.argv.slice(2)) {
initCommandLine(args)
exec(args)
.then(async (value) => {
if (typeof value === 'number') {
process.exitCode = value;
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './api';
export { cli } from './cli';
export { cli, exec } from './cli';

0 comments on commit 4e04c25

Please sign in to comment.