@solid-stack/builder 1.0.7 → 1.1.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/dist/index.js CHANGED
@@ -482,17 +482,21 @@ var meshHealthcheckSchema = z.union([
482
482
  }).passthrough()
483
483
  ]);
484
484
  var meshServiceSchema = z.object({
485
- path: z.string({ required_error: 'Service "path" is required' }),
486
- port: z.union([z.number(), z.string()]).optional(),
487
- provideDependency: z.record(z.string(), z.string()).optional(),
488
- replaceMocks: z.record(z.string(), z.string()).optional(),
489
- envOverrides: z.record(z.string(), z.string()).optional(),
485
+ path: z.string({ required_error: 'Service "path" is required' }).describe("Relative path to service directory containing build.json"),
486
+ port: z.union([z.number(), z.string()]).optional().describe("Public host port mapped to internal port 3000"),
487
+ preservePort: z.boolean().optional().describe("If true, use the port declared in build.json"),
488
+ provideDependency: z.record(z.string(), z.string()).optional().describe("Map consumer dependency key to fellow mesh service provider"),
489
+ replaceMocks: z.record(z.string(), z.string()).optional().describe("Alias for provideDependency"),
490
+ envOverrides: z.record(z.string(), z.string()).optional().describe("Environment variable overrides with URL templating"),
490
491
  healthcheck: meshHealthcheckSchema.optional()
491
492
  }).strict();
492
493
  var meshTesterSchema = z.object({
493
- path: z.string({ required_error: 'Tester "path" is required' }),
494
- compose: z.string().optional(),
495
- dockerfile: z.string().optional(),
494
+ path: z.string({ required_error: 'Tester "path" is required' }).describe("Relative path to E2E tester directory"),
495
+ compose: z.string().optional().describe("Tester compose file (defaults to 'docker-compose.yml')"),
496
+ dockerfile: z.string().optional().describe("Tester Dockerfile (defaults to 'Dockerfile')"),
497
+ envOverrides: z.record(z.string(), z.string()).optional().describe(
498
+ "Environment variable overrides for global tester with URL templating"
499
+ ),
496
500
  policy: z.object({
497
501
  dockerfile: z.string().optional(),
498
502
  compose: z.string().optional()
@@ -500,6 +504,7 @@ var meshTesterSchema = z.object({
500
504
  }).strict();
501
505
  var meshServiceNameRegex = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
502
506
  var meshSchema = z.object({
507
+ $schema: z.string().optional(),
503
508
  name: z.string().optional(),
504
509
  services: z.record(
505
510
  z.string().regex(meshServiceNameRegex, {
@@ -1145,6 +1150,67 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1145
1150
  providerAliases.set(sName, /* @__PURE__ */ new Set());
1146
1151
  }
1147
1152
  const globalDepAliasToProvider = /* @__PURE__ */ new Map();
1153
+ const INTERNAL_PORT = 3e3;
1154
+ const serviceUrls = /* @__PURE__ */ new Map();
1155
+ for (const [serviceName, serviceConfig] of Object.entries(mesh.services)) {
1156
+ const serviceDir = path13.resolve(meshDir, serviceConfig.path);
1157
+ const serviceBuildJson = getBuildJson(serviceDir);
1158
+ let publicPort = null;
1159
+ if (serviceConfig.port !== void 0 && serviceConfig.port !== null && serviceConfig.port !== "") {
1160
+ const p = Number(serviceConfig.port);
1161
+ if (!isNaN(p) && p > 0) {
1162
+ publicPort = p;
1163
+ }
1164
+ } else if (serviceConfig.preservePort && serviceBuildJson.port) {
1165
+ const p = Number(serviceBuildJson.port);
1166
+ if (!isNaN(p) && p > 0) {
1167
+ publicPort = p;
1168
+ }
1169
+ }
1170
+ serviceUrls.set(serviceName, {
1171
+ networkUrl: `http://${serviceName}:${INTERNAL_PORT}`,
1172
+ publicUrl: publicPort ? `http://localhost:${publicPort}` : null
1173
+ });
1174
+ }
1175
+ function escapeRegExp(string) {
1176
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1177
+ }
1178
+ function interpolateEnvTemplates(value, context) {
1179
+ if (typeof value !== "string") {
1180
+ return value;
1181
+ }
1182
+ let interpolated = value;
1183
+ for (const [sName, urls] of serviceUrls.entries()) {
1184
+ interpolated = interpolated.replace(
1185
+ new RegExp(`\\$\\{${escapeRegExp(sName)}\\.network_url\\}`, "g"),
1186
+ urls.networkUrl
1187
+ );
1188
+ if (urls.publicUrl) {
1189
+ interpolated = interpolated.replace(
1190
+ new RegExp(`\\$\\{${escapeRegExp(sName)}\\.public_url\\}`, "g"),
1191
+ urls.publicUrl
1192
+ );
1193
+ }
1194
+ }
1195
+ const unresolvedMatch = interpolated.match(/\$\{([^}]+?)\.(network_url|public_url)\}/);
1196
+ if (unresolvedMatch) {
1197
+ const variable = unresolvedMatch[0];
1198
+ const targetService = unresolvedMatch[1];
1199
+ const type = unresolvedMatch[2];
1200
+ if (!serviceUrls.has(targetService)) {
1201
+ throw new ScriptError(
1202
+ `[${context}] Unresolved template variable: ${variable}. Service "${targetService}" is not defined in mesh.json.`
1203
+ );
1204
+ } else if (type === "public_url") {
1205
+ throw new ScriptError(
1206
+ `[${context}] Unresolved template variable: ${variable}. Service "${targetService}" does not expose a public port (missing 'port' or 'preservePort' in mesh.json).`
1207
+ );
1208
+ } else {
1209
+ throw new ScriptError(`[${context}] Unresolved template variable: ${variable}.`);
1210
+ }
1211
+ }
1212
+ return interpolated;
1213
+ }
1148
1214
  for (const [serviceName, serviceConfig] of Object.entries(mesh.services)) {
1149
1215
  const serviceDir = path13.resolve(meshDir, serviceConfig.path);
1150
1216
  const serviceBuildJson = getBuildJson(serviceDir);
@@ -1156,9 +1222,6 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1156
1222
  INFRA_MODE: "integrated",
1157
1223
  EXEC_MODE: mode
1158
1224
  };
1159
- if (serviceConfig.port) {
1160
- environment.PORT = String(serviceConfig.port);
1161
- }
1162
1225
  let overrideVolumes = [];
1163
1226
  let overrideHealthcheck = void 0;
1164
1227
  let overrideCommand = void 0;
@@ -1206,15 +1269,11 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1206
1269
  );
1207
1270
  }
1208
1271
  }
1272
+ environment.PORT = String(INTERNAL_PORT);
1209
1273
  if (serviceConfig.envOverrides) {
1210
- Object.assign(environment, serviceConfig.envOverrides);
1211
- }
1212
- const dockerfilePath = path13.resolve(serviceDir, dockerfile);
1213
- const envFilePort = extractPortFromEnvFile(path13.resolve(serviceDir, `.env.${mode}`)) || extractPortFromEnvFile(path13.resolve(serviceDir, ".env"));
1214
- const dockerfilePort = extractPortFromDockerfile(dockerfilePath);
1215
- const resolvedPort = serviceConfig.port || serviceConfig.healthcheck?.port || (environment.PORT ? parseInt(String(environment.PORT), 10) : void 0) || envFilePort || dockerfilePort || 3e3;
1216
- if (!environment.PORT) {
1217
- environment.PORT = String(resolvedPort);
1274
+ for (const [k, v] of Object.entries(serviceConfig.envOverrides)) {
1275
+ environment[k] = interpolateEnvTemplates(v, `Service: ${serviceName}`);
1276
+ }
1218
1277
  }
1219
1278
  const serviceDef = {
1220
1279
  build: {
@@ -1248,15 +1307,19 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1248
1307
  }
1249
1308
  serviceDef.volumes = Array.from(volSet);
1250
1309
  }
1251
- if (serviceConfig.port) {
1252
- serviceDef.ports = [`${serviceConfig.port}:${serviceConfig.port}`];
1310
+ const urls = serviceUrls.get(serviceName);
1311
+ if (urls.publicUrl) {
1312
+ const pubPort = urls.publicUrl.split(":").pop();
1313
+ serviceDef.ports = [`${pubPort}:${INTERNAL_PORT}`];
1314
+ } else {
1315
+ delete serviceDef.ports;
1253
1316
  }
1254
1317
  if (overrideCommand) {
1255
1318
  serviceDef.command = overrideCommand;
1256
1319
  }
1257
1320
  if (serviceConfig.healthcheck) {
1258
1321
  if (serviceConfig.healthcheck.type === "tcp") {
1259
- const hcPort = serviceConfig.healthcheck.port || resolvedPort;
1322
+ const hcPort = serviceConfig.healthcheck.port || INTERNAL_PORT;
1260
1323
  serviceDef.healthcheck = {
1261
1324
  test: [
1262
1325
  "CMD-SHELL",
@@ -1273,11 +1336,10 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1273
1336
  } else if (overrideHealthcheck) {
1274
1337
  serviceDef.healthcheck = overrideHealthcheck;
1275
1338
  } else {
1276
- const portValue = resolvedPort;
1277
1339
  serviceDef.healthcheck = {
1278
1340
  test: [
1279
1341
  "CMD-SHELL",
1280
- `nc -z 127.0.0.1 ${portValue} || node -e "require('net').connect(${portValue},'127.0.0.1').on('connect',()=>process.exit(0)).on('error',()=>process.exit(1))" || exit 1`
1342
+ `nc -z 127.0.0.1 ${INTERNAL_PORT} || node -e "require('net').connect(${INTERNAL_PORT},'127.0.0.1').on('connect',()=>process.exit(0)).on('error',()=>process.exit(1))" || exit 1`
1281
1343
  ],
1282
1344
  interval: "2s",
1283
1345
  timeout: "2s",
@@ -1459,6 +1521,27 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1459
1521
  condition: "service_healthy"
1460
1522
  };
1461
1523
  }
1524
+ if (mesh.tester.envOverrides) {
1525
+ if (Array.isArray(resolvedTester.environment)) {
1526
+ const envObj = {};
1527
+ for (const item of resolvedTester.environment) {
1528
+ if (typeof item === "string") {
1529
+ const eqIdx = item.indexOf("=");
1530
+ if (eqIdx !== -1) {
1531
+ envObj[item.slice(0, eqIdx)] = item.slice(eqIdx + 1);
1532
+ } else {
1533
+ envObj[item] = process.env[item] ?? null;
1534
+ }
1535
+ }
1536
+ }
1537
+ resolvedTester.environment = envObj;
1538
+ } else if (!resolvedTester.environment || typeof resolvedTester.environment !== "object") {
1539
+ resolvedTester.environment = {};
1540
+ }
1541
+ for (const [k, v] of Object.entries(mesh.tester.envOverrides)) {
1542
+ resolvedTester.environment[k] = interpolateEnvTemplates(v, `Tester: ${tKey}`);
1543
+ }
1544
+ }
1462
1545
  }
1463
1546
  registerService(tKey, resolvedTester, `tester service "${tKey}"`);
1464
1547
  }
@@ -1575,37 +1658,6 @@ function resolveServicePaths(service, baseDir, meshDir) {
1575
1658
  normalizeDependsOn(cloned);
1576
1659
  return cloned;
1577
1660
  }
1578
- function extractPortFromEnvFile(filePath) {
1579
- if (!existsSync(filePath)) return void 0;
1580
- try {
1581
- const content = readFileSync(filePath, "utf-8");
1582
- for (const line of content.split("\n")) {
1583
- const trimmed = line.trim();
1584
- if (trimmed.startsWith("#") || !trimmed.includes("=")) continue;
1585
- const [k, ...vParts] = trimmed.split("=");
1586
- if (k && k.trim() === "PORT") {
1587
- const rawVal = vParts.join("=").trim().replace(/^["']|["']$/g, "");
1588
- const val = parseInt(rawVal, 10);
1589
- if (!isNaN(val) && val > 0) return val;
1590
- }
1591
- }
1592
- } catch {
1593
- }
1594
- return void 0;
1595
- }
1596
- function extractPortFromDockerfile(dockerfilePath) {
1597
- if (!existsSync(dockerfilePath)) return void 0;
1598
- try {
1599
- const content = readFileSync(dockerfilePath, "utf-8");
1600
- const portMatch = content.match(/process\.env\.PORT\s*\|\|\s*(\d+)/) || content.match(/EXPOSE\s+(\d+)/);
1601
- if (portMatch && portMatch[1]) {
1602
- const val = parseInt(portMatch[1], 10);
1603
- if (!isNaN(val) && val > 0) return val;
1604
- }
1605
- } catch {
1606
- }
1607
- return void 0;
1608
- }
1609
1661
  var activeComposeSet = /* @__PURE__ */ new Set();
1610
1662
  var signalHandlersRegistered = false;
1611
1663
  var activeChildProcess = null;
@@ -2607,9 +2659,15 @@ var compileEnvironment = (environment, projectDir = process.cwd()) => {
2607
2659
  }
2608
2660
  }
2609
2661
  composeFlags.push("config");
2662
+ const envVars = { ...process.env };
2663
+ if (buildJson.port) {
2664
+ envVars.PUBLIC_PORT = String(buildJson.port);
2665
+ }
2666
+ envVars.PORT = "3000";
2610
2667
  const result = spawnSync("docker", composeFlags, {
2611
2668
  cwd: absProjectDir,
2612
- encoding: "utf-8"
2669
+ encoding: "utf-8",
2670
+ env: envVars
2613
2671
  });
2614
2672
  if (result.error) {
2615
2673
  throw new ScriptError(
@@ -2637,6 +2695,66 @@ ${explanation}`);
2637
2695
  }
2638
2696
  if (environment === "e2e") {
2639
2697
  makeE2eDependOnApp(yml);
2698
+ if (buildJson.tester?.envOverrides && yml.services && yml.services.tester) {
2699
+ const INTERNAL_PORT = 3e3;
2700
+ const urlRegistry = /* @__PURE__ */ new Map();
2701
+ const appPublicPort = buildJson.port ? Number(buildJson.port) : null;
2702
+ urlRegistry.set("app", {
2703
+ networkUrl: `http://app:${INTERNAL_PORT}`,
2704
+ publicUrl: appPublicPort ? `http://localhost:${appPublicPort}` : null
2705
+ });
2706
+ if (buildJson.name && buildJson.name !== "app") {
2707
+ urlRegistry.set(buildJson.name, urlRegistry.get("app"));
2708
+ }
2709
+ for (const dep of dependencies) {
2710
+ urlRegistry.set(dep.name, {
2711
+ networkUrl: `http://${dep.name}:${INTERNAL_PORT}`,
2712
+ publicUrl: null
2713
+ });
2714
+ }
2715
+ const interpolateEnv = (val) => {
2716
+ let output = val;
2717
+ for (const [sName, urls] of urlRegistry.entries()) {
2718
+ const escaped = sName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2719
+ output = output.replace(
2720
+ new RegExp(`\\$\\{${escaped}\\.network_url\\}`, "g"),
2721
+ urls.networkUrl
2722
+ );
2723
+ if (urls.publicUrl) {
2724
+ output = output.replace(
2725
+ new RegExp(`\\$\\{${escaped}\\.public_url\\}`, "g"),
2726
+ urls.publicUrl
2727
+ );
2728
+ }
2729
+ }
2730
+ const unresolvedMatch = output.match(/\$\{([^}]+?)\.(network_url|public_url)\}/);
2731
+ if (unresolvedMatch) {
2732
+ throw new ScriptError(
2733
+ `[Local E2E Tester] Unresolved template variable: ${unresolvedMatch[0]}. Check build.json dependencies.`
2734
+ );
2735
+ }
2736
+ return output;
2737
+ };
2738
+ if (Array.isArray(yml.services.tester.environment)) {
2739
+ const envObj = {};
2740
+ for (const item of yml.services.tester.environment) {
2741
+ if (typeof item === "string") {
2742
+ const eqIdx = item.indexOf("=");
2743
+ if (eqIdx !== -1) {
2744
+ envObj[item.slice(0, eqIdx)] = item.slice(eqIdx + 1);
2745
+ } else {
2746
+ envObj[item] = process.env[item] ?? null;
2747
+ }
2748
+ }
2749
+ }
2750
+ yml.services.tester.environment = envObj;
2751
+ } else if (!yml.services.tester.environment || typeof yml.services.tester.environment !== "object") {
2752
+ yml.services.tester.environment = {};
2753
+ }
2754
+ for (const [k, v] of Object.entries(buildJson.tester.envOverrides)) {
2755
+ yml.services.tester.environment[k] = interpolateEnv(v);
2756
+ }
2757
+ }
2640
2758
  }
2641
2759
  attachName(yml, projectName);
2642
2760
  const finalYamlConfig = stringify(yml);
@@ -3379,7 +3497,15 @@ function createCli() {
3379
3497
  }
3380
3498
  async function runCli(argv = process.argv) {
3381
3499
  const cli = createCli();
3500
+ const isHelpOrVersion = argv.some(
3501
+ (arg) => arg === "-h" || arg === "--help" || arg === "-V" || arg === "--version"
3502
+ );
3503
+ if (!isHelpOrVersion && argv.length > 2) {
3504
+ console.log(pc20.dim(`builder v${version}`));
3505
+ }
3382
3506
  if (argv.length <= 2) {
3507
+ console.log(pc20.dim(`builder v${version}
3508
+ `));
3383
3509
  cli.outputHelp();
3384
3510
  return;
3385
3511
  }
@@ -3423,7 +3549,41 @@ ${formatted}
3423
3549
  process.exit(exitCode);
3424
3550
  }
3425
3551
  }
3552
+ var buildPolicyConfigSchema = z.object({
3553
+ dockerfile: z.string().optional().describe("Relative path to policy directory for Dockerfile checks"),
3554
+ compose: z.string().optional().describe("Relative path to policy directory for Compose contract checks")
3555
+ }).strict().describe("Policy configurations for infra/contract verification");
3556
+ var buildDependencySchema = z.object({
3557
+ path: z.string({ required_error: 'Dependency "path" is required' }).describe("Relative path to mock Docker Compose file"),
3558
+ service: z.string().optional().describe("Target service name in the mock compose file")
3559
+ }).strict().describe("Service dependency specification");
3560
+ var buildOverrideSchema = z.object({
3561
+ path: z.string({ required_error: 'Override "path" is required' }).describe("Relative path to the environment stage override file")
3562
+ }).strict().describe("Environment override specification");
3563
+ var buildTesterSchema = z.object({
3564
+ envOverrides: z.record(z.string(), z.string()).optional().describe(
3565
+ "Environment variable overrides for the individual E2E tester with URL templating"
3566
+ )
3567
+ }).strict().describe("E2E tester configuration");
3568
+ var buildJsonSchema = z.object({
3569
+ $schema: z.string().optional().describe("JSON Schema URL or relative path"),
3570
+ name: z.string().optional().describe("Service project name"),
3571
+ dockerfile: z.string().optional().describe("Relative path to the Dockerfile (defaults to 'Dockerfile')"),
3572
+ port: z.union([z.number(), z.string()]).optional().describe(
3573
+ "Host port exposed by this service when listening publicly"
3574
+ ),
3575
+ policy: buildPolicyConfigSchema.optional(),
3576
+ policies: buildPolicyConfigSchema.optional(),
3577
+ composeFiles: z.array(z.string()).optional().describe("Additional compose files to merge"),
3578
+ dependencies: z.record(z.string(), buildDependencySchema).optional().describe("Service dependencies and mocks"),
3579
+ overrides: z.record(
3580
+ z.enum(["dev", "prod", "test", "e2e"]),
3581
+ buildOverrideSchema
3582
+ ).optional().describe("Stage overrides (dev, prod, test, e2e)"),
3583
+ services: z.record(z.string(), z.any()).optional().describe("Additional Docker Compose service overrides"),
3584
+ tester: buildTesterSchema.optional()
3585
+ }).strict().describe("Solid Stack service build and deployment configuration");
3426
3586
 
3427
- export { NotFoundError, SERVICE_STAGES, ScriptError, compileEnvironment, compileMeshEnvironment, createCli, runCli, runCompose, teardownCompose, version };
3587
+ export { NotFoundError, SERVICE_STAGES, ScriptError, buildJsonSchema, compileEnvironment, compileMeshEnvironment, createCli, meshSchema, runCli, runCompose, teardownCompose, version };
3428
3588
  //# sourceMappingURL=index.js.map
3429
3589
  //# sourceMappingURL=index.js.map