@solid-stack/builder 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -0
- package/README.md +161 -0
- package/dist/bin.cjs +805 -0
- package/dist/bin.cjs.map +1 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +795 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/commands/checkInfra.d.ts +2 -0
- package/dist/commands/checkInfra.d.ts.map +1 -0
- package/dist/commands/serviceUp.d.ts +3 -0
- package/dist/commands/serviceUp.d.ts.map +1 -0
- package/dist/core/compileEnvironment.d.ts +3 -0
- package/dist/core/compileEnvironment.d.ts.map +1 -0
- package/dist/core/templates.d.ts +3 -0
- package/dist/core/templates.d.ts.map +1 -0
- package/dist/errors/NotFoundError.d.ts +5 -0
- package/dist/errors/NotFoundError.d.ts.map +1 -0
- package/dist/errors/ScriptError.d.ts +4 -0
- package/dist/errors/ScriptError.d.ts.map +1 -0
- package/dist/index.cjs +814 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +792 -0
- package/dist/index.js.map +1 -0
- package/dist/templates/docker/docker-compose.base.yml +10 -0
- package/dist/templates/docker/docker-compose.dev.yml +11 -0
- package/dist/templates/docker/docker-compose.e2e.yml +9 -0
- package/dist/templates/docker/docker-compose.prod.yml +10 -0
- package/dist/templates/docker/docker-compose.standalone.yml +4 -0
- package/dist/templates/docker/docker-compose.test.yml +10 -0
- package/dist/types/index.d.ts +31 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/utils/checkDependencies.d.ts +2 -0
- package/dist/utils/checkDependencies.d.ts.map +1 -0
- package/dist/utils/extractBuildDeps.d.ts +3 -0
- package/dist/utils/extractBuildDeps.d.ts.map +1 -0
- package/dist/utils/extractOverrides.d.ts +3 -0
- package/dist/utils/extractOverrides.d.ts.map +1 -0
- package/dist/utils/getBuildJson.d.ts +3 -0
- package/dist/utils/getBuildJson.d.ts.map +1 -0
- package/dist/utils/ymlMods/addDependenciesToAppService.d.ts +3 -0
- package/dist/utils/ymlMods/addDependenciesToAppService.d.ts.map +1 -0
- package/dist/utils/ymlMods/attachName.d.ts +2 -0
- package/dist/utils/ymlMods/attachName.d.ts.map +1 -0
- package/dist/utils/ymlMods/makeE2eDependOnApp.d.ts +2 -0
- package/dist/utils/ymlMods/makeE2eDependOnApp.d.ts.map +1 -0
- package/dist/utils/ymlMods/renameServicesWithDependencies.d.ts +3 -0
- package/dist/utils/ymlMods/renameServicesWithDependencies.d.ts.map +1 -0
- package/package.json +75 -0
- package/templates/docker/docker-compose.base.yml +10 -0
- package/templates/docker/docker-compose.dev.yml +11 -0
- package/templates/docker/docker-compose.e2e.yml +9 -0
- package/templates/docker/docker-compose.prod.yml +10 -0
- package/templates/docker/docker-compose.standalone.yml +4 -0
- package/templates/docker/docker-compose.test.yml +10 -0
package/dist/bin.js
ADDED
|
@@ -0,0 +1,795 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { cac } from 'cac';
|
|
3
|
+
import pc2 from 'picocolors';
|
|
4
|
+
import { spawnSync } from 'child_process';
|
|
5
|
+
import fs, { existsSync, readFileSync, readdirSync } from 'fs';
|
|
6
|
+
import path5 from 'path';
|
|
7
|
+
import { parse, stringify } from 'yaml';
|
|
8
|
+
import z from 'zod';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
|
|
11
|
+
// src/errors/ScriptError.ts
|
|
12
|
+
var ScriptError = class extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "ScriptError";
|
|
16
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/commands/checkInfra.ts
|
|
21
|
+
function findServiceDefiners(projectDir, serviceName) {
|
|
22
|
+
const dirsToScan = [
|
|
23
|
+
path5.join(projectDir, ".docker/core"),
|
|
24
|
+
path5.join(projectDir, ".docker/mocks"),
|
|
25
|
+
path5.join(projectDir, ".docker/overrides")
|
|
26
|
+
];
|
|
27
|
+
const hits = [];
|
|
28
|
+
for (const dir of dirsToScan) {
|
|
29
|
+
if (!existsSync(dir)) continue;
|
|
30
|
+
for (const file of readdirSync(dir)) {
|
|
31
|
+
if (!file.endsWith(".yml") && !file.endsWith(".yaml")) continue;
|
|
32
|
+
const filePath = path5.join(dir, file);
|
|
33
|
+
const content = readFileSync(filePath, "utf-8");
|
|
34
|
+
const hasBlock = new RegExp(`^\\s{2}${serviceName}:\\s*$`, "m").test(content);
|
|
35
|
+
if (!hasBlock) continue;
|
|
36
|
+
const hasImageOrBuild = new RegExp(
|
|
37
|
+
`^\\s{2}${serviceName}:[\\s\\S]*?^\\s{4,}(image|build):`,
|
|
38
|
+
"m"
|
|
39
|
+
).test(content);
|
|
40
|
+
hits.push(
|
|
41
|
+
` - ${filePath}${hasImageOrBuild ? " (defines image/build)" : " (no image/build here)"}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return hits;
|
|
46
|
+
}
|
|
47
|
+
function explainComposeMergeFailure(projectDir, ctx, rawOutput) {
|
|
48
|
+
const lines = [];
|
|
49
|
+
const serviceMatch = rawOutput.match(
|
|
50
|
+
/service "([^"]+)" has neither an image nor a build context specified/
|
|
51
|
+
);
|
|
52
|
+
const envMissingMatch = /env file .* not found|no such file or directory.*\.env/i.test(rawOutput);
|
|
53
|
+
lines.push(`Environment : ${ctx.envName}`);
|
|
54
|
+
lines.push(`Compose files merged :`);
|
|
55
|
+
(ctx.filesUsed ?? []).filter((f) => f !== "-f").forEach((f) => lines.push(` - ${f}`));
|
|
56
|
+
lines.push("");
|
|
57
|
+
if (serviceMatch && serviceMatch[1]) {
|
|
58
|
+
const serviceName = serviceMatch[1];
|
|
59
|
+
lines.push(
|
|
60
|
+
`ROOT CAUSE: Service "${serviceName}" is referenced in the merged stack above, but none of those`
|
|
61
|
+
);
|
|
62
|
+
lines.push(
|
|
63
|
+
`files provide an "image:" or "build:" for it \u2014 Docker Compose cannot start it as-is.`
|
|
64
|
+
);
|
|
65
|
+
lines.push("");
|
|
66
|
+
lines.push(
|
|
67
|
+
`Every file under .docker/ that defines a "${serviceName}:" service block:`
|
|
68
|
+
);
|
|
69
|
+
const definers = findServiceDefiners(projectDir, serviceName);
|
|
70
|
+
if (definers.length > 0) {
|
|
71
|
+
definers.forEach((d) => lines.push(d));
|
|
72
|
+
} else {
|
|
73
|
+
lines.push(
|
|
74
|
+
` (none found \u2014 check spelling/indentation of "${serviceName}:" in your yml files)`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} else if (envMissingMatch) {
|
|
78
|
+
lines.push(`ROOT CAUSE: A referenced .env file could not be found.`);
|
|
79
|
+
lines.push("");
|
|
80
|
+
lines.push("HOW TO FIX:");
|
|
81
|
+
lines.push(
|
|
82
|
+
` - Create the missing env file at the project root (e.g. .env.dev, .env.prod, .env.test)`
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
lines.push(
|
|
86
|
+
"Docker Compose failed to resolve this merged stack. Common causes:"
|
|
87
|
+
);
|
|
88
|
+
lines.push(
|
|
89
|
+
` - A "\${VAR}" substitution with no default and no value set in the shell or an env_file.`
|
|
90
|
+
);
|
|
91
|
+
lines.push(` - A YAML syntax error in one of the files listed above.`);
|
|
92
|
+
lines.push(` - A duplicate or conflicting key across two merged files.`);
|
|
93
|
+
}
|
|
94
|
+
lines.push("");
|
|
95
|
+
lines.push("Raw Docker Compose / Conftest output:");
|
|
96
|
+
lines.push("-".repeat(60));
|
|
97
|
+
lines.push(rawOutput.trim());
|
|
98
|
+
lines.push("-".repeat(60));
|
|
99
|
+
return lines.join("\n");
|
|
100
|
+
}
|
|
101
|
+
function runStep(command, projectDir, ctx) {
|
|
102
|
+
const result = spawnSync(command, {
|
|
103
|
+
shell: true,
|
|
104
|
+
cwd: projectDir,
|
|
105
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
106
|
+
encoding: "utf-8"
|
|
107
|
+
});
|
|
108
|
+
const stdout = result.stdout ?? "";
|
|
109
|
+
const stderr = result.stderr ?? "";
|
|
110
|
+
if (stdout) process.stdout.write(stdout);
|
|
111
|
+
if (stderr) process.stderr.write(stderr);
|
|
112
|
+
if (result.status !== 0) {
|
|
113
|
+
console.error("\n" + "=".repeat(60));
|
|
114
|
+
console.error(pc2.red(`\u{1F6A8} INFRASTRUCTURE CHECK FAILED \u{1F6A8}`));
|
|
115
|
+
console.error("=".repeat(60));
|
|
116
|
+
console.error(`Failed Step : ${ctx.stepName}`);
|
|
117
|
+
if (ctx.targetFile) {
|
|
118
|
+
console.error(`Target File : ${ctx.targetFile} <-- FIX THIS FILE`);
|
|
119
|
+
}
|
|
120
|
+
console.error("=".repeat(60));
|
|
121
|
+
if (ctx.envName) {
|
|
122
|
+
console.error("\n" + explainComposeMergeFailure(projectDir, ctx, stdout + stderr));
|
|
123
|
+
}
|
|
124
|
+
console.error("");
|
|
125
|
+
throw new ScriptError(`Infrastructure check failed at step: ${ctx.stepName}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function checkInfra(projectDir = process.cwd()) {
|
|
129
|
+
const dockerfilePath = path5.join(projectDir, "Dockerfile");
|
|
130
|
+
if (!existsSync(dockerfilePath)) {
|
|
131
|
+
console.log(pc2.yellow("\u26A0\uFE0F No Dockerfile found in project directory. Skipping Dockerfile checks."));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
console.log(pc2.cyan("\n\u{1F50D} 1. Linting Dockerfile..."));
|
|
135
|
+
runStep(
|
|
136
|
+
"docker run --rm -i hadolint/hadolint hadolint --failure-threshold error - < Dockerfile",
|
|
137
|
+
projectDir,
|
|
138
|
+
{ stepName: "Dockerfile Linting", targetFile: "Dockerfile" }
|
|
139
|
+
);
|
|
140
|
+
const policyDockerfileDir = path5.join(projectDir, "policy/dockerfile");
|
|
141
|
+
if (existsSync(policyDockerfileDir)) {
|
|
142
|
+
console.log(pc2.cyan("\n\u{1F50D} 2. Validating Dockerfile Contract..."));
|
|
143
|
+
runStep(
|
|
144
|
+
`docker run --rm -v "${projectDir}:/project" -w /project openpolicyagent/conftest test Dockerfile -p policy/dockerfile/`,
|
|
145
|
+
projectDir,
|
|
146
|
+
{ stepName: "Dockerfile Contract", targetFile: "Dockerfile" }
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
console.log(pc2.green("\n\u2705 All infrastructure and contract checks passed.\n"));
|
|
150
|
+
}
|
|
151
|
+
var extractBuildDeps = (buildJson, projectDir = process.cwd()) => {
|
|
152
|
+
if (!buildJson || typeof buildJson !== "object") {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
if (!("dependencies" in buildJson) || !buildJson.dependencies) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
const schema = z.object({
|
|
159
|
+
dependencies: z.record(
|
|
160
|
+
z.string(),
|
|
161
|
+
z.object({
|
|
162
|
+
path: z.string(),
|
|
163
|
+
service: z.string()
|
|
164
|
+
})
|
|
165
|
+
)
|
|
166
|
+
});
|
|
167
|
+
const result = schema.safeParse(buildJson);
|
|
168
|
+
if (!result.success) {
|
|
169
|
+
throw new ScriptError(
|
|
170
|
+
`Invalid build.json structure for dependencies: ${result.error.message}`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
const dependenciesData = result.data.dependencies;
|
|
174
|
+
const dependencies = Object.entries(dependenciesData || {}).map(
|
|
175
|
+
([name, service]) => {
|
|
176
|
+
return {
|
|
177
|
+
path: path5.resolve(projectDir, service.path),
|
|
178
|
+
name,
|
|
179
|
+
serviceName: service.service
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
return dependencies;
|
|
184
|
+
};
|
|
185
|
+
var extractOverrides = (buildJson, projectDir = process.cwd()) => {
|
|
186
|
+
if (!buildJson || typeof buildJson !== "object") {
|
|
187
|
+
return {};
|
|
188
|
+
}
|
|
189
|
+
if (!("overrides" in buildJson) || !buildJson.overrides) {
|
|
190
|
+
return {};
|
|
191
|
+
}
|
|
192
|
+
const schema = z.object({
|
|
193
|
+
overrides: z.record(
|
|
194
|
+
z.string(),
|
|
195
|
+
z.object({
|
|
196
|
+
path: z.string()
|
|
197
|
+
})
|
|
198
|
+
)
|
|
199
|
+
});
|
|
200
|
+
const result = schema.safeParse(buildJson);
|
|
201
|
+
if (!result.success) {
|
|
202
|
+
throw new ScriptError(
|
|
203
|
+
`Invalid build.json structure for overrides: ${result.error.message}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
const resolvedOverrides = {};
|
|
207
|
+
for (const [key, value] of Object.entries(result.data.overrides)) {
|
|
208
|
+
resolvedOverrides[key] = {
|
|
209
|
+
path: path5.resolve(projectDir, value.path)
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
return resolvedOverrides;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/errors/NotFoundError.ts
|
|
216
|
+
var NotFoundError = class extends ScriptError {
|
|
217
|
+
constructor(message) {
|
|
218
|
+
super(message);
|
|
219
|
+
this.name = "NotFoundError";
|
|
220
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// src/utils/getBuildJson.ts
|
|
225
|
+
var getBuildJson = (projectDir = process.cwd()) => {
|
|
226
|
+
const buildJsonPath = path5.join(projectDir, "build.json");
|
|
227
|
+
if (!existsSync(buildJsonPath)) {
|
|
228
|
+
throw new NotFoundError(
|
|
229
|
+
`Build JSON file not found at ${buildJsonPath}. Please make sure build.json exists in the project root.`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
let buildJson;
|
|
233
|
+
try {
|
|
234
|
+
const rawData = readFileSync(buildJsonPath, "utf-8");
|
|
235
|
+
buildJson = JSON.parse(rawData);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
throw new ScriptError(
|
|
238
|
+
`Failed to parse build.json: ${error instanceof Error ? error.message : String(error)}`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
if (buildJson && typeof buildJson === "object" && "services" in buildJson) {
|
|
242
|
+
const services = buildJson.services;
|
|
243
|
+
if (services && typeof services === "object") {
|
|
244
|
+
for (const serviceConfig of Object.values(services)) {
|
|
245
|
+
if (serviceConfig && typeof serviceConfig === "object" && "depends_on" in serviceConfig) {
|
|
246
|
+
const dependsOn = serviceConfig.depends_on;
|
|
247
|
+
if (Array.isArray(dependsOn)) {
|
|
248
|
+
serviceConfig.depends_on = dependsOn.reduce(
|
|
249
|
+
(acc, dep) => {
|
|
250
|
+
acc[dep] = { condition: "service_started" };
|
|
251
|
+
return acc;
|
|
252
|
+
},
|
|
253
|
+
{}
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return buildJson;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/utils/ymlMods/addDependenciesToAppService.ts
|
|
264
|
+
var addDependenciesToAppService = (yml, dependencies) => {
|
|
265
|
+
if (!yml || !yml.services || !yml.services.app) {
|
|
266
|
+
throw new ScriptError("Invalid YAML structure. Missing 'services.app' field.");
|
|
267
|
+
}
|
|
268
|
+
if (!dependencies || dependencies.length === 0) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
yml.services.app.depends_on = yml.services.app.depends_on || {};
|
|
272
|
+
dependencies.forEach((dep) => {
|
|
273
|
+
yml.services.app.depends_on[dep.name] = {
|
|
274
|
+
condition: "service_healthy"
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// src/utils/ymlMods/attachName.ts
|
|
280
|
+
var attachName = (yml, buildJson) => {
|
|
281
|
+
if (!yml) {
|
|
282
|
+
throw new ScriptError("Invalid YAML structure. Missing 'yml' field.");
|
|
283
|
+
}
|
|
284
|
+
yml.name = buildJson.name || yml.name;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// src/utils/ymlMods/makeE2eDependOnApp.ts
|
|
288
|
+
var makeE2eDependOnApp = (yml) => {
|
|
289
|
+
if (!yml || !yml.services) {
|
|
290
|
+
throw new ScriptError("Invalid YAML structure. Missing 'services' field.");
|
|
291
|
+
}
|
|
292
|
+
if (!yml.services.tester) {
|
|
293
|
+
throw new ScriptError("Tester service not found");
|
|
294
|
+
}
|
|
295
|
+
if (!yml.services.app) {
|
|
296
|
+
throw new ScriptError("App service not found");
|
|
297
|
+
}
|
|
298
|
+
yml.services.tester.depends_on = {
|
|
299
|
+
...yml.services.tester.depends_on,
|
|
300
|
+
app: {
|
|
301
|
+
condition: "service_healthy"
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
// src/utils/ymlMods/renameServicesWithDependencies.ts
|
|
307
|
+
var renameServicesWithDependencies = (yml, dependencies) => {
|
|
308
|
+
if (!yml || !yml.services) {
|
|
309
|
+
throw new Error("Invalid YAML structure. Missing 'services' field.");
|
|
310
|
+
}
|
|
311
|
+
if (!dependencies || dependencies.length === 0) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
yml.services = Object.fromEntries(
|
|
315
|
+
Object.entries(yml.services).map(([name, service]) => {
|
|
316
|
+
const dependency = dependencies.find((d) => d.serviceName === name);
|
|
317
|
+
return [dependency ? dependency.name : name, service];
|
|
318
|
+
})
|
|
319
|
+
);
|
|
320
|
+
};
|
|
321
|
+
var cachedTemplatesDir = null;
|
|
322
|
+
function getTemplatesDir() {
|
|
323
|
+
if (process.env.BUILDER_TEMPLATES_DIR && fs.existsSync(process.env.BUILDER_TEMPLATES_DIR)) {
|
|
324
|
+
return process.env.BUILDER_TEMPLATES_DIR;
|
|
325
|
+
}
|
|
326
|
+
if (cachedTemplatesDir && fs.existsSync(cachedTemplatesDir)) {
|
|
327
|
+
return cachedTemplatesDir;
|
|
328
|
+
}
|
|
329
|
+
const currentDir = typeof __dirname !== "undefined" ? __dirname : path5.dirname(fileURLToPath(import.meta.url));
|
|
330
|
+
const candidates = [
|
|
331
|
+
// When executing compiled output in dist/ (e.g. dist/templates)
|
|
332
|
+
path5.resolve(currentDir, "templates"),
|
|
333
|
+
path5.resolve(currentDir, "../templates"),
|
|
334
|
+
// When executing from src/ (e.g. src/core/templates.ts -> ../../templates)
|
|
335
|
+
path5.resolve(currentDir, "../../templates"),
|
|
336
|
+
// Fallback relative to cwd
|
|
337
|
+
path5.resolve(process.cwd(), "templates")
|
|
338
|
+
];
|
|
339
|
+
for (const cand of candidates) {
|
|
340
|
+
if (fs.existsSync(cand) && fs.existsSync(path5.join(cand, "docker"))) {
|
|
341
|
+
cachedTemplatesDir = cand;
|
|
342
|
+
return cand;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
throw new Error(
|
|
346
|
+
`Templates directory not found. Searched candidates:
|
|
347
|
+
${candidates.join("\n")}`
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
function getDockerComposeTemplate(filename) {
|
|
351
|
+
const templatesDir = getTemplatesDir();
|
|
352
|
+
const filePath = path5.join(templatesDir, "docker", filename);
|
|
353
|
+
if (!fs.existsSync(filePath)) {
|
|
354
|
+
throw new Error(`Docker compose template not found: ${filePath}`);
|
|
355
|
+
}
|
|
356
|
+
return filePath;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// src/core/compileEnvironment.ts
|
|
360
|
+
var compileEnvironment = (environment, projectDir = process.cwd()) => {
|
|
361
|
+
const buildJson = getBuildJson(projectDir);
|
|
362
|
+
const overrides = extractOverrides(buildJson, projectDir);
|
|
363
|
+
const dependencies = extractBuildDeps(buildJson, projectDir);
|
|
364
|
+
const baseComposePath = getDockerComposeTemplate("docker-compose.base.yml");
|
|
365
|
+
const baseComposeDevPath = getDockerComposeTemplate("docker-compose.dev.yml");
|
|
366
|
+
const baseComposeProdPath = getDockerComposeTemplate("docker-compose.prod.yml");
|
|
367
|
+
const baseComposeTestPath = getDockerComposeTemplate("docker-compose.test.yml");
|
|
368
|
+
const baseComposeE2ePath = getDockerComposeTemplate("docker-compose.e2e.yml");
|
|
369
|
+
const baseComposeStandalonePath = getDockerComposeTemplate("docker-compose.standalone.yml");
|
|
370
|
+
const projectName = buildJson.name || path5.basename(projectDir);
|
|
371
|
+
const composeFlags = [
|
|
372
|
+
"compose",
|
|
373
|
+
"-p",
|
|
374
|
+
projectName,
|
|
375
|
+
"--project-directory",
|
|
376
|
+
projectDir,
|
|
377
|
+
"-f",
|
|
378
|
+
baseComposePath
|
|
379
|
+
];
|
|
380
|
+
const envFile = path5.resolve(projectDir, `.env.${environment}`);
|
|
381
|
+
const hasEnvFile = existsSync(envFile);
|
|
382
|
+
switch (environment) {
|
|
383
|
+
case "dev":
|
|
384
|
+
if (hasEnvFile) {
|
|
385
|
+
composeFlags.push("--env-file", envFile);
|
|
386
|
+
}
|
|
387
|
+
composeFlags.push("-f", baseComposeDevPath);
|
|
388
|
+
if (overrides.dev) {
|
|
389
|
+
composeFlags.push("-f", overrides.dev.path);
|
|
390
|
+
}
|
|
391
|
+
composeFlags.push("-f", baseComposeStandalonePath);
|
|
392
|
+
break;
|
|
393
|
+
case "test":
|
|
394
|
+
if (hasEnvFile) {
|
|
395
|
+
composeFlags.push("--env-file", envFile);
|
|
396
|
+
}
|
|
397
|
+
composeFlags.push("-f", baseComposeTestPath);
|
|
398
|
+
if (overrides.test) {
|
|
399
|
+
composeFlags.push("-f", overrides.test.path);
|
|
400
|
+
}
|
|
401
|
+
break;
|
|
402
|
+
case "prod":
|
|
403
|
+
if (hasEnvFile) {
|
|
404
|
+
composeFlags.push("--env-file", envFile);
|
|
405
|
+
}
|
|
406
|
+
composeFlags.push("-f", baseComposeProdPath);
|
|
407
|
+
if (overrides.prod) {
|
|
408
|
+
composeFlags.push("-f", overrides.prod.path);
|
|
409
|
+
}
|
|
410
|
+
if (dependencies.length > 0) {
|
|
411
|
+
composeFlags.push(...dependencies.flatMap((dep) => ["-f", dep.path]));
|
|
412
|
+
}
|
|
413
|
+
composeFlags.push("-f", baseComposeStandalonePath);
|
|
414
|
+
break;
|
|
415
|
+
case "e2e":
|
|
416
|
+
if (hasEnvFile) {
|
|
417
|
+
composeFlags.push("--env-file", envFile);
|
|
418
|
+
}
|
|
419
|
+
composeFlags.push("-f", baseComposeProdPath);
|
|
420
|
+
if (overrides.prod) {
|
|
421
|
+
composeFlags.push("-f", overrides.prod.path);
|
|
422
|
+
}
|
|
423
|
+
if (dependencies.length > 0) {
|
|
424
|
+
composeFlags.push(...dependencies.flatMap((dep) => ["-f", dep.path]));
|
|
425
|
+
}
|
|
426
|
+
composeFlags.push("-f", baseComposeE2ePath);
|
|
427
|
+
if (overrides.e2e) {
|
|
428
|
+
composeFlags.push("-f", overrides.e2e.path);
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
composeFlags.push("config");
|
|
433
|
+
const result = spawnSync("docker", composeFlags, {
|
|
434
|
+
cwd: projectDir,
|
|
435
|
+
encoding: "utf-8"
|
|
436
|
+
});
|
|
437
|
+
if (result.error) {
|
|
438
|
+
throw new ScriptError(
|
|
439
|
+
`Failed to run docker compose: ${result.error.message}`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
if (result.status !== 0) {
|
|
443
|
+
throw new ScriptError(
|
|
444
|
+
`\u274C Failed to merge compose files:
|
|
445
|
+
${result.stderr || result.stdout}`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
const mergedYamlConfig = result.stdout;
|
|
449
|
+
const yml = parse(mergedYamlConfig);
|
|
450
|
+
if (environment === "prod" || environment === "e2e") {
|
|
451
|
+
renameServicesWithDependencies(yml, dependencies);
|
|
452
|
+
addDependenciesToAppService(yml, dependencies);
|
|
453
|
+
}
|
|
454
|
+
if (environment === "e2e") {
|
|
455
|
+
makeE2eDependOnApp(yml);
|
|
456
|
+
}
|
|
457
|
+
attachName(yml, buildJson);
|
|
458
|
+
const finalYamlConfig = stringify(yml);
|
|
459
|
+
return finalYamlConfig;
|
|
460
|
+
};
|
|
461
|
+
function checkDependencies() {
|
|
462
|
+
const dockerCheck = spawnSync("command -v docker", { shell: true });
|
|
463
|
+
if (dockerCheck.status !== 0) {
|
|
464
|
+
throw new ScriptError(
|
|
465
|
+
"Error: docker is not installed or not in PATH. Please install Docker first."
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/commands/serviceUp.ts
|
|
471
|
+
function runCompose(args, yamlConfig, projectDir) {
|
|
472
|
+
const runResult = spawnSync(
|
|
473
|
+
"docker",
|
|
474
|
+
["compose", "--project-directory", projectDir, "-f", "-", ...args],
|
|
475
|
+
{
|
|
476
|
+
cwd: projectDir,
|
|
477
|
+
input: yamlConfig,
|
|
478
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
479
|
+
encoding: "utf-8"
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
if (runResult.error) {
|
|
483
|
+
throw new ScriptError(
|
|
484
|
+
`Failed to run docker compose: ${runResult.error.message}`
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
return runResult.status ?? (runResult.error ? 1 : 0);
|
|
488
|
+
}
|
|
489
|
+
function teardownCompose(yamlConfig, projectDir) {
|
|
490
|
+
const downResult = spawnSync(
|
|
491
|
+
"docker",
|
|
492
|
+
[
|
|
493
|
+
"compose",
|
|
494
|
+
"--project-directory",
|
|
495
|
+
projectDir,
|
|
496
|
+
"-f",
|
|
497
|
+
"-",
|
|
498
|
+
"down",
|
|
499
|
+
"-v",
|
|
500
|
+
"--remove-orphans"
|
|
501
|
+
],
|
|
502
|
+
{
|
|
503
|
+
cwd: projectDir,
|
|
504
|
+
input: yamlConfig,
|
|
505
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
506
|
+
encoding: "utf-8"
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
if (downResult.error) {
|
|
510
|
+
console.error(
|
|
511
|
+
pc2.red(`Failed to run docker compose down: ${downResult.error.message}`)
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
async function handleServiceUp(rawStage, options = {}) {
|
|
516
|
+
const projectDir = options.projectDir || process.cwd();
|
|
517
|
+
const stage = (rawStage || "dev").toLowerCase();
|
|
518
|
+
checkDependencies();
|
|
519
|
+
if (!options.skipCheck) {
|
|
520
|
+
checkInfra(projectDir);
|
|
521
|
+
}
|
|
522
|
+
if (stage === "dev") {
|
|
523
|
+
console.log(pc2.cyan(`\u{1F680} Starting DEV environment...`));
|
|
524
|
+
const finalYamlConfig = compileEnvironment("dev", projectDir);
|
|
525
|
+
if (options.dryRun || options.debug || process.env.CI_DEBUG === "true") {
|
|
526
|
+
console.log(pc2.yellow("Final merged YAML configuration:\n"), finalYamlConfig);
|
|
527
|
+
}
|
|
528
|
+
if (options.dryRun) {
|
|
529
|
+
console.log(pc2.green("\u2705 Dry run completed successfully."));
|
|
530
|
+
return 0;
|
|
531
|
+
}
|
|
532
|
+
const upArgs = ["up", "--build"];
|
|
533
|
+
if (options.detach) {
|
|
534
|
+
upArgs.push("-d");
|
|
535
|
+
}
|
|
536
|
+
const status = runCompose(upArgs, finalYamlConfig, projectDir);
|
|
537
|
+
return status;
|
|
538
|
+
}
|
|
539
|
+
if (stage === "prod") {
|
|
540
|
+
console.log(pc2.cyan(`\u{1F3ED} Starting PROD environment...`));
|
|
541
|
+
const finalYamlConfig = compileEnvironment("prod", projectDir);
|
|
542
|
+
if (options.dryRun || options.debug || process.env.CI_DEBUG === "true") {
|
|
543
|
+
console.log(pc2.yellow("Final merged YAML configuration:\n"), finalYamlConfig);
|
|
544
|
+
}
|
|
545
|
+
if (options.dryRun) {
|
|
546
|
+
console.log(pc2.green("\u2705 Dry run completed successfully."));
|
|
547
|
+
return 0;
|
|
548
|
+
}
|
|
549
|
+
const upArgs = ["up", "--build"];
|
|
550
|
+
if (options.detach) {
|
|
551
|
+
upArgs.push("-d");
|
|
552
|
+
}
|
|
553
|
+
const status = runCompose(upArgs, finalYamlConfig, projectDir);
|
|
554
|
+
return status;
|
|
555
|
+
}
|
|
556
|
+
if (stage === "test-unit" || stage === "test" && options.unit && !options.e2e) {
|
|
557
|
+
console.log(pc2.bold(pc2.blue(`
|
|
558
|
+
========================================`)));
|
|
559
|
+
console.log(pc2.bold(pc2.blue(`\u{1F9EA} STAGE: UNIT & ISOLATED TESTS`)));
|
|
560
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
561
|
+
`)));
|
|
562
|
+
const finalYamlConfig = compileEnvironment("test", projectDir);
|
|
563
|
+
if (options.dryRun || options.debug || process.env.CI_DEBUG === "true") {
|
|
564
|
+
console.log(pc2.yellow("Final merged YAML configuration:\n"), finalYamlConfig);
|
|
565
|
+
}
|
|
566
|
+
if (options.dryRun) {
|
|
567
|
+
console.log(pc2.green("\u2705 Dry run completed successfully."));
|
|
568
|
+
return 0;
|
|
569
|
+
}
|
|
570
|
+
let status = 0;
|
|
571
|
+
try {
|
|
572
|
+
status = runCompose(
|
|
573
|
+
["up", "--build", "--abort-on-container-exit"],
|
|
574
|
+
finalYamlConfig,
|
|
575
|
+
projectDir
|
|
576
|
+
);
|
|
577
|
+
} finally {
|
|
578
|
+
teardownCompose(finalYamlConfig, projectDir);
|
|
579
|
+
}
|
|
580
|
+
if (status === 0) {
|
|
581
|
+
console.log(pc2.bold(pc2.green(`
|
|
582
|
+
\u2705 Unit tests passed successfully.
|
|
583
|
+
`)));
|
|
584
|
+
} else {
|
|
585
|
+
console.error(pc2.red(`
|
|
586
|
+
\u274C Unit tests failed with exit code ${status}.
|
|
587
|
+
`));
|
|
588
|
+
}
|
|
589
|
+
return status;
|
|
590
|
+
}
|
|
591
|
+
if (stage === "test-e2e" || stage === "e2e" || stage === "test" && options.e2e && !options.unit) {
|
|
592
|
+
console.log(pc2.bold(pc2.blue(`
|
|
593
|
+
========================================`)));
|
|
594
|
+
console.log(pc2.bold(pc2.blue(`\u{1F6A6} STAGE: INTEGRATED E2E TESTS`)));
|
|
595
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
596
|
+
`)));
|
|
597
|
+
const finalYamlConfig = compileEnvironment("e2e", projectDir);
|
|
598
|
+
if (options.dryRun || options.debug || process.env.CI_DEBUG === "true") {
|
|
599
|
+
console.log(pc2.yellow("Final merged YAML configuration:\n"), finalYamlConfig);
|
|
600
|
+
}
|
|
601
|
+
if (options.dryRun) {
|
|
602
|
+
console.log(pc2.green("\u2705 Dry run completed successfully."));
|
|
603
|
+
return 0;
|
|
604
|
+
}
|
|
605
|
+
let status = 0;
|
|
606
|
+
try {
|
|
607
|
+
status = runCompose(
|
|
608
|
+
[
|
|
609
|
+
"up",
|
|
610
|
+
"--build",
|
|
611
|
+
"--abort-on-container-exit",
|
|
612
|
+
"--exit-code-from",
|
|
613
|
+
"tester"
|
|
614
|
+
],
|
|
615
|
+
finalYamlConfig,
|
|
616
|
+
projectDir
|
|
617
|
+
);
|
|
618
|
+
} finally {
|
|
619
|
+
teardownCompose(finalYamlConfig, projectDir);
|
|
620
|
+
}
|
|
621
|
+
if (status === 0) {
|
|
622
|
+
console.log(pc2.bold(pc2.green(`
|
|
623
|
+
\u2705 E2E tests passed successfully.
|
|
624
|
+
`)));
|
|
625
|
+
} else {
|
|
626
|
+
console.error(pc2.red(`
|
|
627
|
+
\u274C E2E tests failed with exit code ${status}.
|
|
628
|
+
`));
|
|
629
|
+
}
|
|
630
|
+
return status;
|
|
631
|
+
}
|
|
632
|
+
if (stage === "test") {
|
|
633
|
+
if (options.dryRun) {
|
|
634
|
+
console.log(pc2.bold(pc2.blue(`
|
|
635
|
+
========================================`)));
|
|
636
|
+
console.log(pc2.bold(pc2.blue(`\u{1F9EA} [1/2] STAGE: UNIT & ISOLATED TESTS`)));
|
|
637
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
638
|
+
`)));
|
|
639
|
+
const unitYaml2 = compileEnvironment("test", projectDir);
|
|
640
|
+
console.log(pc2.yellow("Unit Test YAML configuration:\n"), unitYaml2);
|
|
641
|
+
console.log(pc2.bold(pc2.blue(`
|
|
642
|
+
========================================`)));
|
|
643
|
+
console.log(pc2.bold(pc2.blue(`\u{1F6A6} [2/2] STAGE: INTEGRATED E2E TESTS`)));
|
|
644
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
645
|
+
`)));
|
|
646
|
+
const e2eYaml2 = compileEnvironment("e2e", projectDir);
|
|
647
|
+
console.log(pc2.yellow("E2E Test YAML configuration:\n"), e2eYaml2);
|
|
648
|
+
console.log(pc2.green("\u2705 Dry run completed successfully for all test stages."));
|
|
649
|
+
return 0;
|
|
650
|
+
}
|
|
651
|
+
console.log(pc2.bold(pc2.blue(`
|
|
652
|
+
========================================`)));
|
|
653
|
+
console.log(pc2.bold(pc2.blue(`\u{1F9EA} [1/2] STAGE: UNIT & ISOLATED TESTS`)));
|
|
654
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
655
|
+
`)));
|
|
656
|
+
const unitYaml = compileEnvironment("test", projectDir);
|
|
657
|
+
if (options.debug || process.env.CI_DEBUG === "true") {
|
|
658
|
+
console.log(pc2.yellow("Unit Test YAML configuration:\n"), unitYaml);
|
|
659
|
+
}
|
|
660
|
+
let unitStatus = 0;
|
|
661
|
+
try {
|
|
662
|
+
unitStatus = runCompose(
|
|
663
|
+
["up", "--build", "--abort-on-container-exit"],
|
|
664
|
+
unitYaml,
|
|
665
|
+
projectDir
|
|
666
|
+
);
|
|
667
|
+
} finally {
|
|
668
|
+
teardownCompose(unitYaml, projectDir);
|
|
669
|
+
}
|
|
670
|
+
if (unitStatus !== 0) {
|
|
671
|
+
console.error(pc2.red(`
|
|
672
|
+
\u274C [1/2] Unit tests failed with exit code ${unitStatus}. Aborting E2E tests.
|
|
673
|
+
`));
|
|
674
|
+
return unitStatus;
|
|
675
|
+
}
|
|
676
|
+
console.log(pc2.bold(pc2.green(`
|
|
677
|
+
\u2705 [1/2] Unit tests passed successfully.
|
|
678
|
+
`)));
|
|
679
|
+
console.log(pc2.bold(pc2.blue(`========================================`)));
|
|
680
|
+
console.log(pc2.bold(pc2.blue(`\u{1F6A6} [2/2] STAGE: INTEGRATED E2E TESTS`)));
|
|
681
|
+
console.log(pc2.bold(pc2.blue(`========================================
|
|
682
|
+
`)));
|
|
683
|
+
const e2eYaml = compileEnvironment("e2e", projectDir);
|
|
684
|
+
if (options.debug || process.env.CI_DEBUG === "true") {
|
|
685
|
+
console.log(pc2.yellow("E2E Test YAML configuration:\n"), e2eYaml);
|
|
686
|
+
}
|
|
687
|
+
let e2eStatus = 0;
|
|
688
|
+
try {
|
|
689
|
+
e2eStatus = runCompose(
|
|
690
|
+
[
|
|
691
|
+
"up",
|
|
692
|
+
"--build",
|
|
693
|
+
"--abort-on-container-exit",
|
|
694
|
+
"--exit-code-from",
|
|
695
|
+
"tester"
|
|
696
|
+
],
|
|
697
|
+
e2eYaml,
|
|
698
|
+
projectDir
|
|
699
|
+
);
|
|
700
|
+
} finally {
|
|
701
|
+
teardownCompose(e2eYaml, projectDir);
|
|
702
|
+
}
|
|
703
|
+
if (e2eStatus !== 0) {
|
|
704
|
+
console.error(pc2.red(`
|
|
705
|
+
\u274C [2/2] E2E tests failed with exit code ${e2eStatus}.
|
|
706
|
+
`));
|
|
707
|
+
return e2eStatus;
|
|
708
|
+
}
|
|
709
|
+
console.log(pc2.bold(pc2.green(`
|
|
710
|
+
\u2705 [2/2] E2E tests passed successfully.
|
|
711
|
+
`)));
|
|
712
|
+
console.log(pc2.bold(pc2.green(`\u{1F389} All test stages (unit + e2e) completed successfully!
|
|
713
|
+
`)));
|
|
714
|
+
return 0;
|
|
715
|
+
}
|
|
716
|
+
throw new ScriptError(
|
|
717
|
+
`Unknown environment stage: "${rawStage}". Supported stages: dev, prod, test, test-unit, test-e2e`
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// src/cli.ts
|
|
722
|
+
function createCli() {
|
|
723
|
+
const cli = cac("builder");
|
|
724
|
+
cli.command("service [action] [stage]", "Manage and run services").option("--skip-check", "Skip infrastructure and contract checks").option("--debug", "Output the merged Docker Compose YAML configuration").option("-d, --detach", "Run containers in the background").option("--dry-run", "Preview generated compose configuration without starting containers").option("-C, --project-dir <dir>", "Service project directory (defaults to cwd)").option("--unit", "Run only unit tests (when stage is test)").option("--e2e", "Run only e2e tests (when stage is test)").example("builder service up dev").example("builder service up prod").example("builder service up test").action(async (action, stage, flags = {}) => {
|
|
725
|
+
if (!action || action === "up") {
|
|
726
|
+
const exitCode = await handleServiceUp(stage, {
|
|
727
|
+
projectDir: flags.projectDir || flags.C,
|
|
728
|
+
skipCheck: flags.skipCheck,
|
|
729
|
+
debug: flags.debug,
|
|
730
|
+
detach: flags.detach || flags.d,
|
|
731
|
+
dryRun: flags.dryRun,
|
|
732
|
+
unit: flags.unit,
|
|
733
|
+
e2e: flags.e2e
|
|
734
|
+
});
|
|
735
|
+
if (exitCode !== 0) {
|
|
736
|
+
process.exit(exitCode);
|
|
737
|
+
}
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
console.error(
|
|
741
|
+
pc2.red(`Unknown action: "${action}". Did you mean "builder service up ${stage || "dev"}"?`)
|
|
742
|
+
);
|
|
743
|
+
process.exit(1);
|
|
744
|
+
});
|
|
745
|
+
cli.command("up [stage]", "Shorthand for 'builder service up [stage]'").option("--skip-check", "Skip infrastructure and contract checks").option("--debug", "Output the merged Docker Compose YAML configuration").option("-d, --detach", "Run containers in the background").option("--dry-run", "Preview generated compose configuration without starting containers").option("-C, --project-dir <dir>", "Service project directory (defaults to cwd)").option("--unit", "Run only unit tests (when stage is test)").option("--e2e", "Run only e2e tests (when stage is test)").action(async (stage, flags = {}) => {
|
|
746
|
+
const exitCode = await handleServiceUp(stage, {
|
|
747
|
+
projectDir: flags.projectDir || flags.C,
|
|
748
|
+
skipCheck: flags.skipCheck,
|
|
749
|
+
debug: flags.debug,
|
|
750
|
+
detach: flags.detach || flags.d,
|
|
751
|
+
dryRun: flags.dryRun,
|
|
752
|
+
unit: flags.unit,
|
|
753
|
+
e2e: flags.e2e
|
|
754
|
+
});
|
|
755
|
+
if (exitCode !== 0) {
|
|
756
|
+
process.exit(exitCode);
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
cli.command("check", "Run infrastructure and contract checks (hadolint, conftest)").option("-C, --project-dir <dir>", "Service project directory (defaults to cwd)").action((flags = {}) => {
|
|
760
|
+
const projectDir = flags.projectDir || flags.C || process.cwd();
|
|
761
|
+
checkInfra(projectDir);
|
|
762
|
+
});
|
|
763
|
+
cli.help();
|
|
764
|
+
cli.version("1.0.0");
|
|
765
|
+
return cli;
|
|
766
|
+
}
|
|
767
|
+
async function runCli(argv = process.argv) {
|
|
768
|
+
const cli = createCli();
|
|
769
|
+
try {
|
|
770
|
+
cli.parse(argv, { run: true });
|
|
771
|
+
} catch (err) {
|
|
772
|
+
if (err instanceof ScriptError) {
|
|
773
|
+
console.error(pc2.red(`
|
|
774
|
+
\u274C ${err.message}
|
|
775
|
+
`));
|
|
776
|
+
} else {
|
|
777
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
778
|
+
console.error(pc2.red(`
|
|
779
|
+
\u274C Error: ${message}
|
|
780
|
+
`));
|
|
781
|
+
}
|
|
782
|
+
if (process.env.VITEST) {
|
|
783
|
+
throw err;
|
|
784
|
+
}
|
|
785
|
+
process.exit(1);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// src/bin.ts
|
|
790
|
+
runCli().catch((err) => {
|
|
791
|
+
console.error(err);
|
|
792
|
+
process.exit(1);
|
|
793
|
+
});
|
|
794
|
+
//# sourceMappingURL=bin.js.map
|
|
795
|
+
//# sourceMappingURL=bin.js.map
|