-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
178 lines (144 loc) · 4.49 KB
/
main.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
import 'reflect-metadata';
import './env';
import { existsSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { buildTerraformCloudStack } from '@support/cloud';
import {
buildConstructs,
files,
loadConstructs,
loadFunctions,
resolve,
workspaceNameAndProject,
} from '@support/functions';
import {
CURRENT_STACK,
PARENT_SCOPE,
SCOPE_ID,
STACK_VARIABLES,
} from '@support/symbols';
import { StackDetails } from '@support/types';
import {
App,
CloudBackend,
LocalBackend,
NamedCloudWorkspace,
TerraformProvider,
TerraformStack,
} from 'cdktf';
import { Construct } from 'constructs';
// @ts-expect-error typescript files dont exist...
import * as printTree from 'print-tree';
import { kebabCase } from 'scule';
import { container } from 'tsyringe';
process.env.BASE_DIRECTORY = __dirname;
async function main(app: App): Promise<string[]> {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv-expand').expand(require('dotenv').config());
const stacks = new Map<string, StackDetails>();
for (const file of files('stacks/**/stack.ts')) {
const dir = dirname(file);
const childContainer = container.createChildContainer();
for (const cls of loadConstructs(file)) {
const stackId = kebabCase(cls.name);
childContainer.registerInstance(PARENT_SCOPE, app);
childContainer.register(SCOPE_ID, { useValue: stackId });
const instance = resolve(childContainer, cls) as TerraformStack;
const details: StackDetails = {
path: dir,
stack: instance,
};
container.registerInstance(`stack:${stackId}`, instance);
container.registerInstance(cls, instance);
childContainer.registerInstance(CURRENT_STACK, instance);
if (existsSync(join(dir, 'variables.ts'))) {
for (const cls of loadConstructs(join(dir, 'variables.ts'))) {
const vars = new cls(instance, 'vars');
childContainer.registerInstance(cls, vars);
childContainer.registerInstance(STACK_VARIABLES, vars);
break;
}
}
if (existsSync(join(dir, 'provider.ts'))) {
for (const fn of loadFunctions<
(stack: TerraformStack) => TerraformProvider
>(join(dir, 'provider.ts'))) {
const provider = fn(instance);
childContainer.registerInstance(
`provider:${provider.node.id}`,
provider,
);
}
}
await buildConstructs(
join(dir, 'constructs'),
childContainer,
childContainer,
instance,
'/',
);
if (existsSync(join(dir, 'backend.ts'))) {
details.backend = {
functions: loadFunctions(join(dir, 'backend.ts')),
};
}
if (Reflect.hasMetadata('tfe:workspace', instance.constructor)) {
details.workspace = {
functions: existsSync(join(dir, 'workspace.ts'))
? loadFunctions(join(dir, 'workspace.ts'))
: [],
};
}
stacks.set(stackId, details);
break;
}
// await childContainer.dispose();
}
let buildTfCloudStack = false;
for (const [_, details] of stacks.entries()) {
if (details.backend) {
for (const fn of details.backend.functions) {
fn(details.stack);
break;
}
continue;
}
if (details.workspace && process.env.TFE_ORGANIZATION) {
const [name, project] = workspaceNameAndProject(details.stack);
new CloudBackend(details.stack, {
organization: process.env.TFE_ORGANIZATION,
workspaces: new NamedCloudWorkspace(`${name}-${project}`, project),
});
buildTfCloudStack = true;
continue;
}
new LocalBackend(details.stack);
}
if (buildTfCloudStack) {
buildTerraformCloudStack(app, stacks);
}
return Array.from(stacks.keys());
}
(async (app: App) => {
const stacks = await main(app);
return { app, stacks };
})(new App()).then(({ app, stacks }) => {
if (process.env.CDKTF_OUTDIR) {
app.synth();
} else if (process.env.CI) {
process.stdout.write(`${JSON.stringify(stacks)}\n`, 'utf-8');
} else {
printTree(
app,
(scope: Construct) => {
let name = scope.constructor.name;
if (!name || name.startsWith('default_')) {
name = Object.getPrototypeOf(scope.constructor).name;
}
return `${scope.node.id} (${name})`.trim();
},
// @ts-expect-error just trying to hijack...
(scope: Construct) => Object.values(scope.node._children),
);
}
});