Skip to content

Commit

Permalink
feat(AwsCdkConstruct): make cdkDependencies behavior configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Miradorn committed Mar 1, 2021
1 parent cadbc19 commit f8fb1e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
8 changes: 7 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ new AwsCdkConstructLibrary(options: AwsCdkConstructLibraryOptions)
* **sampleCode** (<code>boolean</code>) Generate one-time sample in `src/` and `test/` if there are no files there. __*Default*__: true
* **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:. __*Default*__: new version will be announced
* **cdkVersion** (<code>string</code>) Minimum target version this library is tested against.
* **addCdkDependenciesToDeps** (<code>boolean</code>) Should `cdkDependencies` be added to package.json `dependencies`. __*Default*__: true
* **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*__
* **cdkTestDependencies** (<code>Array<string></code>) AWS CDK modules required for testing. __*Optional*__
Expand All @@ -430,6 +431,7 @@ new AwsCdkConstructLibrary(options: AwsCdkConstructLibraryOptions)

Name | Type | Description
-----|------|-------------
**addCdkDependenciesToDeps**🔹 | <code>boolean</code> | <span></span>
**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 `addCdkDependenciesToDeps` for changing the default behavior.

```ts
addCdkDependencies(...deps: string[]): void
Expand Down Expand Up @@ -912,6 +915,7 @@ new ConstructLibraryAws(options: AwsCdkConstructLibraryOptions)
* **sampleCode** (<code>boolean</code>) Generate one-time sample in `src/` and `test/` if there are no files there. __*Default*__: true
* **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:. __*Default*__: new version will be announced
* **cdkVersion** (<code>string</code>) Minimum target version this library is tested against.
* **addCdkDependenciesToDeps** (<code>boolean</code>) Should `cdkDependencies` be added to package.json `dependencies`. __*Default*__: true
* **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*__
* **cdkTestDependencies** (<code>Array<string></code>) AWS CDK modules required for testing. __*Optional*__
Expand Down Expand Up @@ -6658,6 +6662,7 @@ Name | Type | Description
**defaultReleaseBranch**🔹 | <code>string</code> | The name of the main release branch.
**name**🔹 | <code>string</code> | This is the name of your project.
**repositoryUrl**🔹 | <code>string</code> | Git repository URL.
**addCdkDependenciesToDeps**?🔹 | <code>boolean</code> | Should `cdkDependencies` be added to package.json `dependencies`.<br/>__*Default*__: true
**allowLibraryDependencies**?🔹 | <code>boolean</code> | Allow the project to include `peerDependencies` and `bundledDependencies`.<br/>__*Default*__: true
**antitamper**?🔹 | <code>boolean</code> | Checks that after build there are no modified files on git.<br/>__*Default*__: true
**artifactsDirectory**?🔹 | <code>string</code> | A directory which will contain artifacts to be published to npm.<br/>__*Default*__: "dist"
Expand Down Expand Up @@ -6895,6 +6900,7 @@ Name | Type | Description
**defaultReleaseBranch**⚠️ | <code>string</code> | The name of the main release branch.
**name**⚠️ | <code>string</code> | This is the name of your project.
**repositoryUrl**⚠️ | <code>string</code> | Git repository URL.
**addCdkDependenciesToDeps**?⚠️ | <code>boolean</code> | Should `cdkDependencies` be added to package.json `dependencies`.<br/>__*Default*__: true
**allowLibraryDependencies**?⚠️ | <code>boolean</code> | Allow the project to include `peerDependencies` and `bundledDependencies`.<br/>__*Default*__: true
**antitamper**?⚠️ | <code>boolean</code> | Checks that after build there are no modified files on git.<br/>__*Default*__: true
**artifactsDirectory**?⚠️ | <code>string</code> | A directory which will contain artifacts to be published to npm.<br/>__*Default*__: "dist"
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 @@ -1173,6 +1173,18 @@ Array [
"fqn": "projen.AwsCdkConstructLibrary",
"moduleName": "projen",
"options": Array [
Object {
"default": "true",
"docs": "Should \`cdkDependencies\` be added to package.json \`dependencies\`.",
"name": "addCdkDependenciesToDeps",
"optional": true,
"parent": "AwsCdkConstructLibraryOptions",
"path": Array [
"addCdkDependenciesToDeps",
],
"switch": "add-cdk-dependencies-to-deps",
"type": "boolean",
},
Object {
"default": "true",
"docs": "Allow the project to include \`peerDependencies\` and \`bundledDependencies\`.",
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 @@ -130,6 +130,7 @@ const project = new AwsCdkConstructLibrary({
repositoryUrl: 'git@boom.com:foo/bar.git',
/* AwsCdkConstructLibraryOptions */
// addCdkDependenciesToDeps: true, /* Should \`cdkDependencies\` be added to package.json \`dependencies\`. */
// 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? */
// cdkTestDependencies: undefined, /* AWS CDK modules required for testing. */
Expand Down
25 changes: 23 additions & 2 deletions src/awscdk-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ 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, since theese don't install peerDependencies by default.
* If `false` `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 +124,8 @@ export class AwsCdkConstructLibrary extends ConstructLibrary {
*/
public readonly version: string;

public readonly cdkDependenciesAsDeps: boolean

constructor(options: AwsCdkConstructLibraryOptions) {
super({
...options,
Expand All @@ -121,6 +135,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 +150,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

0 comments on commit f8fb1e6

Please sign in to comment.