@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/bin.js CHANGED
@@ -474,17 +474,21 @@ var meshHealthcheckSchema = z.union([
474
474
  }).passthrough()
475
475
  ]);
476
476
  var meshServiceSchema = z.object({
477
- path: z.string({ required_error: 'Service "path" is required' }),
478
- port: z.union([z.number(), z.string()]).optional(),
479
- provideDependency: z.record(z.string(), z.string()).optional(),
480
- replaceMocks: z.record(z.string(), z.string()).optional(),
481
- envOverrides: z.record(z.string(), z.string()).optional(),
477
+ path: z.string({ required_error: 'Service "path" is required' }).describe("Relative path to service directory containing build.json"),
478
+ port: z.union([z.number(), z.string()]).optional().describe("Public host port mapped to internal port 3000"),
479
+ preservePort: z.boolean().optional().describe("If true, use the port declared in build.json"),
480
+ provideDependency: z.record(z.string(), z.string()).optional().describe("Map consumer dependency key to fellow mesh service provider"),
481
+ replaceMocks: z.record(z.string(), z.string()).optional().describe("Alias for provideDependency"),
482
+ envOverrides: z.record(z.string(), z.string()).optional().describe("Environment variable overrides with URL templating"),
482
483
  healthcheck: meshHealthcheckSchema.optional()
483
484
  }).strict();
484
485
  var meshTesterSchema = z.object({
485
- path: z.string({ required_error: 'Tester "path" is required' }),
486
- compose: z.string().optional(),
487
- dockerfile: z.string().optional(),
486
+ path: z.string({ required_error: 'Tester "path" is required' }).describe("Relative path to E2E tester directory"),
487
+ compose: z.string().optional().describe("Tester compose file (defaults to 'docker-compose.yml')"),
488
+ dockerfile: z.string().optional().describe("Tester Dockerfile (defaults to 'Dockerfile')"),
489
+ envOverrides: z.record(z.string(), z.string()).optional().describe(
490
+ "Environment variable overrides for global tester with URL templating"
491
+ ),
488
492
  policy: z.object({
489
493
  dockerfile: z.string().optional(),
490
494
  compose: z.string().optional()
@@ -492,6 +496,7 @@ var meshTesterSchema = z.object({
492
496
  }).strict();
493
497
  var meshServiceNameRegex = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
494
498
  var meshSchema = z.object({
499
+ $schema: z.string().optional(),
495
500
  name: z.string().optional(),
496
501
  services: z.record(
497
502
  z.string().regex(meshServiceNameRegex, {
@@ -1137,6 +1142,67 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1137
1142
  providerAliases.set(sName, /* @__PURE__ */ new Set());
1138
1143
  }
1139
1144
  const globalDepAliasToProvider = /* @__PURE__ */ new Map();
1145
+ const INTERNAL_PORT = 3e3;
1146
+ const serviceUrls = /* @__PURE__ */ new Map();
1147
+ for (const [serviceName, serviceConfig] of Object.entries(mesh.services)) {
1148
+ const serviceDir = path13.resolve(meshDir, serviceConfig.path);
1149
+ const serviceBuildJson = getBuildJson(serviceDir);
1150
+ let publicPort = null;
1151
+ if (serviceConfig.port !== void 0 && serviceConfig.port !== null && serviceConfig.port !== "") {
1152
+ const p = Number(serviceConfig.port);
1153
+ if (!isNaN(p) && p > 0) {
1154
+ publicPort = p;
1155
+ }
1156
+ } else if (serviceConfig.preservePort && serviceBuildJson.port) {
1157
+ const p = Number(serviceBuildJson.port);
1158
+ if (!isNaN(p) && p > 0) {
1159
+ publicPort = p;
1160
+ }
1161
+ }
1162
+ serviceUrls.set(serviceName, {
1163
+ networkUrl: `http://${serviceName}:${INTERNAL_PORT}`,
1164
+ publicUrl: publicPort ? `http://localhost:${publicPort}` : null
1165
+ });
1166
+ }
1167
+ function escapeRegExp(string) {
1168
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1169
+ }
1170
+ function interpolateEnvTemplates(value, context) {
1171
+ if (typeof value !== "string") {
1172
+ return value;
1173
+ }
1174
+ let interpolated = value;
1175
+ for (const [sName, urls] of serviceUrls.entries()) {
1176
+ interpolated = interpolated.replace(
1177
+ new RegExp(`\\$\\{${escapeRegExp(sName)}\\.network_url\\}`, "g"),
1178
+ urls.networkUrl
1179
+ );
1180
+ if (urls.publicUrl) {
1181
+ interpolated = interpolated.replace(
1182
+ new RegExp(`\\$\\{${escapeRegExp(sName)}\\.public_url\\}`, "g"),
1183
+ urls.publicUrl
1184
+ );
1185
+ }
1186
+ }
1187
+ const unresolvedMatch = interpolated.match(/\$\{([^}]+?)\.(network_url|public_url)\}/);
1188
+ if (unresolvedMatch) {
1189
+ const variable = unresolvedMatch[0];
1190
+ const targetService = unresolvedMatch[1];
1191
+ const type = unresolvedMatch[2];
1192
+ if (!serviceUrls.has(targetService)) {
1193
+ throw new ScriptError(
1194
+ `[${context}] Unresolved template variable: ${variable}. Service "${targetService}" is not defined in mesh.json.`
1195
+ );
1196
+ } else if (type === "public_url") {
1197
+ throw new ScriptError(
1198
+ `[${context}] Unresolved template variable: ${variable}. Service "${targetService}" does not expose a public port (missing 'port' or 'preservePort' in mesh.json).`
1199
+ );
1200
+ } else {
1201
+ throw new ScriptError(`[${context}] Unresolved template variable: ${variable}.`);
1202
+ }
1203
+ }
1204
+ return interpolated;
1205
+ }
1140
1206
  for (const [serviceName, serviceConfig] of Object.entries(mesh.services)) {
1141
1207
  const serviceDir = path13.resolve(meshDir, serviceConfig.path);
1142
1208
  const serviceBuildJson = getBuildJson(serviceDir);
@@ -1148,9 +1214,6 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1148
1214
  INFRA_MODE: "integrated",
1149
1215
  EXEC_MODE: mode
1150
1216
  };
1151
- if (serviceConfig.port) {
1152
- environment.PORT = String(serviceConfig.port);
1153
- }
1154
1217
  let overrideVolumes = [];
1155
1218
  let overrideHealthcheck = void 0;
1156
1219
  let overrideCommand = void 0;
@@ -1198,15 +1261,11 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1198
1261
  );
1199
1262
  }
1200
1263
  }
1264
+ environment.PORT = String(INTERNAL_PORT);
1201
1265
  if (serviceConfig.envOverrides) {
1202
- Object.assign(environment, serviceConfig.envOverrides);
1203
- }
1204
- const dockerfilePath = path13.resolve(serviceDir, dockerfile);
1205
- const envFilePort = extractPortFromEnvFile(path13.resolve(serviceDir, `.env.${mode}`)) || extractPortFromEnvFile(path13.resolve(serviceDir, ".env"));
1206
- const dockerfilePort = extractPortFromDockerfile(dockerfilePath);
1207
- const resolvedPort = serviceConfig.port || serviceConfig.healthcheck?.port || (environment.PORT ? parseInt(String(environment.PORT), 10) : void 0) || envFilePort || dockerfilePort || 3e3;
1208
- if (!environment.PORT) {
1209
- environment.PORT = String(resolvedPort);
1266
+ for (const [k, v] of Object.entries(serviceConfig.envOverrides)) {
1267
+ environment[k] = interpolateEnvTemplates(v, `Service: ${serviceName}`);
1268
+ }
1210
1269
  }
1211
1270
  const serviceDef = {
1212
1271
  build: {
@@ -1240,15 +1299,19 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1240
1299
  }
1241
1300
  serviceDef.volumes = Array.from(volSet);
1242
1301
  }
1243
- if (serviceConfig.port) {
1244
- serviceDef.ports = [`${serviceConfig.port}:${serviceConfig.port}`];
1302
+ const urls = serviceUrls.get(serviceName);
1303
+ if (urls.publicUrl) {
1304
+ const pubPort = urls.publicUrl.split(":").pop();
1305
+ serviceDef.ports = [`${pubPort}:${INTERNAL_PORT}`];
1306
+ } else {
1307
+ delete serviceDef.ports;
1245
1308
  }
1246
1309
  if (overrideCommand) {
1247
1310
  serviceDef.command = overrideCommand;
1248
1311
  }
1249
1312
  if (serviceConfig.healthcheck) {
1250
1313
  if (serviceConfig.healthcheck.type === "tcp") {
1251
- const hcPort = serviceConfig.healthcheck.port || resolvedPort;
1314
+ const hcPort = serviceConfig.healthcheck.port || INTERNAL_PORT;
1252
1315
  serviceDef.healthcheck = {
1253
1316
  test: [
1254
1317
  "CMD-SHELL",
@@ -1265,11 +1328,10 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1265
1328
  } else if (overrideHealthcheck) {
1266
1329
  serviceDef.healthcheck = overrideHealthcheck;
1267
1330
  } else {
1268
- const portValue = resolvedPort;
1269
1331
  serviceDef.healthcheck = {
1270
1332
  test: [
1271
1333
  "CMD-SHELL",
1272
- `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`
1334
+ `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`
1273
1335
  ],
1274
1336
  interval: "2s",
1275
1337
  timeout: "2s",
@@ -1451,6 +1513,27 @@ function compileMeshEnvironment(mode, rawMeshDir = process.cwd(), options = {})
1451
1513
  condition: "service_healthy"
1452
1514
  };
1453
1515
  }
1516
+ if (mesh.tester.envOverrides) {
1517
+ if (Array.isArray(resolvedTester.environment)) {
1518
+ const envObj = {};
1519
+ for (const item of resolvedTester.environment) {
1520
+ if (typeof item === "string") {
1521
+ const eqIdx = item.indexOf("=");
1522
+ if (eqIdx !== -1) {
1523
+ envObj[item.slice(0, eqIdx)] = item.slice(eqIdx + 1);
1524
+ } else {
1525
+ envObj[item] = process.env[item] ?? null;
1526
+ }
1527
+ }
1528
+ }
1529
+ resolvedTester.environment = envObj;
1530
+ } else if (!resolvedTester.environment || typeof resolvedTester.environment !== "object") {
1531
+ resolvedTester.environment = {};
1532
+ }
1533
+ for (const [k, v] of Object.entries(mesh.tester.envOverrides)) {
1534
+ resolvedTester.environment[k] = interpolateEnvTemplates(v, `Tester: ${tKey}`);
1535
+ }
1536
+ }
1454
1537
  }
1455
1538
  registerService(tKey, resolvedTester, `tester service "${tKey}"`);
1456
1539
  }
@@ -1567,37 +1650,6 @@ function resolveServicePaths(service, baseDir, meshDir) {
1567
1650
  normalizeDependsOn(cloned);
1568
1651
  return cloned;
1569
1652
  }
1570
- function extractPortFromEnvFile(filePath) {
1571
- if (!existsSync(filePath)) return void 0;
1572
- try {
1573
- const content = readFileSync(filePath, "utf-8");
1574
- for (const line of content.split("\n")) {
1575
- const trimmed = line.trim();
1576
- if (trimmed.startsWith("#") || !trimmed.includes("=")) continue;
1577
- const [k, ...vParts] = trimmed.split("=");
1578
- if (k && k.trim() === "PORT") {
1579
- const rawVal = vParts.join("=").trim().replace(/^["']|["']$/g, "");
1580
- const val = parseInt(rawVal, 10);
1581
- if (!isNaN(val) && val > 0) return val;
1582
- }
1583
- }
1584
- } catch {
1585
- }
1586
- return void 0;
1587
- }
1588
- function extractPortFromDockerfile(dockerfilePath) {
1589
- if (!existsSync(dockerfilePath)) return void 0;
1590
- try {
1591
- const content = readFileSync(dockerfilePath, "utf-8");
1592
- const portMatch = content.match(/process\.env\.PORT\s*\|\|\s*(\d+)/) || content.match(/EXPOSE\s+(\d+)/);
1593
- if (portMatch && portMatch[1]) {
1594
- const val = parseInt(portMatch[1], 10);
1595
- if (!isNaN(val) && val > 0) return val;
1596
- }
1597
- } catch {
1598
- }
1599
- return void 0;
1600
- }
1601
1653
  var activeComposeSet = /* @__PURE__ */ new Set();
1602
1654
  var signalHandlersRegistered = false;
1603
1655
  var activeChildProcess = null;
@@ -2599,9 +2651,15 @@ var compileEnvironment = (environment, projectDir = process.cwd()) => {
2599
2651
  }
2600
2652
  }
2601
2653
  composeFlags.push("config");
2654
+ const envVars = { ...process.env };
2655
+ if (buildJson.port) {
2656
+ envVars.PUBLIC_PORT = String(buildJson.port);
2657
+ }
2658
+ envVars.PORT = "3000";
2602
2659
  const result = spawnSync("docker", composeFlags, {
2603
2660
  cwd: absProjectDir,
2604
- encoding: "utf-8"
2661
+ encoding: "utf-8",
2662
+ env: envVars
2605
2663
  });
2606
2664
  if (result.error) {
2607
2665
  throw new ScriptError(
@@ -2629,6 +2687,66 @@ ${explanation}`);
2629
2687
  }
2630
2688
  if (environment === "e2e") {
2631
2689
  makeE2eDependOnApp(yml);
2690
+ if (buildJson.tester?.envOverrides && yml.services && yml.services.tester) {
2691
+ const INTERNAL_PORT = 3e3;
2692
+ const urlRegistry = /* @__PURE__ */ new Map();
2693
+ const appPublicPort = buildJson.port ? Number(buildJson.port) : null;
2694
+ urlRegistry.set("app", {
2695
+ networkUrl: `http://app:${INTERNAL_PORT}`,
2696
+ publicUrl: appPublicPort ? `http://localhost:${appPublicPort}` : null
2697
+ });
2698
+ if (buildJson.name && buildJson.name !== "app") {
2699
+ urlRegistry.set(buildJson.name, urlRegistry.get("app"));
2700
+ }
2701
+ for (const dep of dependencies) {
2702
+ urlRegistry.set(dep.name, {
2703
+ networkUrl: `http://${dep.name}:${INTERNAL_PORT}`,
2704
+ publicUrl: null
2705
+ });
2706
+ }
2707
+ const interpolateEnv = (val) => {
2708
+ let output = val;
2709
+ for (const [sName, urls] of urlRegistry.entries()) {
2710
+ const escaped = sName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2711
+ output = output.replace(
2712
+ new RegExp(`\\$\\{${escaped}\\.network_url\\}`, "g"),
2713
+ urls.networkUrl
2714
+ );
2715
+ if (urls.publicUrl) {
2716
+ output = output.replace(
2717
+ new RegExp(`\\$\\{${escaped}\\.public_url\\}`, "g"),
2718
+ urls.publicUrl
2719
+ );
2720
+ }
2721
+ }
2722
+ const unresolvedMatch = output.match(/\$\{([^}]+?)\.(network_url|public_url)\}/);
2723
+ if (unresolvedMatch) {
2724
+ throw new ScriptError(
2725
+ `[Local E2E Tester] Unresolved template variable: ${unresolvedMatch[0]}. Check build.json dependencies.`
2726
+ );
2727
+ }
2728
+ return output;
2729
+ };
2730
+ if (Array.isArray(yml.services.tester.environment)) {
2731
+ const envObj = {};
2732
+ for (const item of yml.services.tester.environment) {
2733
+ if (typeof item === "string") {
2734
+ const eqIdx = item.indexOf("=");
2735
+ if (eqIdx !== -1) {
2736
+ envObj[item.slice(0, eqIdx)] = item.slice(eqIdx + 1);
2737
+ } else {
2738
+ envObj[item] = process.env[item] ?? null;
2739
+ }
2740
+ }
2741
+ }
2742
+ yml.services.tester.environment = envObj;
2743
+ } else if (!yml.services.tester.environment || typeof yml.services.tester.environment !== "object") {
2744
+ yml.services.tester.environment = {};
2745
+ }
2746
+ for (const [k, v] of Object.entries(buildJson.tester.envOverrides)) {
2747
+ yml.services.tester.environment[k] = interpolateEnv(v);
2748
+ }
2749
+ }
2632
2750
  }
2633
2751
  attachName(yml, projectName);
2634
2752
  const finalYamlConfig = stringify(yml);
@@ -3380,7 +3498,15 @@ function createCli() {
3380
3498
  }
3381
3499
  async function runCli(argv = process.argv) {
3382
3500
  const cli = createCli();
3501
+ const isHelpOrVersion = argv.some(
3502
+ (arg) => arg === "-h" || arg === "--help" || arg === "-V" || arg === "--version"
3503
+ );
3504
+ if (!isHelpOrVersion && argv.length > 2) {
3505
+ console.log(pc20.dim(`builder v${version}`));
3506
+ }
3383
3507
  if (argv.length <= 2) {
3508
+ console.log(pc20.dim(`builder v${version}
3509
+ `));
3384
3510
  cli.outputHelp();
3385
3511
  return;
3386
3512
  }