@solid-stack/builder 2.0.0 → 2.0.2
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/README.md +10 -0
- package/dist/bin.js +287 -125
- package/dist/bin.js.map +1 -1
- package/dist/commands/mesh/commands/down/handler.d.ts.map +1 -1
- package/dist/commands/mesh/commands/up/handler.d.ts.map +1 -1
- package/dist/commands/service/commands/down/handler.d.ts.map +1 -1
- package/dist/core/checkers/checkDockerfile.d.ts.map +1 -1
- package/dist/core/checkers/checkDockerfileContract.d.ts.map +1 -1
- package/dist/core/composition/compileServiceComposition.d.ts.map +1 -1
- package/dist/core/composition/composeNetworks.d.ts +2 -0
- package/dist/core/composition/composeNetworks.d.ts.map +1 -0
- package/dist/core/composition/composePaths.d.ts +2 -0
- package/dist/core/composition/composePaths.d.ts.map +1 -1
- package/dist/core/composition/composeVolumes.d.ts +15 -0
- package/dist/core/composition/composeVolumes.d.ts.map +1 -0
- package/dist/core/composition/namespaceDependencyCompose.d.ts.map +1 -1
- package/dist/core/composition/validateDependencyCompose.d.ts.map +1 -1
- package/dist/core/composition/validateOverrideCompose.d.ts +7 -3
- package/dist/core/composition/validateOverrideCompose.d.ts.map +1 -1
- package/dist/core/config/pathSafety.d.ts +1 -0
- package/dist/core/config/pathSafety.d.ts.map +1 -1
- package/dist/core/mesh/runners/runMeshDev.d.ts.map +1 -1
- package/dist/core/mesh/runners/runMeshProd.d.ts.map +1 -1
- package/dist/core/mesh/runners/runMeshTestE2e.d.ts.map +1 -1
- package/dist/core/mesh/v2/compileMeshComposition.d.ts.map +1 -1
- package/dist/core/runners/runDev.d.ts.map +1 -1
- package/dist/core/runners/runProd.d.ts.map +1 -1
- package/dist/core/teardownCompose.d.ts +1 -0
- package/dist/core/teardownCompose.d.ts.map +1 -1
- package/dist/index.js +287 -125
- package/dist/index.js.map +1 -1
- package/dist/utils/paths.d.ts +2 -0
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/runState.d.ts +1 -0
- package/dist/utils/runState.d.ts.map +1 -1
- package/package.json +6 -3
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Command, Argument } from 'commander';
|
|
2
2
|
import pc22 from 'picocolors';
|
|
3
|
-
import fs, { existsSync, readFileSync, mkdirSync, writeFileSync, renameSync, statSync, unlinkSync } from 'fs';
|
|
4
|
-
import
|
|
3
|
+
import fs, { existsSync, readFileSync, mkdirSync, writeFileSync, renameSync, statSync, realpathSync, lstatSync, readlinkSync, unlinkSync } from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { spawnSync, spawn } from 'child_process';
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
@@ -81,18 +81,44 @@ function errorMessage(err) {
|
|
|
81
81
|
return String(err);
|
|
82
82
|
}
|
|
83
83
|
function resolveProjectDir(projectDir) {
|
|
84
|
-
return
|
|
84
|
+
return path.resolve(projectDir ?? process.cwd());
|
|
85
|
+
}
|
|
86
|
+
function isBindMount(source) {
|
|
87
|
+
if (!source) return false;
|
|
88
|
+
if (source.startsWith(".") || source.startsWith("/") || source.startsWith("~") || source.startsWith("\\")) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (/^[a-zA-Z]:[\\/]/.test(source)) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
85
95
|
}
|
|
86
96
|
function isPathInside(parentDir, childPath) {
|
|
87
|
-
const absParent =
|
|
88
|
-
const absChild =
|
|
89
|
-
const rel =
|
|
90
|
-
return rel !== ".." && !rel.startsWith(".." +
|
|
97
|
+
const absParent = canonicalPath(parentDir);
|
|
98
|
+
const absChild = canonicalPath(path.resolve(parentDir, childPath));
|
|
99
|
+
const rel = path.relative(absParent, absChild);
|
|
100
|
+
return rel !== ".." && !rel.startsWith(".." + path.sep) && !rel.startsWith("../") && !path.isAbsolute(rel);
|
|
101
|
+
}
|
|
102
|
+
function canonicalPath(value) {
|
|
103
|
+
const absolute = path.resolve(value);
|
|
104
|
+
try {
|
|
105
|
+
return realpathSync(absolute);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error.code !== "ENOENT") throw error;
|
|
108
|
+
try {
|
|
109
|
+
if (lstatSync(absolute).isSymbolicLink()) return canonicalPath(path.resolve(path.dirname(absolute), readlinkSync(absolute)));
|
|
110
|
+
} catch (linkError) {
|
|
111
|
+
if (linkError.code !== "ENOENT") throw linkError;
|
|
112
|
+
}
|
|
113
|
+
const parent = path.dirname(absolute);
|
|
114
|
+
if (parent === absolute) throw error;
|
|
115
|
+
return path.join(canonicalPath(parent), path.basename(absolute));
|
|
116
|
+
}
|
|
91
117
|
}
|
|
92
118
|
|
|
93
119
|
// src/utils/getBuildJson.ts
|
|
94
120
|
var getBuildJson = (projectDir = process.cwd()) => {
|
|
95
|
-
const absProjectDir = resolveProjectDir(projectDir), buildJsonPath =
|
|
121
|
+
const absProjectDir = resolveProjectDir(projectDir), buildJsonPath = path.join(absProjectDir, "build.json");
|
|
96
122
|
if (!existsSync(buildJsonPath)) throw new NotFoundError(`Build JSON file not found at ${buildJsonPath}. Please make sure build.json exists in the project root.`);
|
|
97
123
|
let rawData;
|
|
98
124
|
try {
|
|
@@ -113,6 +139,18 @@ var getBuildJson = (projectDir = process.cwd()) => {
|
|
|
113
139
|
}
|
|
114
140
|
return result.data;
|
|
115
141
|
};
|
|
142
|
+
function resolveInsideProject(projectDir, relativePath, context) {
|
|
143
|
+
if (!relativePath.trim()) throw new ConfigError({ ...context, value: relativePath, reason: "Path must not be empty" });
|
|
144
|
+
const resolved = path.resolve(projectDir, relativePath);
|
|
145
|
+
if (!isPathInside(projectDir, relativePath)) throw new ConfigError({ ...context, value: relativePath, reason: "Path escapes the project directory" });
|
|
146
|
+
return resolved;
|
|
147
|
+
}
|
|
148
|
+
function resolveApplicationDockerfile(projectDir, relativePath = "Dockerfile", stage) {
|
|
149
|
+
const context = { sourceFile: path.join(projectDir, "build.json"), configPath: "dockerfile", ...stage ? { stage } : {} };
|
|
150
|
+
const resolved = resolveInsideProject(projectDir, relativePath, context);
|
|
151
|
+
if (!existsSync(resolved) || !statSync(resolved).isFile()) throw new ConfigError({ ...context, value: relativePath, reason: `Application Dockerfile not found or not a regular file: ${resolved}` });
|
|
152
|
+
return resolved;
|
|
153
|
+
}
|
|
116
154
|
|
|
117
155
|
// src/core/checkers/constants.ts
|
|
118
156
|
var HADOLINT_IMAGE = process.env.HADOLINT_IMAGE || "hadolint/hadolint:v2.12.0";
|
|
@@ -157,15 +195,7 @@ ${ctx.detail}`);
|
|
|
157
195
|
// src/core/checkers/checkDockerfile.ts
|
|
158
196
|
function checkDockerfile(buildJson, projectDir) {
|
|
159
197
|
const dockerfileRel = buildJson.dockerfile || "Dockerfile";
|
|
160
|
-
const dockerfilePath =
|
|
161
|
-
if (!isPathInside(projectDir, dockerfilePath)) {
|
|
162
|
-
throw new ScriptError(
|
|
163
|
-
`Security error: Dockerfile path "${dockerfileRel}" escapes the project directory.`
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
if (!existsSync(dockerfilePath)) {
|
|
167
|
-
throw new ConfigError({ sourceFile: path4.join(projectDir, "build.json"), configPath: "dockerfile", value: dockerfileRel, composition: "service", reason: `Application Dockerfile not found at ${dockerfilePath}` });
|
|
168
|
-
}
|
|
198
|
+
const dockerfilePath = resolveApplicationDockerfile(projectDir, dockerfileRel);
|
|
169
199
|
const content = readFileSync(dockerfilePath, "utf-8");
|
|
170
200
|
console.log(pc22.cyan(`
|
|
171
201
|
\u{1F50D} Linting Dockerfile (${dockerfileRel})...`));
|
|
@@ -185,8 +215,8 @@ function getTemplatesDir() {
|
|
|
185
215
|
lastEnvTemplatesDir = envVal;
|
|
186
216
|
}
|
|
187
217
|
if (envVal) {
|
|
188
|
-
const envDir =
|
|
189
|
-
if (!fs.existsSync(envDir) || !fs.existsSync(
|
|
218
|
+
const envDir = path.resolve(envVal);
|
|
219
|
+
if (!fs.existsSync(envDir) || !fs.existsSync(path.join(envDir, "docker"))) {
|
|
190
220
|
throw new ScriptError(
|
|
191
221
|
`BUILDER_TEMPLATES_DIR is set to "${envVal}" but directory does not exist or lacks a "docker/" subdirectory (resolved: "${envDir}").`
|
|
192
222
|
);
|
|
@@ -196,18 +226,18 @@ function getTemplatesDir() {
|
|
|
196
226
|
if (cachedTemplatesDir && fs.existsSync(cachedTemplatesDir)) {
|
|
197
227
|
return cachedTemplatesDir;
|
|
198
228
|
}
|
|
199
|
-
const currentDir = typeof __dirname !== "undefined" ? __dirname :
|
|
229
|
+
const currentDir = typeof __dirname !== "undefined" ? __dirname : path.dirname(fileURLToPath(import.meta.url));
|
|
200
230
|
const candidates = [
|
|
201
231
|
// When executing compiled output in dist/ (e.g. dist/templates)
|
|
202
|
-
|
|
203
|
-
|
|
232
|
+
path.resolve(currentDir, "templates"),
|
|
233
|
+
path.resolve(currentDir, "../templates"),
|
|
204
234
|
// When executing from src/ (e.g. src/core/templates.ts -> ../../templates)
|
|
205
|
-
|
|
235
|
+
path.resolve(currentDir, "../../templates"),
|
|
206
236
|
// Fallback relative to cwd
|
|
207
|
-
|
|
237
|
+
path.resolve(process.cwd(), "templates")
|
|
208
238
|
];
|
|
209
239
|
for (const cand of candidates) {
|
|
210
|
-
if (fs.existsSync(cand) && fs.existsSync(
|
|
240
|
+
if (fs.existsSync(cand) && fs.existsSync(path.join(cand, "docker"))) {
|
|
211
241
|
cachedTemplatesDir = cand;
|
|
212
242
|
return cand;
|
|
213
243
|
}
|
|
@@ -219,15 +249,15 @@ ${candidates.join("\n")}`
|
|
|
219
249
|
}
|
|
220
250
|
function getDockerComposeTemplate(filename) {
|
|
221
251
|
const templatesDir = getTemplatesDir();
|
|
222
|
-
const filePath =
|
|
252
|
+
const filePath = path.join(templatesDir, "docker", filename);
|
|
223
253
|
if (!fs.existsSync(filePath)) {
|
|
224
254
|
throw new ScriptError(`Docker compose template not found: ${filePath}`);
|
|
225
255
|
}
|
|
226
256
|
return filePath;
|
|
227
257
|
}
|
|
228
258
|
function getDockerfilePolicyDir() {
|
|
229
|
-
const policyDir =
|
|
230
|
-
if (!fs.existsSync(
|
|
259
|
+
const policyDir = path.join(getTemplatesDir(), "policy", "dockerfile");
|
|
260
|
+
if (!fs.existsSync(path.join(policyDir, "dockerfile.rego"))) throw new ScriptError(`Builder Dockerfile policy not found: ${policyDir}`);
|
|
231
261
|
return policyDir;
|
|
232
262
|
}
|
|
233
263
|
|
|
@@ -235,19 +265,12 @@ function getDockerfilePolicyDir() {
|
|
|
235
265
|
function checkDockerfileContract(build, projectDir) {
|
|
236
266
|
const root = resolveProjectDir(projectDir);
|
|
237
267
|
const relative = build.dockerfile ?? "Dockerfile";
|
|
238
|
-
|
|
239
|
-
if (!isPathInside(root, dockerfile) || !existsSync(dockerfile)) throw new ConfigError({ sourceFile: path4.join(root, "build.json"), configPath: "dockerfile", value: relative, composition: "service", reason: "Application Dockerfile missing or outside project" });
|
|
268
|
+
resolveApplicationDockerfile(root, relative);
|
|
240
269
|
const policyDir = getDockerfilePolicyDir();
|
|
241
270
|
console.log(pc22.cyan(`
|
|
242
271
|
\u{1F50D} Validating Builder Dockerfile contract (${relative})...`));
|
|
243
272
|
runStep("docker", ["run", "--rm", "--network", "none", "--mount", `type=bind,src=${root},dst=/project,readonly`, "--mount", `type=bind,src=${policyDir},dst=/builder-policy,readonly`, "-w", "/project", CONFTEST_IMAGE, "test", relative, "-p", "/builder-policy"], root, { stepName: "Dockerfile Contract", targetFile: relative });
|
|
244
273
|
}
|
|
245
|
-
function resolveInsideProject(projectDir, relativePath, context) {
|
|
246
|
-
if (!relativePath.trim()) throw new ConfigError({ ...context, value: relativePath, reason: "Path must not be empty" });
|
|
247
|
-
const resolved = path4.resolve(projectDir, relativePath);
|
|
248
|
-
if (!isPathInside(projectDir, relativePath)) throw new ConfigError({ ...context, value: relativePath, reason: "Path escapes the project directory" });
|
|
249
|
-
return resolved;
|
|
250
|
-
}
|
|
251
274
|
function loadComposeDocument(absolutePath, context) {
|
|
252
275
|
const fail = (reason, value) => {
|
|
253
276
|
throw new ConfigError({ ...context, sourceFile: absolutePath, ...{} , reason });
|
|
@@ -281,7 +304,7 @@ function getDeclaredYamlFiles(build, projectDir) {
|
|
|
281
304
|
const files = [];
|
|
282
305
|
const seen = /* @__PURE__ */ new Set();
|
|
283
306
|
const add = (relativePath, source, kind, stage) => {
|
|
284
|
-
const absolutePath =
|
|
307
|
+
const absolutePath = path.resolve(projectDir, relativePath);
|
|
285
308
|
if (seen.has(absolutePath)) return;
|
|
286
309
|
seen.add(absolutePath);
|
|
287
310
|
files.push({ relativePath, absolutePath, source, kind, ...stage ? { stage } : {} });
|
|
@@ -307,6 +330,12 @@ function checkYamlFiles(build, projectDir) {
|
|
|
307
330
|
}
|
|
308
331
|
}
|
|
309
332
|
|
|
333
|
+
// src/core/composition/composeNetworks.ts
|
|
334
|
+
function normalizeNetworks(value) {
|
|
335
|
+
if (Array.isArray(value)) return Object.fromEntries(value.filter((name) => typeof name === "string").map((name) => [name, null]));
|
|
336
|
+
return value && typeof value === "object" ? { ...value } : {};
|
|
337
|
+
}
|
|
338
|
+
|
|
310
339
|
// src/core/config/endpointProperties.ts
|
|
311
340
|
function resolveEndpointProperty(endpoint, property, context) {
|
|
312
341
|
const published = () => {
|
|
@@ -489,7 +518,7 @@ function allocateHostPorts(projectDir, stage, intentsInDeterministicOrder, valid
|
|
|
489
518
|
}
|
|
490
519
|
return allocated;
|
|
491
520
|
}
|
|
492
|
-
const cachePath =
|
|
521
|
+
const cachePath = path.join(projectDir, AUTO_PORT_CACHE_RELATIVE_PATH);
|
|
493
522
|
let cached = {};
|
|
494
523
|
try {
|
|
495
524
|
if (existsSync(cachePath)) {
|
|
@@ -513,7 +542,7 @@ function allocateHostPorts(projectDir, stage, intentsInDeterministicOrder, valid
|
|
|
513
542
|
nextCache[intent.logicalPath] = selected;
|
|
514
543
|
}
|
|
515
544
|
if (intentsInDeterministicOrder.some((i) => i.requested === "AUTO")) {
|
|
516
|
-
mkdirSync(
|
|
545
|
+
mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
517
546
|
const temp = `${cachePath}.${process.pid}.tmp`;
|
|
518
547
|
writeFileSync(temp, JSON.stringify({ version: AUTO_PORT_CACHE_VERSION, allocations: nextCache }, null, 2));
|
|
519
548
|
renameSync(temp, cachePath);
|
|
@@ -524,7 +553,7 @@ function normalizeProjectName(name, projectDir) {
|
|
|
524
553
|
let normalized = name.toLowerCase().replace(/[^a-z0-9_-]/g, "-").replace(/^[-_]+/, "").replace(/[-_]+$/, "");
|
|
525
554
|
if (!normalized || !/^[a-z0-9]/.test(normalized)) {
|
|
526
555
|
if (projectDir) {
|
|
527
|
-
const hash = crypto.createHash("sha1").update(
|
|
556
|
+
const hash = crypto.createHash("sha1").update(path.resolve(projectDir)).digest("hex").slice(0, 7);
|
|
528
557
|
return `project-${hash}`;
|
|
529
558
|
}
|
|
530
559
|
return "project";
|
|
@@ -534,7 +563,7 @@ function normalizeProjectName(name, projectDir) {
|
|
|
534
563
|
function deriveProjectName(name, stage, projectDir, options = {}) {
|
|
535
564
|
let base = normalizeProjectName(name, projectDir);
|
|
536
565
|
if (options.hash && projectDir) {
|
|
537
|
-
const hash = crypto.createHash("sha1").update(
|
|
566
|
+
const hash = crypto.createHash("sha1").update(path.resolve(projectDir)).digest("hex").slice(0, 7);
|
|
538
567
|
base = `${base}-${hash}`;
|
|
539
568
|
}
|
|
540
569
|
if (!stage) {
|
|
@@ -555,24 +584,66 @@ function deriveProjectName(name, stage, projectDir, options = {}) {
|
|
|
555
584
|
}
|
|
556
585
|
return `${base}-${s}`;
|
|
557
586
|
}
|
|
587
|
+
function parseVolume(mount) {
|
|
588
|
+
if (typeof mount === "string") {
|
|
589
|
+
const separators = [];
|
|
590
|
+
let start = 0;
|
|
591
|
+
for (let i = 0; i < mount.length; i++) {
|
|
592
|
+
if (mount[i] !== ":") continue;
|
|
593
|
+
if (i === start + 1 && /[a-z]/i.test(mount[start]) && /[\\/]/.test(mount[i + 1] ?? "")) continue;
|
|
594
|
+
separators.push(i);
|
|
595
|
+
start = i + 1;
|
|
596
|
+
}
|
|
597
|
+
const first = separators[0];
|
|
598
|
+
if (first === void 0) return { type: "volume", target: mount, suffix: "" };
|
|
599
|
+
const source = mount.slice(0, first), end = separators[1];
|
|
600
|
+
return { type: isBindMount(source) ? "bind" : "volume", source, target: mount.slice(first + 1, end), suffix: mount.slice(first) };
|
|
601
|
+
}
|
|
602
|
+
if (mount && typeof mount === "object") {
|
|
603
|
+
const value = mount;
|
|
604
|
+
return { type: String(value.type ?? "volume"), ...typeof value.source === "string" ? { source: value.source } : {}, target: String(value.target ?? ""), suffix: "" };
|
|
605
|
+
}
|
|
606
|
+
return void 0;
|
|
607
|
+
}
|
|
608
|
+
function rewriteVolumeSource(mount, source) {
|
|
609
|
+
const parsed = parseVolume(mount);
|
|
610
|
+
return typeof mount === "string" ? `${source}${parsed.suffix}` : { ...mount, source };
|
|
611
|
+
}
|
|
612
|
+
function namespaceVolume(mount, names) {
|
|
613
|
+
const parsed = parseVolume(mount);
|
|
614
|
+
const source = parsed?.type === "volume" && parsed.source ? names.get(parsed.source) : void 0;
|
|
615
|
+
return source ? rewriteVolumeSource(mount, source) : mount;
|
|
616
|
+
}
|
|
617
|
+
function isAnonymousVolume(mount) {
|
|
618
|
+
const parsed = parseVolume(mount);
|
|
619
|
+
return parsed?.type === "volume" && !parsed.source;
|
|
620
|
+
}
|
|
621
|
+
function isHostFilesystemRoot(source) {
|
|
622
|
+
const normalized = source.replaceAll("\\", "/");
|
|
623
|
+
if (/^[a-z]:\//i.test(normalized) || normalized.startsWith("//")) {
|
|
624
|
+
const windows = path.win32.normalize(source);
|
|
625
|
+
return windows === path.win32.parse(windows).root;
|
|
626
|
+
}
|
|
627
|
+
return path.posix.normalize(normalized) === "/";
|
|
628
|
+
}
|
|
629
|
+
function anonymousVolumeServices(services) {
|
|
630
|
+
return Object.entries(services).filter(([, service]) => Array.isArray(service.volumes) && service.volumes.some(isAnonymousVolume)).map(([name]) => name);
|
|
631
|
+
}
|
|
558
632
|
var posix = (p) => p.replace(/\\/g, "/");
|
|
559
633
|
function rebase(p, base, out) {
|
|
560
|
-
if (
|
|
561
|
-
const abs =
|
|
562
|
-
const rel =
|
|
634
|
+
if (path.isAbsolute(p) || /^[A-Za-z]:[\\/]/.test(p)) return posix(path.normalize(p));
|
|
635
|
+
const abs = path.resolve(base, p);
|
|
636
|
+
const rel = path.relative(out, abs);
|
|
563
637
|
const result = posix(isPathInside(out, rel) ? rel || "." : abs);
|
|
564
|
-
return result !== "." && !result.startsWith(".") && !
|
|
638
|
+
return result !== "." && !result.startsWith(".") && !path.isAbsolute(result) ? `./${result}` : result;
|
|
565
639
|
}
|
|
566
640
|
function volumeShort(v, base, out) {
|
|
567
|
-
const
|
|
568
|
-
if (
|
|
569
|
-
|
|
570
|
-
if (source === "" || source.startsWith("/") || source.startsWith("~") || /^[A-Za-z]:[\\/]/.test(source)) return v;
|
|
571
|
-
if (source.startsWith(".") || source.includes("/") || source.includes("\\")) parts[0] = rebase(source, base, out);
|
|
572
|
-
return parts.join(":");
|
|
641
|
+
const mount = parseVolume(v);
|
|
642
|
+
if (mount?.type !== "bind" || !mount.source || /^(?:[\\/]|~|[a-z]:)/i.test(mount.source)) return v;
|
|
643
|
+
return rewriteVolumeSource(v, rebase(mount.source, base, out));
|
|
573
644
|
}
|
|
574
645
|
function rebaseComposePaths(document, composeFilePath, outputProjectDir) {
|
|
575
|
-
const doc = structuredClone(document), base =
|
|
646
|
+
const doc = structuredClone(document), base = path.dirname(composeFilePath), out = path.resolve(outputProjectDir);
|
|
576
647
|
for (const svc of Object.values(doc.services ?? {})) {
|
|
577
648
|
if (typeof svc.build === "string") svc.build = rebase(svc.build, base, out);
|
|
578
649
|
else if (svc.build && typeof svc.build === "object") {
|
|
@@ -587,6 +658,52 @@ function rebaseComposePaths(document, composeFilePath, outputProjectDir) {
|
|
|
587
658
|
for (const kind of ["configs", "secrets"]) for (const def of Object.values(doc[kind] ?? {})) if (def && typeof def === "object" && typeof def.file === "string") def.file = rebase(def.file, base, out);
|
|
588
659
|
return doc;
|
|
589
660
|
}
|
|
661
|
+
function rebaseAppOverridePaths(document, serviceProjectDir, outputProjectDir) {
|
|
662
|
+
const doc = structuredClone(document);
|
|
663
|
+
const base = path.resolve(serviceProjectDir);
|
|
664
|
+
const out = path.resolve(outputProjectDir);
|
|
665
|
+
for (const svc of Object.values(doc.services ?? {})) {
|
|
666
|
+
if (svc.build && typeof svc.build === "object" && !Array.isArray(svc.build)) {
|
|
667
|
+
const build = svc.build;
|
|
668
|
+
if (typeof build.context === "string") {
|
|
669
|
+
build.context = rebase(build.context, base, out);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
if (typeof svc.env_file === "string") {
|
|
673
|
+
svc.env_file = rebase(svc.env_file, base, out);
|
|
674
|
+
} else if (Array.isArray(svc.env_file)) {
|
|
675
|
+
svc.env_file = svc.env_file.map((entry) => {
|
|
676
|
+
if (typeof entry === "string") return rebase(entry, base, out);
|
|
677
|
+
if (entry && typeof entry === "object") {
|
|
678
|
+
const value = entry;
|
|
679
|
+
if (typeof value.path === "string") return { ...value, path: rebase(value.path, base, out) };
|
|
680
|
+
}
|
|
681
|
+
return entry;
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
if (Array.isArray(svc.volumes)) {
|
|
685
|
+
svc.volumes = svc.volumes.map((volume) => {
|
|
686
|
+
if (typeof volume === "string") return volumeShort(volume, base, out);
|
|
687
|
+
if (volume && typeof volume === "object") {
|
|
688
|
+
const value = volume;
|
|
689
|
+
if (value.type === "bind" && typeof value.source === "string") {
|
|
690
|
+
return { ...value, source: rebase(value.source, base, out) };
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return volume;
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
for (const kind of ["configs", "secrets"]) {
|
|
698
|
+
for (const definition of Object.values(doc[kind] ?? {})) {
|
|
699
|
+
if (definition && typeof definition === "object") {
|
|
700
|
+
const value = definition;
|
|
701
|
+
if (typeof value.file === "string") value.file = rebase(value.file, base, out);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return doc;
|
|
706
|
+
}
|
|
590
707
|
|
|
591
708
|
// src/core/composition/composeEnvironment.ts
|
|
592
709
|
function normalizeComposeEnvironment(value, context) {
|
|
@@ -639,13 +756,13 @@ function namespaceDependencyCompose(options) {
|
|
|
639
756
|
svc.network_mode = `service:${serviceNameMap.get(key) ?? key}`;
|
|
640
757
|
}
|
|
641
758
|
if (Array.isArray(svc.volumes_from)) svc.volumes_from = svc.volumes_from.map((v) => typeof v === "string" ? v.replace(/^([^:]+)(.*)$/, (all, n, rest) => `${serviceNameMap.get(n) ?? n}${rest}`) : v);
|
|
642
|
-
if (Array.isArray(svc.volumes)) svc.volumes = svc.volumes.map((v) =>
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
759
|
+
if (Array.isArray(svc.volumes)) svc.volumes = svc.volumes.map((v) => namespaceVolume(v, volumeNameMap));
|
|
760
|
+
if (svc.networks === void 0 && svc.network_mode === void 0 && svc.extends === void 0 && networkNameMap.has("default")) svc.networks = { default: null };
|
|
761
|
+
for (const key of ["ipc", "pid"]) if (typeof svc[key] === "string" && svc[key].startsWith("service:")) svc[key] = `service:${serviceNameMap.get(svc[key].slice(8)) ?? svc[key].slice(8)}`;
|
|
762
|
+
if (svc.extends && typeof svc.extends === "object" && !svc.extends.file) {
|
|
763
|
+
const ref = svc.extends;
|
|
764
|
+
if (typeof ref.service === "string") ref.service = serviceNameMap.get(ref.service) ?? ref.service;
|
|
765
|
+
}
|
|
649
766
|
if (Array.isArray(svc.networks)) svc.networks = svc.networks.map((v) => typeof v === "string" ? networkNameMap.get(v) ?? v : v);
|
|
650
767
|
else if (svc.networks && typeof svc.networks === "object") svc.networks = rewriteKeys(svc.networks, networkNameMap);
|
|
651
768
|
for (const key of ["configs", "secrets"]) if (Array.isArray(svc[key])) svc[key] = svc[key].map((v) => typeof v === "string" ? (key === "configs" ? configNameMap : secretNameMap).get(v) ?? v : v && typeof v === "object" ? { ...v, source: (key === "configs" ? configNameMap : secretNameMap).get(v.source) ?? v.source } : v);
|
|
@@ -672,8 +789,8 @@ function validateDependencyCompose(dependencyKey, dependency, document, context)
|
|
|
672
789
|
if (svc.privileged === true) fail(`services.${name}.privileged`, `privileged services are forbidden`);
|
|
673
790
|
if (svc.container_name !== void 0) fail(`services.${name}.container_name`, `fixed container_name is forbidden`);
|
|
674
791
|
for (const v of Array.isArray(svc.volumes) ? svc.volumes : []) {
|
|
675
|
-
const
|
|
676
|
-
if (
|
|
792
|
+
const mount = parseVolume(v);
|
|
793
|
+
if (mount?.type === "bind" && mount.source && isHostFilesystemRoot(mount.source)) fail(`services.${name}.volumes`, "host root bind mounts are forbidden", v);
|
|
677
794
|
}
|
|
678
795
|
const internalRefs = [];
|
|
679
796
|
if (Array.isArray(svc.depends_on)) {
|
|
@@ -688,6 +805,12 @@ function validateDependencyCompose(dependencyKey, dependency, document, context)
|
|
|
688
805
|
if (Array.isArray(svc.volumes_from)) {
|
|
689
806
|
for (const ref of svc.volumes_from) if (typeof ref === "string") internalRefs.push(["volumes_from", ref.split(":")[0]]);
|
|
690
807
|
}
|
|
808
|
+
for (const key of ["ipc", "pid"]) if (typeof svc[key] === "string" && svc[key].startsWith("service:")) internalRefs.push([key, svc[key].slice(8)]);
|
|
809
|
+
if (svc.extends && typeof svc.extends === "object") {
|
|
810
|
+
const ref = svc.extends;
|
|
811
|
+
if (ref.file !== void 0) fail(`services.${name}.extends.file`, "external extends is unsupported for atomic dependency implementations");
|
|
812
|
+
if (typeof ref.service === "string") internalRefs.push(["extends.service", ref.service]);
|
|
813
|
+
}
|
|
691
814
|
for (const [property, target] of internalRefs) if (!services[target]) fail(`services.${name}.${property}`, `internal service reference "${target}" does not exist in this dependency Compose bundle`, target);
|
|
692
815
|
}
|
|
693
816
|
if (!Object.keys(dependency.endpoints).length) return;
|
|
@@ -704,11 +827,15 @@ function validateDependencyCompose(dependencyKey, dependency, document, context)
|
|
|
704
827
|
if (!health || health.disable === true || !health.test || typeof health.test === "string" && !health.test.trim() || Array.isArray(health.test) && (!health.test.length || health.test[0] === "NONE" || health.test.every((v) => typeof v === "string" && !v.trim()))) fail(`services.${owner}.healthcheck`, `endpoint owner requires an enabled, non-empty healthcheck`);
|
|
705
828
|
}
|
|
706
829
|
}
|
|
707
|
-
|
|
708
|
-
|
|
830
|
+
function canonicalBuildContext(value, baseDir) {
|
|
831
|
+
return canonicalPath(path.resolve(baseDir, value));
|
|
832
|
+
}
|
|
833
|
+
function isSameBuildContext(actual, expected, baseDir) {
|
|
834
|
+
return canonicalBuildContext(actual, baseDir) === canonicalBuildContext(expected, baseDir);
|
|
835
|
+
}
|
|
709
836
|
function validateAndPrepareAppOverride(document, context) {
|
|
710
837
|
const fail = (path20, reason, value) => {
|
|
711
|
-
throw new ConfigError({ sourceFile: context.file, configPath: path20, stage: context.stage, composition: "service", ...{} , reason });
|
|
838
|
+
throw new ConfigError({ sourceFile: context.file, configPath: path20, stage: context.stage, composition: "service", ...value === void 0 ? {} : { value }, reason });
|
|
712
839
|
};
|
|
713
840
|
const keys = Object.keys(document.services ?? {});
|
|
714
841
|
if (keys.length !== 1 || keys[0] !== "app") fail("services", "override must define exactly one service named app");
|
|
@@ -718,10 +845,24 @@ function validateAndPrepareAppOverride(document, context) {
|
|
|
718
845
|
const health = app.healthcheck;
|
|
719
846
|
for (const key of Object.keys(health)) if (!["interval", "timeout", "retries", "start_period", "start_interval"].includes(key)) fail(`services.app.healthcheck.${key}`, "override may set only healthcheck timing fields");
|
|
720
847
|
}
|
|
848
|
+
if (typeof app.build === "string") {
|
|
849
|
+
fail("services.app.build", "override cannot replace Builder-owned app build context; use object-form build.args for build arguments", app.build);
|
|
850
|
+
}
|
|
721
851
|
if (app.build && typeof app.build === "object") {
|
|
722
|
-
const
|
|
723
|
-
for (const
|
|
724
|
-
|
|
852
|
+
const build = app.build;
|
|
853
|
+
for (const key of ["target", "dockerfile"]) {
|
|
854
|
+
if (build[key] !== void 0) {
|
|
855
|
+
fail(`services.app.build.${key}`, "override cannot change Builder-owned build configuration");
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
if (build.context !== void 0) {
|
|
859
|
+
if (typeof build.context !== "string") {
|
|
860
|
+
fail("services.app.build.context", "override app build context must be a string", build.context);
|
|
861
|
+
}
|
|
862
|
+
if (!isSameBuildContext(build.context, context.expectedBuildContext, context.buildContextBaseDir)) {
|
|
863
|
+
fail("services.app.build.context", "override cannot change app build context", build.context);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
725
866
|
}
|
|
726
867
|
const env = normalizeComposeEnvironment(app.environment, "services.app.environment");
|
|
727
868
|
for (const k of ["EXEC_MODE", "INFRA_MODE"]) if (k in env) fail(`services.app.environment.${k}`, "override cannot set Builder-owned environment");
|
|
@@ -729,27 +870,25 @@ function validateAndPrepareAppOverride(document, context) {
|
|
|
729
870
|
const resourceKeys = ["volumes", "networks", "configs", "secrets"];
|
|
730
871
|
const resources = {};
|
|
731
872
|
for (const kind of resourceKeys) {
|
|
732
|
-
const defs = document[kind] ?? {};
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
else if (
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
const mapped = {};
|
|
744
|
-
for (const [name, def] of Object.entries(defs)) {
|
|
873
|
+
const defs = document[kind] ?? {}, refs2 = /* @__PURE__ */ new Set(), value = app[kind];
|
|
874
|
+
if (Array.isArray(value)) for (const ref of value) {
|
|
875
|
+
if (kind === "volumes") {
|
|
876
|
+
const volume = parseVolume(ref);
|
|
877
|
+
if (volume?.type === "volume" && volume.source) refs2.add(volume.source);
|
|
878
|
+
} else if (typeof ref === "string") refs2.add(ref);
|
|
879
|
+
else if (ref && typeof ref === "object" && typeof ref.source === "string") refs2.add(ref.source);
|
|
880
|
+
}
|
|
881
|
+
else if (value && typeof value === "object") for (const name of Object.keys(value)) refs2.add(name);
|
|
882
|
+
const names = /* @__PURE__ */ new Map(), mapped = {};
|
|
883
|
+
for (const [name, definition] of Object.entries(defs)) {
|
|
745
884
|
if (!refs2.has(name)) fail(`${kind}.${name}`, "override resource must be consumed by app");
|
|
746
885
|
const target = `app-override--${context.stage}--${kind.slice(0, -1)}--${name}`;
|
|
747
|
-
|
|
748
|
-
|
|
886
|
+
names.set(name, target);
|
|
887
|
+
mapped[target] = definition;
|
|
749
888
|
}
|
|
750
|
-
if (
|
|
751
|
-
if (app[kind]
|
|
752
|
-
else if (
|
|
889
|
+
if (names.size) resources[kind] = mapped;
|
|
890
|
+
if (Array.isArray(value)) app[kind] = value.map((ref) => kind === "volumes" ? namespaceVolume(ref, names) : typeof ref === "string" ? names.get(ref) ?? ref : ref && typeof ref === "object" && typeof ref.source === "string" ? { ...ref, source: names.get(ref.source) ?? ref.source } : ref);
|
|
891
|
+
else if (value && typeof value === "object") app[kind] = Object.fromEntries(Object.entries(value).map(([name, definition]) => [names.get(name) ?? name, definition]));
|
|
753
892
|
}
|
|
754
893
|
return { app, resources };
|
|
755
894
|
}
|
|
@@ -860,10 +999,10 @@ function addPorts(service, endpointPairs) {
|
|
|
860
999
|
}
|
|
861
1000
|
}
|
|
862
1001
|
function compileServiceComposition(stage, projectDir, options = {}) {
|
|
863
|
-
const root = resolveProjectDir(projectDir), buildPath =
|
|
864
|
-
|
|
1002
|
+
const root = resolveProjectDir(projectDir), buildPath = path.join(root, "build.json"), raw = getBuildJson(root), effective = resolveBuildStage(raw, stage, buildPath), profile = getExecutionProfile(stage, Boolean(options.full));
|
|
1003
|
+
resolveApplicationDockerfile(root, raw.dockerfile ?? "Dockerfile", stage);
|
|
865
1004
|
const seed = getTemplateSeed(stage, Boolean(options.meshMode));
|
|
866
|
-
const rawName = options.projectNameOverride ?? raw.name ??
|
|
1005
|
+
const rawName = options.projectNameOverride ?? raw.name ?? path.basename(root);
|
|
867
1006
|
const projectName = deriveProjectName(rawName, stage);
|
|
868
1007
|
const namespaced = {};
|
|
869
1008
|
for (const [key, dep] of Object.entries(effective.dependencies).sort(([a], [b]) => a.localeCompare(b))) {
|
|
@@ -937,7 +1076,7 @@ function compileServiceComposition(stage, projectDir, options = {}) {
|
|
|
937
1076
|
}
|
|
938
1077
|
const appEnv = options.deferReferenceResolution ? ownEnv(effective, stage) : resolveEnv(ownEnv(effective, stage), registry, "app.env");
|
|
939
1078
|
let buildAppEnvironment = { ...appEnv };
|
|
940
|
-
const runtimeDotenv =
|
|
1079
|
+
const runtimeDotenv = path.join(root, `.env.${profile.runtimeStage}`);
|
|
941
1080
|
if (fs.existsSync(runtimeDotenv)) {
|
|
942
1081
|
const text = fs.readFileSync(runtimeDotenv, "utf8");
|
|
943
1082
|
for (const line of text.split(/\r?\n/)) {
|
|
@@ -947,14 +1086,14 @@ function compileServiceComposition(stage, projectDir, options = {}) {
|
|
|
947
1086
|
}
|
|
948
1087
|
const internal = { EXEC_MODE: profile.runtimeStage, INFRA_MODE: profile.integrated ? "integrated" : "isolated", ...getPrimaryEndpoint(effective.app.endpoints) ? { PORT: String(getPrimaryEndpoint(effective.app.endpoints)[1].port) } : {} };
|
|
949
1088
|
const appBuild = app.build && typeof app.build === "object" ? app.build : { context: "." };
|
|
950
|
-
const outputDir = options.composeOutputDir ?
|
|
951
|
-
const serviceContext =
|
|
1089
|
+
const outputDir = options.composeOutputDir ? path.resolve(options.composeOutputDir) : root;
|
|
1090
|
+
const serviceContext = path.relative(outputDir, root).replace(/\\/g, "/") || ".";
|
|
952
1091
|
appBuild.context = options.meshMode ? serviceContext : ".";
|
|
953
1092
|
if (raw.dockerfile) appBuild.dockerfile = raw.dockerfile;
|
|
954
1093
|
else delete appBuild.dockerfile;
|
|
955
1094
|
appBuild.target = profile.appTarget;
|
|
956
1095
|
app.build = appBuild;
|
|
957
|
-
app.env_file = [{ path: options.meshMode ?
|
|
1096
|
+
app.env_file = [{ path: options.meshMode ? path.posix.join(serviceContext, `.env.${profile.runtimeStage}`) : `.env.${profile.runtimeStage}`, required: false }];
|
|
958
1097
|
app.environment = { ...appEnv, ...options.externalAppEnv ? Object.fromEntries(Object.entries(options.externalAppEnv).map(([k, v]) => [k, v.replace(/\$/g, "$$$$")])) : {}, ...internal };
|
|
959
1098
|
if (stage === "dev") app.volumes = [`${options.meshMode && serviceContext !== "." ? `./${serviceContext}` : "."}:/app`, "/app/node_modules"];
|
|
960
1099
|
for (const [name, ep] of Object.entries(appEndpoints)) if (!options.meshMode && ep.effectivelyPublished) addPorts(app, [[ep, ep.publicPort]]);
|
|
@@ -975,9 +1114,24 @@ function compileServiceComposition(stage, projectDir, options = {}) {
|
|
|
975
1114
|
for (const [epName, ep] of Object.entries(depEndpoints[key])) dependency.endpoints[epName] = ep;
|
|
976
1115
|
}
|
|
977
1116
|
if (effective.overrideCompose) {
|
|
978
|
-
const overridePath = resolveInsideProject(root, effective.overrideCompose, {
|
|
979
|
-
|
|
980
|
-
|
|
1117
|
+
const overridePath = resolveInsideProject(root, effective.overrideCompose, {
|
|
1118
|
+
sourceFile: buildPath,
|
|
1119
|
+
configPath: "override-compose",
|
|
1120
|
+
stage
|
|
1121
|
+
});
|
|
1122
|
+
const loadedOverride = loadComposeDocument(overridePath, {
|
|
1123
|
+
sourceFile: buildPath,
|
|
1124
|
+
configPath: "override-compose",
|
|
1125
|
+
stage,
|
|
1126
|
+
composition: "service"
|
|
1127
|
+
});
|
|
1128
|
+
const overrideDocument = rebaseAppOverridePaths(loadedOverride, root, outputDir);
|
|
1129
|
+
const prepared = validateAndPrepareAppOverride(overrideDocument, {
|
|
1130
|
+
file: overridePath,
|
|
1131
|
+
stage,
|
|
1132
|
+
expectedBuildContext: String(appBuild.context),
|
|
1133
|
+
buildContextBaseDir: outputDir
|
|
1134
|
+
});
|
|
981
1135
|
for (const section of ["volumes", "networks", "configs", "secrets"]) if (prepared.resources[section]) mergeDocument(compose, { services: {}, [section]: prepared.resources[section] }, "app override");
|
|
982
1136
|
const rawOverrideEnv = normalizeComposeEnvironment(prepared.app.environment, "override-compose.services.app.environment");
|
|
983
1137
|
const overrideEnv = options.deferReferenceResolution ? rawOverrideEnv : resolveEnv(rawOverrideEnv, registry, "override-compose.services.app.environment");
|
|
@@ -990,7 +1144,7 @@ function compileServiceComposition(stage, projectDir, options = {}) {
|
|
|
990
1144
|
merged.build = appBuild;
|
|
991
1145
|
merged.env_file = app.env_file;
|
|
992
1146
|
merged.depends_on = app.depends_on;
|
|
993
|
-
merged.networks = { default: null, ...app.networks
|
|
1147
|
+
merged.networks = { ...!options.meshMode ? { default: null } : {}, ...normalizeNetworks(app.networks), ...normalizeNetworks(prepared.app.networks) };
|
|
994
1148
|
compose.services.app = merged;
|
|
995
1149
|
app = merged;
|
|
996
1150
|
}
|
|
@@ -1120,7 +1274,7 @@ var meshSchema = z.object({ $schema: z.string().optional(), name: z.string().opt
|
|
|
1120
1274
|
|
|
1121
1275
|
// src/core/mesh/getMeshJson.ts
|
|
1122
1276
|
function getMeshJson(projectDir = process.cwd()) {
|
|
1123
|
-
const absDir = resolveProjectDir(projectDir), meshPath =
|
|
1277
|
+
const absDir = resolveProjectDir(projectDir), meshPath = path.resolve(absDir, "mesh.json");
|
|
1124
1278
|
if (!existsSync(meshPath)) throw new NotFoundError(`mesh.json not found in directory: ${absDir}`);
|
|
1125
1279
|
let content;
|
|
1126
1280
|
try {
|
|
@@ -1173,9 +1327,9 @@ function resolveMeshStage(mesh, stage, sourceFile) {
|
|
|
1173
1327
|
function loadEffectiveMeshServices(meshDir, effectiveMesh, stage) {
|
|
1174
1328
|
const out = {};
|
|
1175
1329
|
for (const [name, svc] of Object.entries(effectiveMesh.services).sort(([a], [b]) => a.localeCompare(b))) {
|
|
1176
|
-
const abs = resolveInsideProject(meshDir, svc.path, { sourceFile:
|
|
1177
|
-
if (!fs.existsSync(abs) || !fs.statSync(abs).isDirectory()) throw new ConfigError({ sourceFile:
|
|
1178
|
-
const buildJsonPath =
|
|
1330
|
+
const abs = resolveInsideProject(meshDir, svc.path, { sourceFile: path.join(meshDir, "mesh.json"), configPath: `services.${name}.path`, stage });
|
|
1331
|
+
if (!fs.existsSync(abs) || !fs.statSync(abs).isDirectory()) throw new ConfigError({ sourceFile: path.join(meshDir, "mesh.json"), configPath: `services.${name}.path`, stage, composition: "mesh", value: svc.path, reason: "Mesh service directory does not exist" });
|
|
1332
|
+
const buildJsonPath = path.join(abs, "build.json"), build = getBuildJson(abs), effectiveBuild = resolveBuildStage(build, stage, buildJsonPath);
|
|
1179
1333
|
out[name] = { name, path: svc.path, absolutePath: abs, buildJsonPath, effectiveBuild, meshEnv: { ...svc.env ?? {} }, dependencyBindings: { ...svc.dependencies ?? {} }, dependsOn: [...svc.dependsOn ?? []], publish: [...svc.publish ?? []] };
|
|
1180
1334
|
}
|
|
1181
1335
|
return out;
|
|
@@ -1417,8 +1571,8 @@ function setEnv(service, env) {
|
|
|
1417
1571
|
service.environment = { ...current, ...env };
|
|
1418
1572
|
}
|
|
1419
1573
|
function compileMeshComposition(stage, meshDir, options = {}) {
|
|
1420
|
-
const meshPath =
|
|
1421
|
-
const rawName = raw.name ??
|
|
1574
|
+
const meshPath = path.join(meshDir, "mesh.json"), raw = getMeshJson(meshDir), effective = resolveMeshStage(raw, stage, meshPath), services = loadEffectiveMeshServices(meshDir, effective, stage), bindings = buildDependencyBindings(services, stage, meshPath), bindingMap = bindingsMap(bindings);
|
|
1575
|
+
const rawName = raw.name ?? path.basename(meshDir), projectName = deriveProjectName(rawName, stage, meshDir);
|
|
1422
1576
|
const fragments = {};
|
|
1423
1577
|
const compose = { name: projectName, services: {}, networks: { mesh: { driver: "bridge" } } };
|
|
1424
1578
|
for (const [name, svc] of Object.entries(services).sort(([a], [b]) => a.localeCompare(b))) {
|
|
@@ -1439,20 +1593,20 @@ function compileMeshComposition(stage, meshDir, options = {}) {
|
|
|
1439
1593
|
const owner = ep.networkHost;
|
|
1440
1594
|
const ownerService = compose.services[owner];
|
|
1441
1595
|
if (ownerService) {
|
|
1442
|
-
const old =
|
|
1596
|
+
const old = normalizeNetworks(ownerService.networks);
|
|
1443
1597
|
old[privateName] = null;
|
|
1444
1598
|
ownerService.networks = old;
|
|
1445
1599
|
}
|
|
1446
1600
|
}
|
|
1447
1601
|
for (const generated of Object.keys(fragment.compose.services).filter((k) => k.startsWith(`svc--${name}--dep--${dep}--`))) {
|
|
1448
1602
|
const ownerService = compose.services[generated];
|
|
1449
|
-
const old =
|
|
1603
|
+
const old = normalizeNetworks(ownerService.networks);
|
|
1450
1604
|
old[privateName] = null;
|
|
1451
1605
|
ownerService.networks = old;
|
|
1452
1606
|
}
|
|
1453
1607
|
}
|
|
1454
1608
|
}
|
|
1455
|
-
app.networks = networks;
|
|
1609
|
+
app.networks = { ...normalizeNetworks(app.networks), ...networks };
|
|
1456
1610
|
}
|
|
1457
1611
|
const publication = resolveMeshPublication(services, bindingMap, stage, meshPath), intents = [];
|
|
1458
1612
|
for (const name of Object.keys(services).sort()) for (const pub of publication.filter((p) => p.ownerMeshService === name && p.target === "app").sort((a, b) => a.endpoint.localeCompare(b.endpoint))) {
|
|
@@ -1557,7 +1711,7 @@ function compileMeshComposition(stage, meshDir, options = {}) {
|
|
|
1557
1711
|
|
|
1558
1712
|
// src/core/mesh/checkers/checkMeshServices.ts
|
|
1559
1713
|
function checkMeshServices(mesh, meshDir) {
|
|
1560
|
-
const root = resolveProjectDir(meshDir), meshPath =
|
|
1714
|
+
const root = resolveProjectDir(meshDir), meshPath = path.join(root, "mesh.json");
|
|
1561
1715
|
const checked = /* @__PURE__ */ new Set();
|
|
1562
1716
|
for (const stage of ["dev", "prod", "test", "e2e"]) {
|
|
1563
1717
|
const effective = resolveMeshStage(mesh, stage, meshPath);
|
|
@@ -1587,11 +1741,11 @@ function checkMeshServices(mesh, meshDir) {
|
|
|
1587
1741
|
function checkMeshTester(mesh, meshDir, options = {}) {
|
|
1588
1742
|
const root = resolveProjectDir(meshDir);
|
|
1589
1743
|
if (!mesh.tester) {
|
|
1590
|
-
if (options.required) throw new ConfigError({ sourceFile:
|
|
1744
|
+
if (options.required) throw new ConfigError({ sourceFile: path.join(root, "mesh.json"), configPath: "tester", stage: "e2e", composition: "mesh", reason: "Global mesh E2E requires root tester" });
|
|
1591
1745
|
return;
|
|
1592
1746
|
}
|
|
1593
|
-
const file = resolveInsideProject(root, mesh.tester.compose, { sourceFile:
|
|
1594
|
-
loadComposeDocument(file, { sourceFile:
|
|
1747
|
+
const file = resolveInsideProject(root, mesh.tester.compose, { sourceFile: path.join(root, "mesh.json"), configPath: "tester.compose", stage: "e2e" });
|
|
1748
|
+
loadComposeDocument(file, { sourceFile: path.join(root, "mesh.json"), configPath: "tester.compose", stage: "e2e", composition: "mesh" });
|
|
1595
1749
|
compileMeshComposition("e2e", root, { validationMode: true });
|
|
1596
1750
|
}
|
|
1597
1751
|
|
|
@@ -1803,8 +1957,8 @@ async function teardownCompose(yamlConfig, projectDir, options = {}) {
|
|
|
1803
1957
|
}
|
|
1804
1958
|
const inputData = options.projectName ? void 0 : yamlConfig;
|
|
1805
1959
|
if (!removeVolumes) {
|
|
1806
|
-
const
|
|
1807
|
-
await runSpawn("docker",
|
|
1960
|
+
const services = yamlConfig.trim() ? anonymousVolumeServices(parse(yamlConfig)?.services ?? {}) : options.anonymousVolumeServices ?? [];
|
|
1961
|
+
if (services.length) await runSpawn("docker", [...baseArgs, "rm", "-s", "-f", "-v", ...services], absProjectDir, inputData, true, 15e3);
|
|
1808
1962
|
}
|
|
1809
1963
|
const downArgs = [...baseArgs, "down"];
|
|
1810
1964
|
if (removeVolumes) {
|
|
@@ -1861,12 +2015,12 @@ function redactYamlSecrets(yamlContent) {
|
|
|
1861
2015
|
return redacted;
|
|
1862
2016
|
}
|
|
1863
2017
|
function getRunStatePath(projectDir) {
|
|
1864
|
-
return
|
|
2018
|
+
return path.resolve(resolveProjectDir(projectDir), ".builder", "run.state");
|
|
1865
2019
|
}
|
|
1866
2020
|
function writeRunState(projectDir, state) {
|
|
1867
2021
|
try {
|
|
1868
2022
|
const absDir = resolveProjectDir(projectDir);
|
|
1869
|
-
const builderDir =
|
|
2023
|
+
const builderDir = path.resolve(absDir, ".builder");
|
|
1870
2024
|
if (!existsSync(builderDir)) {
|
|
1871
2025
|
mkdirSync(builderDir, { recursive: true });
|
|
1872
2026
|
}
|
|
@@ -1875,7 +2029,7 @@ function writeRunState(projectDir, state) {
|
|
|
1875
2029
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1876
2030
|
};
|
|
1877
2031
|
writeFileSync(
|
|
1878
|
-
|
|
2032
|
+
path.resolve(builderDir, "run.state"),
|
|
1879
2033
|
JSON.stringify(fullState, null, 2),
|
|
1880
2034
|
"utf-8"
|
|
1881
2035
|
);
|
|
@@ -1906,9 +2060,9 @@ function projectNamesForServiceStage(projectDir, stage) {
|
|
|
1906
2060
|
let rawName;
|
|
1907
2061
|
try {
|
|
1908
2062
|
const buildJson = getBuildJson(absDir);
|
|
1909
|
-
rawName = buildJson.name ||
|
|
2063
|
+
rawName = buildJson.name || path.basename(absDir);
|
|
1910
2064
|
} catch {
|
|
1911
|
-
rawName =
|
|
2065
|
+
rawName = path.basename(absDir);
|
|
1912
2066
|
}
|
|
1913
2067
|
const s = stage.toLowerCase();
|
|
1914
2068
|
if (s === "dev") {
|
|
@@ -1937,10 +2091,10 @@ function projectNamesForMeshStage(meshDir, stage) {
|
|
|
1937
2091
|
let serviceNames = [];
|
|
1938
2092
|
try {
|
|
1939
2093
|
const mesh = getMeshJson(absDir);
|
|
1940
|
-
rawMeshName = mesh.name ||
|
|
1941
|
-
serviceNames = Object.keys(resolveMeshStage(mesh, "test",
|
|
2094
|
+
rawMeshName = mesh.name || path.basename(absDir);
|
|
2095
|
+
serviceNames = Object.keys(resolveMeshStage(mesh, "test", path.join(absDir, "mesh.json")).services);
|
|
1942
2096
|
} catch {
|
|
1943
|
-
rawMeshName =
|
|
2097
|
+
rawMeshName = path.basename(absDir);
|
|
1944
2098
|
}
|
|
1945
2099
|
const s = stage.toLowerCase();
|
|
1946
2100
|
if (s === "dev") {
|
|
@@ -1995,6 +2149,7 @@ async function handleMeshDown(rawStage = "dev", options = {}) {
|
|
|
1995
2149
|
for (const proj of uniqueProjects) {
|
|
1996
2150
|
await teardownCompose("", meshDir, {
|
|
1997
2151
|
projectName: proj,
|
|
2152
|
+
...runState?.projectName === proj && runState.anonymousVolumeServices ? { anonymousVolumeServices: runState.anonymousVolumeServices } : {},
|
|
1998
2153
|
removeVolumes: Boolean(options.volumes)
|
|
1999
2154
|
});
|
|
2000
2155
|
}
|
|
@@ -2162,6 +2317,7 @@ async function runMeshDev(options = {}) {
|
|
|
2162
2317
|
await checkHostPortCollisions(hostPorts);
|
|
2163
2318
|
writeRunState(meshDir, {
|
|
2164
2319
|
projectName,
|
|
2320
|
+
anonymousVolumeServices: anonymousVolumeServices(parsed?.services ?? {}),
|
|
2165
2321
|
stage: "dev",
|
|
2166
2322
|
projectDir: meshDir
|
|
2167
2323
|
});
|
|
@@ -2203,6 +2359,7 @@ async function runMeshProd(options = {}) {
|
|
|
2203
2359
|
await checkHostPortCollisions(hostPorts);
|
|
2204
2360
|
writeRunState(meshDir, {
|
|
2205
2361
|
projectName,
|
|
2362
|
+
anonymousVolumeServices: anonymousVolumeServices(parsed?.services ?? {}),
|
|
2206
2363
|
stage: "prod",
|
|
2207
2364
|
projectDir: meshDir
|
|
2208
2365
|
});
|
|
@@ -2224,7 +2381,7 @@ async function runMeshProd(options = {}) {
|
|
|
2224
2381
|
return status;
|
|
2225
2382
|
}
|
|
2226
2383
|
async function runMeshTestUnit(options = {}) {
|
|
2227
|
-
const meshDir = resolveProjectDir(options.projectDir), meshPath =
|
|
2384
|
+
const meshDir = resolveProjectDir(options.projectDir), meshPath = path.join(meshDir, "mesh.json"), raw = getMeshJson(meshDir), effective = resolveMeshStage(raw, "test", meshPath), services = loadEffectiveMeshServices(meshDir, effective, "test");
|
|
2228
2385
|
console.log(pc22.bold(pc22.blue("\n[1/2] MESH SERVICE UNIT/ISOLATED TESTS\n")));
|
|
2229
2386
|
for (const [name, svc] of Object.entries(services).sort(([a], [b]) => a.localeCompare(b))) {
|
|
2230
2387
|
console.log(pc22.cyan(`-> Running isolated unit tests for [${name}]`));
|
|
@@ -2238,7 +2395,7 @@ async function runMeshTestUnit(options = {}) {
|
|
|
2238
2395
|
const resolved = resolveMeshEnvMap(env, refCtx, `services.${name}.env`);
|
|
2239
2396
|
const app = compiled.compose.services.app;
|
|
2240
2397
|
app.environment = { ...app.environment, ...resolvedBuild, ...resolved };
|
|
2241
|
-
const projectName = `${deriveProjectName(raw.name ??
|
|
2398
|
+
const projectName = `${deriveProjectName(raw.name ?? path.basename(meshDir), "dev")}-${name}-test`;
|
|
2242
2399
|
compiled.compose.name = projectName;
|
|
2243
2400
|
const yaml = stringify(compiled.compose);
|
|
2244
2401
|
if (options.debug) console.log(yaml);
|
|
@@ -2261,7 +2418,6 @@ async function runMeshTestUnit(options = {}) {
|
|
|
2261
2418
|
}
|
|
2262
2419
|
async function runMeshTestE2e(options = {}) {
|
|
2263
2420
|
const meshDir = resolveProjectDir(options.projectDir), mesh = getMeshJson(meshDir);
|
|
2264
|
-
if (!options.skipChecks) checkMesh(meshDir, { requireTester: true });
|
|
2265
2421
|
if (!mesh.tester) throw new ScriptError("Global mesh E2E requires root mesh.json.tester { compose, service, env? }");
|
|
2266
2422
|
const { yaml, testerServiceName } = compileMeshEnvironment("e2e", meshDir);
|
|
2267
2423
|
if (!testerServiceName) throw new ScriptError("Global mesh E2E tester runner could not be compiled.");
|
|
@@ -2302,6 +2458,9 @@ async function handleMeshUp(rawStage = "dev", options = {}) {
|
|
|
2302
2458
|
...options,
|
|
2303
2459
|
projectDir
|
|
2304
2460
|
};
|
|
2461
|
+
if (["dev", "prod", "test-unit", "test-e2e", "test"].includes(stage) && !options.skipChecks) {
|
|
2462
|
+
checkMesh(projectDir, { requireTester: stage === "test-e2e" || stage === "test" });
|
|
2463
|
+
}
|
|
2305
2464
|
switch (stage) {
|
|
2306
2465
|
case "dev":
|
|
2307
2466
|
return await runMeshDev(runOptions);
|
|
@@ -2455,6 +2614,7 @@ async function handleServiceDown(rawStage = "dev", options = {}) {
|
|
|
2455
2614
|
for (const proj of uniqueProjects) {
|
|
2456
2615
|
await teardownCompose("", projectDir, {
|
|
2457
2616
|
projectName: proj,
|
|
2617
|
+
...runState?.projectName === proj && runState.anonymousVolumeServices ? { anonymousVolumeServices: runState.anonymousVolumeServices } : {},
|
|
2458
2618
|
removeVolumes: Boolean(options.volumes)
|
|
2459
2619
|
});
|
|
2460
2620
|
}
|
|
@@ -2523,6 +2683,7 @@ var runDev = async (options) => {
|
|
|
2523
2683
|
await checkHostPortCollisions(hostPorts);
|
|
2524
2684
|
writeRunState(options.projectDir, {
|
|
2525
2685
|
projectName,
|
|
2686
|
+
anonymousVolumeServices: anonymousVolumeServices(parsed?.services ?? {}),
|
|
2526
2687
|
stage: "dev",
|
|
2527
2688
|
projectDir: options.projectDir
|
|
2528
2689
|
});
|
|
@@ -2566,6 +2727,7 @@ var runProd = async (options) => {
|
|
|
2566
2727
|
await checkHostPortCollisions(hostPorts);
|
|
2567
2728
|
writeRunState(options.projectDir, {
|
|
2568
2729
|
projectName,
|
|
2730
|
+
anonymousVolumeServices: anonymousVolumeServices(parsed?.services ?? {}),
|
|
2569
2731
|
stage: "prod",
|
|
2570
2732
|
projectDir: options.projectDir
|
|
2571
2733
|
});
|
|
@@ -2902,7 +3064,7 @@ async function handleExportEnv(rawStage, options = {}) {
|
|
|
2902
3064
|
const files = [];
|
|
2903
3065
|
try {
|
|
2904
3066
|
for (const snapshot of snapshots) {
|
|
2905
|
-
let filename =
|
|
3067
|
+
let filename = path.join(projectDir, `.env.${snapshot.stage}`);
|
|
2906
3068
|
while (true) {
|
|
2907
3069
|
let overwrite = false;
|
|
2908
3070
|
if (existsSync(filename)) {
|
|
@@ -2911,11 +3073,11 @@ async function handleExportEnv(rawStage, options = {}) {
|
|
|
2911
3073
|
if (!overwrite) {
|
|
2912
3074
|
const name = (await ask(`New filename for ${snapshot.stage} (relative to the service directory): `)).trim();
|
|
2913
3075
|
if (!name) continue;
|
|
2914
|
-
filename =
|
|
3076
|
+
filename = path.resolve(projectDir, name);
|
|
2915
3077
|
continue;
|
|
2916
3078
|
}
|
|
2917
3079
|
}
|
|
2918
|
-
mkdirSync(
|
|
3080
|
+
mkdirSync(path.dirname(filename), { recursive: true });
|
|
2919
3081
|
try {
|
|
2920
3082
|
writeFileSync(filename, snapshot.content, { encoding: "utf-8", flag: overwrite ? "w" : "wx", mode: 384 });
|
|
2921
3083
|
} catch (error) {
|