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

feat(awscdk-construct): cdkDependenciesAsDeps #581

Merged
merged 3 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ new AwsCdkConstructLibrary(options: AwsCdkConstructLibraryOptions)
* **cdkVersion** (<code>string</code>) Minimum target version this library is tested against.
* **cdkAssert** (<code>boolean</code>) Install the @aws-cdk/assert library? __*Default*__: true
* **cdkDependencies** (<code>Array<string></code>) Which AWS CDK modules (those that start with "@aws-cdk/") does this library require when consumed? __*Optional*__
* **cdkDependenciesAsDeps** (<code>boolean</code>) If this is enabled (default), all modules declared in `cdkDependencies` will be also added as normal `dependencies` (as well as `peerDependencies`). __*Default*__: true
* **cdkTestDependencies** (<code>Array<string></code>) AWS CDK modules required for testing. __*Optional*__
* **cdkVersionPinning** (<code>boolean</code>) Use pinned version instead of caret version for CDK. __*Default*__: false

Expand All @@ -430,6 +431,7 @@ new AwsCdkConstructLibrary(options: AwsCdkConstructLibraryOptions)

Name | Type | Description
-----|------|-------------
**cdkDependenciesAsDeps**🔹 | <code>boolean</code> | Whether CDK dependencies are added as normal dependencies (and peer dependencies).
**version**🔹 | <code>string</code> | The target CDK version for this library.

### Methods
Expand All @@ -439,12 +441,13 @@ Name | Type | Description

Adds CDK modules as runtime dependencies.

Modules are currently added with a caret CDK version both as "dependencies"
Modules are currently by default added with a caret CDK version both as "dependencies"
and "peerDependencies". This is because currently npm would not
automatically install peer dependencies that are not declared as concerete
dependencies by the consumer, so this is a little npm "hack" so that
consumers will not need to depend on them directly if they don't interact
with them.
See `cdkDependenciesAsDeps` for changing the default behavior.

```ts
addCdkDependencies(...deps: string[]): void
Expand Down Expand Up @@ -914,6 +917,7 @@ new ConstructLibraryAws(options: AwsCdkConstructLibraryOptions)
* **cdkVersion** (<code>string</code>) Minimum target version this library is tested against.
* **cdkAssert** (<code>boolean</code>) Install the @aws-cdk/assert library? __*Default*__: true
* **cdkDependencies** (<code>Array<string></code>) Which AWS CDK modules (those that start with "@aws-cdk/") does this library require when consumed? __*Optional*__
* **cdkDependenciesAsDeps** (<code>boolean</code>) If this is enabled (default), all modules declared in `cdkDependencies` will be also added as normal `dependencies` (as well as `peerDependencies`). __*Default*__: true
* **cdkTestDependencies** (<code>Array<string></code>) AWS CDK modules required for testing. __*Optional*__
* **cdkVersionPinning** (<code>boolean</code>) Use pinned version instead of caret version for CDK. __*Default*__: false

Expand Down Expand Up @@ -6672,6 +6676,7 @@ Name | Type | Description
**catalog**?🔹 | <code>[Catalog](#projen-catalog)</code> | Libraries will be picked up by the construct catalog when they are published to npm as jsii modules and will be published under:.<br/>__*Default*__: new version will be announced
**cdkAssert**?🔹 | <code>boolean</code> | Install the @aws-cdk/assert library?<br/>__*Default*__: true
**cdkDependencies**?🔹 | <code>Array<string></code> | Which AWS CDK modules (those that start with "@aws-cdk/") does this library require when consumed?<br/>__*Optional*__
**cdkDependenciesAsDeps**?🔹 | <code>boolean</code> | If this is enabled (default), all modules declared in `cdkDependencies` will be also added as normal `dependencies` (as well as `peerDependencies`).<br/>__*Default*__: true
**cdkTestDependencies**?🔹 | <code>Array<string></code> | AWS CDK modules required for testing.<br/>__*Optional*__
**cdkVersionPinning**?🔹 | <code>boolean</code> | Use pinned version instead of caret version for CDK.<br/>__*Default*__: false
**clobber**?🔹 | <code>boolean</code> | Add a `clobber` task which resets the repo to origin.<br/>__*Default*__: true
Expand Down Expand Up @@ -6909,6 +6914,7 @@ Name | Type | Description
**catalog**?⚠️ | <code>[Catalog](#projen-catalog)</code> | Libraries will be picked up by the construct catalog when they are published to npm as jsii modules and will be published under:.<br/>__*Default*__: new version will be announced
**cdkAssert**?⚠️ | <code>boolean</code> | Install the @aws-cdk/assert library?<br/>__*Default*__: true
**cdkDependencies**?⚠️ | <code>Array<string></code> | Which AWS CDK modules (those that start with "@aws-cdk/") does this library require when consumed?<br/>__*Optional*__
**cdkDependenciesAsDeps**?⚠️ | <code>boolean</code> | If this is enabled (default), all modules declared in `cdkDependencies` will be also added as normal `dependencies` (as well as `peerDependencies`).<br/>__*Default*__: true
**cdkTestDependencies**?⚠️ | <code>Array<string></code> | AWS CDK modules required for testing.<br/>__*Optional*__
**cdkVersionPinning**?⚠️ | <code>boolean</code> | Use pinned version instead of caret version for CDK.<br/>__*Default*__: false
**clobber**?⚠️ | <code>boolean</code> | Add a `clobber` task which resets the repo to origin.<br/>__*Default*__: true
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/inventory.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,18 @@ Array [
"switch": "cdk-dependencies",
"type": "unknown",
},
Object {
"default": "true",
"docs": "If this is enabled (default), all modules declared in \`cdkDependencies\` will be also added as normal \`dependencies\` (as well as \`peerDependencies\`).",
"name": "cdkDependenciesAsDeps",
"optional": true,
"parent": "AwsCdkConstructLibraryOptions",
"path": Array [
"cdkDependenciesAsDeps",
],
"switch": "cdk-dependencies-as-deps",
"type": "boolean",
},
Object {
"docs": "AWS CDK modules required for testing.",
"name": "cdkTestDependencies",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/new.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const project = new AwsCdkConstructLibrary({
/* AwsCdkConstructLibraryOptions */
// cdkAssert: true, /* Install the @aws-cdk/assert library? */
// cdkDependencies: undefined, /* Which AWS CDK modules (those that start with \\"@aws-cdk/\\") does this library require when consumed? */
// cdkDependenciesAsDeps: true, /* If this is enabled (default), all modules declared in \`cdkDependencies\` will be also added as normal \`dependencies\` (as well as \`peerDependencies\`). */
// cdkTestDependencies: undefined, /* AWS CDK modules required for testing. */
// cdkVersionPinning: false, /* Use pinned version instead of caret version for CDK. */

Expand Down
29 changes: 27 additions & 2 deletions src/awscdk-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export interface AwsCdkConstructLibraryOptions extends ConstructLibraryOptions {
*/
readonly cdkDependencies?: string[];

/**
* If this is enabled (default), all modules declared in `cdkDependencies` will be also added as
* normal `dependencies` (as well as `peerDependencies`).
*
* This is to ensure that downstream consumers actually have your CDK dependencies installed
* when using npm < 7 or yarn, where peer dependencies are not automatically installed.
* If this is disabled, `cdkDependencies` will be added to `devDependencies` to ensure
* they are present during development.
*
* @default true
*/
readonly cdkDependenciesAsDeps?: boolean;

/**
* Install the @aws-cdk/assert library?
* @default true
Expand Down Expand Up @@ -112,6 +125,11 @@ export class AwsCdkConstructLibrary extends ConstructLibrary {
*/
public readonly version: string;

/**
* Whether CDK dependencies are added as normal dependencies (and peer dependencies).
*/
public readonly cdkDependenciesAsDeps: boolean
eladb marked this conversation as resolved.
Show resolved Hide resolved

constructor(options: AwsCdkConstructLibraryOptions) {
super({
...options,
Expand All @@ -121,6 +139,7 @@ export class AwsCdkConstructLibrary extends ConstructLibrary {
});

this.version = options.cdkVersionPinning ? options.cdkVersion : `^${options.cdkVersion}`;
this.cdkDependenciesAsDeps = options.cdkDependenciesAsDeps ?? true;

this.addPeerDeps('constructs@^3.2.27');

Expand All @@ -135,19 +154,25 @@ export class AwsCdkConstructLibrary extends ConstructLibrary {
/**
* Adds CDK modules as runtime dependencies.
*
* Modules are currently added with a caret CDK version both as "dependencies"
* Modules are currently by default added with a caret CDK version both as "dependencies"
* and "peerDependencies". This is because currently npm would not
* automatically install peer dependencies that are not declared as concerete
* dependencies by the consumer, so this is a little npm "hack" so that
* consumers will not need to depend on them directly if they don't interact
* with them.
* See `cdkDependenciesAsDeps` for changing the default behavior.
*
* @param deps names of cdk modules (e.g. `@aws-cdk/aws-lambda`).
*/
public addCdkDependencies(...deps: string[]) {
// this ugliness will go away in cdk v2.0
this.addPeerDeps(...deps.map(m => this.formatModuleSpec(m)));
this.addDeps(...deps.map(m => this.formatModuleSpec(m)));

if (this.cdkDependenciesAsDeps) {
this.addDeps(...deps.map(m => this.formatModuleSpec(m)));
} else {
this.addDevDeps(...deps.map(m => this.formatModuleSpec(m)));
}
}

/**
Expand Down