-
Notifications
You must be signed in to change notification settings - Fork 9
/
.projenrc.ts
197 lines (188 loc) · 5.45 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
import * as fs from "fs";
import * as path from "path";
import { Link, Node } from "@npmcli/arborist";
import { awscdk } from "projen";
import { AwsCdkTypeScriptApp } from "projen/lib/awscdk";
import { NodePackageManager, NodeProject } from "projen/lib/javascript";
const Arborist = require("@npmcli/arborist");
const app = async (): Promise<AwsCdkTypeScriptApp> => {
const project = new awscdk.AwsCdkTypeScriptApp({
cdkVersion: "2.105.0",
defaultReleaseBranch: "main",
name: "aws-organizations-tag-inventory",
projenrcTs: true,
packageManager: NodePackageManager.NPM,
gitignore: [
".idea",
"*.iml",
".DS_Store",
"repolinter.txt",
".$architecture.drawio.*",
"*.output.txt",
"QuestionsQuicksightShouldAnswer.md",
"definition.json",
],
license: "MIT-0",
github: false,
eslintOptions: {
dirs: ["src"],
ignorePatterns: ["cli.ts"],
prettier: true,
},
prettierOptions: {
settings: {
printWidth: 160,
},
},
copyrightOwner: "Amazon.com, Inc. or its affiliates. All Rights Reserved.",
deps: [
"@types/aws-lambda",
"@aws-sdk/client-resource-explorer-2",
"uuid",
"@aws-sdk/client-athena",
"@aws-sdk/client-s3",
"@aws-lambda-powertools/logger",
"cdk-nag",
] /* Runtime dependencies of this module. */,
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
devDeps: [
"@types/uuid",
"@npmcli/arborist",
"@types/npm-packlist",
"@types/npmcli__arborist",
"cdk-assets",
"@aws-sdk/client-organizations",
"prompts",
"@types/prompts",
"@aws-sdk/client-ec2",
"@smithy/shared-ini-file-loader",
"@aws-sdk/credential-providers",
"@aws-sdk/client-sts",
"@aws-sdk/client-iam",
"@aws-sdk/client-quicksight",
"@aws-sdk/client-ssm",
"kleur",
] /* Build dependencies for this module. */,
// packageName: undefined, /* The "name" in package.json. */
});
await addZipLayerTask(project, [
"@aws-lambda-powertools",
"aws-cdk-lib",
"constructs",
"@types",
]);
/**
* This function walks the package.json for the project and zips all production dependencies for use in a lambda layer
* @param p
* @param exclude
*/
async function addZipLayerTask(p: NodeProject, exclude: string[] = []) {
const steps = [
{
exec: "rm -Rf ./dist",
},
{
exec: `rm -Rf /tmp/${p.name}`,
},
{
exec: `mkdir -p /tmp/${p.name}/nodejs/node_modules/${p.package.packageName}`,
},
];
const a = new Arborist({ path: p.outdir });
const depNames: string[] = [];
const excludeDeps = (depName: string) => {
for (const ex of exclude) {
if (depName.startsWith(ex)) {
return true;
}
}
return false;
};
const recursivelyGetDependencies = (node: Node | Link) => {
if (node.package.dependencies != undefined) {
for (const depName of Object.keys(node.package.dependencies)) {
if (depNames.indexOf(depName) == -1) {
let d = node.children.get(depName);
if (d == undefined) {
d = node.root.inventory.get(`node_modules/${depName}`);
}
if (d != undefined && !d.dev && !excludeDeps(d.name)) {
const relativePath = path.relative(p.outdir, d.path);
if (fs.existsSync(relativePath)) {
steps.push({
exec: `mkdir -p /tmp/${p.name}/nodejs/node_modules/${depName}`,
});
steps.push({
exec: `cp -R ${relativePath}/* /tmp/${p.name}/nodejs/node_modules/${depName}`,
});
}
recursivelyGetDependencies(d);
}
depNames.push(depName);
}
}
}
};
//@ts-ignore
const tree = await a.loadActual();
recursivelyGetDependencies(tree);
steps.push({
exec: `mkdir ./dist;(cd /tmp/${p.name} && zip -r /${p.outdir}/dist/${p.name}-layer.zip ./nodejs)`,
});
p.addTask("zip-layer", {
cwd: p.outdir,
steps: steps,
});
p.tasks.tryFind("compile")?.exec("npm run zip-layer");
}
return project;
};
app()
.then((project) => {
project.tasks.addTask("cli", {
exec: "npx ts-node -P tsconfig.json --prefer-ts-exts src/cli.ts",
});
project.tasks.tryFind("post-compile")?.reset();
project.tasks.tryFind("synth")?.reset("cdk synth", {
receiveArgs: true,
});
project.tasks.tryFind("synth:silent")?.reset("cdk synth -q", {
receiveArgs: true,
});
project.tasks.tryFind("deploy")?.reset("cdk deploy", {
receiveArgs: true,
});
project.tasks.addTask("deploy:cli", {
steps: [
{
spawn: "default",
},
{
spawn: "pre-compile",
},
{
spawn: "compile",
},
{
spawn: "post-compile",
},
{
spawn: "synth:silent",
receiveArgs: true,
},
{
exec: "cdk deploy",
receiveArgs: true,
},
],
receiveArgs: true,
});
// project.tasks.tryFind('deploy')?.prependExec('cdk synth', {
// receiveArgs: true,
// });
project.tasks.tryFind("deploy")?.prependExec("npm run build", {});
project.synth();
})
.catch((reason) => {
throw new Error(reason);
});