/
awscdk-construct.ts
146 lines (130 loc) · 4.44 KB
/
awscdk-construct.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as semver from "semver";
import { AutoDiscover } from "./auto-discover";
import { AwsCdkDeps, AwsCdkDepsCommonOptions } from "./awscdk-deps";
import { AwsCdkDepsJs } from "./awscdk-deps-js";
import { LambdaFunctionCommonOptions } from "./lambda-function";
import { ConstructLibrary, ConstructLibraryOptions } from "../cdk";
import { DependencyType } from "../dependencies";
/**
* Options for `AwsCdkConstructLibrary`.
*/
export interface AwsCdkConstructLibraryOptions
extends ConstructLibraryOptions,
AwsCdkDepsCommonOptions {
/**
* Automatically adds an `aws_lambda.Function` for each `.lambda.ts` handler
* in your source tree. If this is disabled, you either need to explicitly
* call `aws_lambda.Function.autoDiscover()` or define a `new
* aws_lambda.Function()` for each handler.
*
* @default true
*/
readonly lambdaAutoDiscover?: boolean;
/**
* Automatically adds an `cloudfront.experimental.EdgeFunction` for each
* `.edge-lambda.ts` handler in your source tree. If this is disabled, you can
* manually add an `awscdk.AutoDiscover` component to your project.
*
* @default true
*/
readonly edgeLambdaAutoDiscover?: boolean;
/**
* Automatically adds an `awscdk.LambdaExtension` for each `.lambda-extension.ts`
* entrypoint in your source tree. If this is disabled, you can manually add an
* `awscdk.AutoDiscover` component to your project
*
* @default true
*/
readonly lambdaExtensionAutoDiscover?: boolean;
/**
* Automatically discovers and creates integration tests for each `.integ.ts`
* file in under your test directory.
*
* @default true
*/
readonly integrationTestAutoDiscover?: boolean;
/**
* Common options for all AWS Lambda functions.
*
* @default - default options
*/
readonly lambdaOptions?: LambdaFunctionCommonOptions;
}
/**
* AWS CDK construct library project
*
* A multi-language (jsii) construct library which vends constructs designed to
* use within the AWS CDK with a friendly workflow and automatic publishing to
* the construct catalog.
*
* @pjid awscdk-construct
*/
export class AwsCdkConstructLibrary extends ConstructLibrary {
public readonly cdkDeps: AwsCdkDeps;
constructor(options: AwsCdkConstructLibraryOptions) {
const cdkMajorVersion = semver.parse(options.cdkVersion)?.major;
super({
peerDependencyOptions:
cdkMajorVersion === 1
? {
pinnedDevDependency: false,
}
: undefined,
workflowNodeVersion: options.minNodeVersion ?? "16.x",
...options,
});
this.cdkDeps = new AwsCdkDepsJs(this, {
// since this we are a library, dependencies should be added a peers
dependencyType: DependencyType.PEER,
...options,
});
new AutoDiscover(this, {
srcdir: this.srcdir,
testdir: this.testdir,
lambdaOptions: options.lambdaOptions,
tsconfigPath: this.tsconfigDev.fileName,
cdkDeps: this.cdkDeps,
lambdaAutoDiscover: options.lambdaAutoDiscover ?? true,
edgeLambdaAutoDiscover: options.edgeLambdaAutoDiscover ?? true,
lambdaExtensionAutoDiscover: options.lambdaExtensionAutoDiscover ?? true,
integrationTestAutoDiscover: options.integrationTestAutoDiscover ?? true,
});
}
/**
* The target CDK version for this library.
*/
public get cdkVersion() {
return this.cdkDeps.cdkVersion;
}
/**
* @deprecated use `cdkVersion`
*/
public get version() {
return this.cdkVersion;
}
/**
* Adds dependencies to AWS CDK modules.
*
* Since this is a library project, dependencies will be added as peer dependencies.
*
* @param deps names of cdk modules (e.g. `@aws-cdk/aws-lambda`).
* @deprecated Not supported in v2. For v1, use `project.cdkDeps.addV1Dependencies()`
*/
public addCdkDependencies(...deps: string[]) {
return this.cdkDeps.addV1Dependencies(...deps);
}
/**
* Adds AWS CDK modules as dev dependencies.
*
* @param deps names of cdk modules (e.g. `@aws-cdk/aws-lambda`).
* @deprecated Not supported in v2. For v1, use `project.cdkDeps.addV1DevDependencies()`
*/
public addCdkTestDependencies(...deps: string[]) {
return this.cdkDeps.addV1DevDependencies(...deps);
}
}
/** @deprecated use `AwsCdkConstructLibraryOptions` */
export interface ConstructLibraryAwsOptions
extends AwsCdkConstructLibraryOptions {}
/** @deprecated use `AwsCdkConstructLibrary` */
export class ConstructLibraryAws extends AwsCdkConstructLibrary {}