-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.projenrc.ts
385 lines (378 loc) · 12.7 KB
/
.projenrc.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { join } from "node:path";
import { ProjenStruct, Struct } from "@mrgrain/jsii-struct-builder";
import { awscdk, javascript, ReleasableCommits } from "projen";
import { LambdaRuntime } from "projen/lib/awscdk";
import { JobStep } from "projen/lib/github/workflows-model";
import { UpgradeDependenciesSchedule } from "projen/lib/javascript";
const nodeVersion = 22;
const project = new awscdk.AwsCdkConstructLibrary({
// repository config
author: "Ben Stickley",
authorAddress: "[email protected]",
defaultReleaseBranch: "main",
repositoryUrl: "https://github.com/cdklabs/cdk-nextjs.git",
depsUpgradeOptions: {
workflowOptions: {
schedule: UpgradeDependenciesSchedule.WEEKLY,
},
},
// package.json config
name: "cdk-nextjs",
description:
"Deploy Next.js apps on AWS with CDK" /* The description is just a string that helps people understand the purpose of the package. */,
// majorVersion: 1,
// prerelease: "beta",
keywords: ["nextjs", "next", "next.js", "aws-cdk", "aws", "cdk"],
cdkVersion: "2.177.0",
jsiiVersion: "~5.5.0",
packageManager: javascript.NodePackageManager.PNPM,
pnpmVersion: "9",
projenVersion: "^0.90.6",
devDeps: [
"@aws-crypto/sha256-js",
"@aws-sdk/client-sqs",
"@aws-sdk/client-s3",
"@aws-sdk/lib-storage",
"@mrgrain/jsii-struct-builder",
"@smithy/signature-v4",
"@types/aws-lambda",
"@types/mime-types",
"@types/node@^20",
"cdk-nag",
"esbuild",
"mime-types",
"next", // bundled in src/nextjs-build/cache-handler.ts
"undici",
],
npmIgnoreOptions: {
ignorePatterns: ["examples/**/*"],
},
// tooling config
lambdaOptions: {
runtime: new LambdaRuntime(`nodejs${nodeVersion}.x`, `node${nodeVersion}`),
awsSdkConnectionReuse: false, // doesn't exist in AWS SDK JS v3
},
projenCommand: "pnpm dlx projen",
gitignore: [
".idea",
".DS_Store",
"cdk.out",
".env",
"*.drawio.bkp",
"ash_output",
"examples/.dockerignore",
"examples/builder.Dockerfile",
],
projenrcTs: true,
eslintOptions: {
prettier: true,
dirs: ["src"],
ignorePatterns: ["generated-structs/", "**/*-function.ts", "examples/"],
},
sampleCode: false,
releasableCommits: ReleasableCommits.ofType([
"feat",
"fix",
"chore",
"refactor",
"perf",
]),
githubOptions: {
pullRequestLintOptions: {
semanticTitleOptions: {
types: [
// see commit types here: https://www.conventionalcommits.org/en/v1.0.0/#summary
"feat",
"fix",
"chore",
"refactor",
"perf",
"docs",
"style",
"test",
"build",
"ci",
],
},
},
},
versionrcOptions: {
types: [
{ type: "feat", section: "Features" },
{ type: "fix", section: "Bug Fixes" },
{ type: "chore", section: "Chores" },
{ type: "docs", section: "Docs" },
{ type: "style", hidden: true },
{ type: "refactor", hidden: true },
{ type: "perf", section: "Performance" },
{ type: "test", hidden: true },
],
},
});
// by default projen ignores all tsconfigs, but we don't want do this for non-projen
// managed repo.
project.gitignore.addPatterns("!/examples/**/tsconfig.json"); // must call method, cannot set in initial props
copyDockerfiles();
bundle();
updateGitHubWorkflows();
generateStructs();
project.synth();
function bundle() {
const target = `node${nodeVersion}`;
project.bundler.addBundle("src/nextjs-build/cache-handler.ts", {
platform: "node",
target,
outfile: "../../../lib/nextjs-build/cache-handler.cjs",
});
project.bundler.addBundle("src/nextjs-build/add-cache-handler.ts", {
platform: "node",
target,
outfile: "../../../lib/nextjs-build/add-cache-handler.mjs",
format: "esm",
});
project.bundler.addBundle("src/lambdas/assets-deployment/patch-fetch.js", {
platform: "browser",
// https://nextjs.org/docs/architecture/supported-browsers
target: "chrome64,firefox67,safari12,edge79",
minify: true,
outfile: "../assets-deployment.lambda/patch-fetch.js",
});
project.bundler.addBundle("src/nextjs-build/symlink.ts", {
platform: "node",
target,
outfile: "../../../lib/nextjs-build/symlink.mjs",
format: "esm",
});
}
function copyDockerfiles() {
const bundleTask = project.tasks.tryFind("bundle");
if (bundleTask) {
bundleTask.exec(`mkdir -p ${join("lib", "nextjs-build")}`);
bundleTask.exec(
`cp ${join("src", "nextjs-build", "assets-deployment.Dockerfile")} ${join("assets", "lambdas", "assets-deployment", "assets-deployment.lambda")}`,
);
bundleTask.exec(
`cp ${join("src", "nextjs-build", "builder.Dockerfile")} ${join("lib", "nextjs-build")}`,
);
bundleTask.exec(
`cp ${join("src", "nextjs-build", "global-containers.Dockerfile")} ${join("lib", "nextjs-build")}`,
);
bundleTask.exec(
`cp ${join("src", "nextjs-build", "global-functions.Dockerfile")} ${join("lib", "nextjs-build")}`,
);
bundleTask.exec(
`cp ${join("src", "nextjs-build", "regional-containers.Dockerfile")} ${join("lib", "nextjs-build")}`,
);
}
}
/**
* @mrgrain/jsii-struct-builder is also used to generate optional structs of code
* within this repository (OptionalNextjsBucketDeploymentProps, etc.). In order
* for @mrgrain/jsii-struct-builder to read the source code struct to create a
* generate struct with optional properties, the JSII assembly must exist. If
* you simply run projen build this would fail because the JSII assembly of the
* source code hasn't been created yet. We can get around this issue by running
* projen compile first to create the JSII assembly, then projen build to use
* @mrgrain/jsii-struct-builder to create the optional version of the struct.
* The .projenrc.ts patches the build GitHub Workflow and Job to compile then
* build. See more here.
*/
function updateGitHubWorkflows() {
// .github/workflows/build.yml
const buildWorkflow = project.github?.tryFindWorkflow("build");
if (!buildWorkflow) return;
const buildJob = buildWorkflow.getJob("build");
if (!buildJob || !("steps" in buildJob)) return;
// TODO: figure out why wrong types
const getBuildSteps = buildJob.steps as unknown as () => JobStep[];
const buildJobSteps = getBuildSteps();
buildWorkflow.updateJob("build", {
...buildJob,
steps: [
...buildJobSteps.slice(0, 4),
{
name: "Compile JSII",
run: `pnpm projen compile`,
},
...buildJobSteps.slice(4),
],
});
// .github/workflows/release.yml
const releaseWorkflow = project.github?.tryFindWorkflow("release");
if (!releaseWorkflow) return;
const releaseJob = releaseWorkflow.getJob("release");
if (!releaseJob || !("steps" in releaseJob)) return;
const releaseJobSteps = releaseJob.steps;
releaseWorkflow.updateJob("release", {
...releaseJob,
steps: [
...releaseJobSteps.slice(0, 5),
{
name: "Compile JSII",
run: `pnpm projen compile`,
},
...releaseJobSteps.slice(5),
],
});
// .github/workflows/upgrade-main.yml
const upgradeMainWorkflow = project.github?.tryFindWorkflow("upgrade-main");
if (!upgradeMainWorkflow) return;
const upgradeJob = upgradeMainWorkflow.getJob("upgrade");
if (!upgradeJob || !("steps" in upgradeJob)) return;
const upgradeJobSteps = upgradeJob.steps;
upgradeMainWorkflow.updateJob("upgrade", {
...upgradeJob,
steps: [
...upgradeJobSteps.slice(0, 4),
{
name: "Compile JSII",
run: `pnpm projen compile`,
},
...upgradeJobSteps.slice(4),
],
});
}
/**
* When you want to reuse interfaces/structs from the AWS CDK library and
* customize them so all of their properties are optional, you cannot simply use
* the TypeScript utility type, [Partial](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype),
* because of the TypeScript [limitations](https://aws.github.io/jsii/user-guides/lib-author/typescript-restrictions/#typescript-mapped-types)
* of JSII. To solve this problem, this construct library uses
* [@mrgrain/jsii-struct-builder](https://github.com/mrgrain/jsii-struct-builder)
* to generate partial types. These types are defined in the .projenrc.ts files
* (you'll need to scroll down to see them) and are primarily used in
* NextjsOverrides. They files are in the src/generated-structs folder.
*
* Note, sometimes you might need to delete .jsii file to reset
*/
function generateStructs() {
const getFilePath = (fileName: string) =>
"src/generated-structs/" + fileName + ".ts";
new ProjenStruct(project, {
name: "OptionalS3OriginBucketWithOACProps",
filePath: getFilePath("OptionalS3OriginBucketWithOACProps"),
})
.mixin(
Struct.fromFqn(
"aws-cdk-lib.aws_cloudfront_origins.S3BucketOriginWithOACProps",
),
)
.allOptional();
new ProjenStruct(project, {
name: "OptionalEdgeFunctionProps",
filePath: getFilePath("OptionalEdgeFunctionProps"),
})
.mixin(
Struct.fromFqn(
"aws-cdk-lib.aws_cloudfront.experimental.EdgeFunctionProps",
),
)
.allOptional();
new ProjenStruct(project, {
name: "OptionalCloudFrontFunctionProps",
filePath: getFilePath("OptionalCloudFrontFunctionProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_cloudfront.FunctionProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalDistributionProps",
filePath: getFilePath("OptionalDistributionProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_cloudfront.DistributionProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalClusterProps",
filePath: getFilePath("OptionalClusterProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_ecs.ClusterProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalDockerImageAssetProps",
filePath: getFilePath("OptionalDockerImageAssetProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_ecr_assets.DockerImageAssetProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsBuildProps",
filePath: getFilePath("OptionalNextjsBuildProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsBuildProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsDistributionProps",
filePath: getFilePath("OptionalNextjsDistributionProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsDistributionProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsVpcProps",
filePath: getFilePath("OptionalNextjsVpcProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsVpcProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsFileSystemProps",
filePath: getFilePath("OptionalNextjsFileSystemProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsFileSystemProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsAssetsDeploymentProps",
filePath: getFilePath("OptionalNextjsAssetsDeploymentProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsAssetsDeploymentProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsContainersProps",
filePath: getFilePath("OptionalNextjsContainersProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsContainersProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalNextjsInvalidationProps",
filePath: getFilePath("OptionalNextjsInvalidationProps"),
})
.mixin(Struct.fromFqn("cdk-nextjs.NextjsInvalidationProps"))
.omit("overrides")
.allOptional();
new ProjenStruct(project, {
name: "OptionalApplicationLoadBalancedTaskImageOptions",
filePath: getFilePath("OptionalApplicationLoadBalancedTaskImageOptions"),
})
.mixin(
Struct.fromFqn(
"aws-cdk-lib.aws_ecs_patterns.ApplicationLoadBalancedTaskImageOptions",
),
)
.allOptional();
new ProjenStruct(project, {
name: "OptionalVpcProps",
filePath: getFilePath("OptionalVpcProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_ec2.VpcProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalFunctionProps",
filePath: getFilePath("OptionalFunctionProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_lambda.FunctionProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalFunctionUrlProps",
filePath: getFilePath("OptionalFunctionUrlProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_lambda.FunctionUrlProps"))
.allOptional();
new ProjenStruct(project, {
name: "OptionalDockerImageFunctionProps",
filePath: getFilePath("OptionalDockerImageFunctionProps"),
})
.mixin(Struct.fromFqn("aws-cdk-lib.aws_lambda.DockerImageFunctionProps"))
.allOptional();
}