@oxog/dockcraft 1.3.1 → 1.3.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/dist/index.js CHANGED
@@ -584,12 +584,13 @@ function mergePreset(config, presetName) {
584
584
  );
585
585
  return merged;
586
586
  }
587
- async function resolveServicePorts(portConfig, containerPort) {
587
+ async function resolveServicePorts(portConfig, containerPort, claimed = /* @__PURE__ */ new Set()) {
588
588
  if (portConfig === "auto") {
589
- return {
590
- port: containerPort,
591
- hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
592
- };
589
+ let hostPort = await resolvePort({ preferred: containerPort, strategy: "auto" });
590
+ while (claimed.has(hostPort)) {
591
+ hostPort = await resolvePort({ preferred: hostPort + 1, strategy: "auto" });
592
+ }
593
+ return { port: containerPort, hostPort };
593
594
  }
594
595
  if (typeof portConfig === "number") {
595
596
  return { port: containerPort, hostPort: portConfig };
@@ -597,26 +598,36 @@ async function resolveServicePorts(portConfig, containerPort) {
597
598
  if (portConfig && typeof portConfig === "object") {
598
599
  const startPort = portConfig.start ?? containerPort;
599
600
  const endPort = portConfig.end ?? startPort + 100;
600
- return {
601
- port: containerPort,
602
- hostPort: await resolvePort({
603
- preferred: startPort,
604
- strategy: "range",
605
- range: [startPort, endPort]
606
- })
607
- };
601
+ let hostPort = await resolvePort({
602
+ preferred: startPort,
603
+ strategy: "range",
604
+ range: [startPort, endPort]
605
+ });
606
+ while (hostPort !== void 0 && claimed.has(hostPort)) {
607
+ hostPort = hostPort < endPort ? await findAvailablePortInRange(hostPort + 1, endPort) : void 0;
608
+ }
609
+ if (hostPort === void 0) throw new NoPortAvailableError([startPort, endPort]);
610
+ return { port: containerPort, hostPort };
608
611
  }
609
612
  return { port: containerPort };
610
613
  }
611
- async function resolveServiceConfig(name, config, projectName, definitions) {
614
+ async function resolveServiceConfig(name, config, projectName, definitions, claimedPorts) {
612
615
  if ("image" in config && config.image) {
613
- return resolveCustomService(name, config);
616
+ return resolveCustomService(
617
+ name,
618
+ config,
619
+ claimedPorts
620
+ );
614
621
  }
615
622
  const definition = definitions?.get(name) ?? getService(name);
616
623
  if (!definition) {
617
624
  throw new ServiceNotFoundError(name);
618
625
  }
619
- const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
626
+ const { port, hostPort } = await resolveServicePorts(
627
+ config.port,
628
+ definition.defaultPort,
629
+ claimedPorts
630
+ );
620
631
  const version = config.version ?? definition.defaultVersion;
621
632
  const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
622
633
  const env = { ...definition.env, ...config.env };
@@ -666,14 +677,14 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
666
677
  }
667
678
  return resolvedConfig;
668
679
  }
669
- async function resolveCustomService(name, config) {
680
+ async function resolveCustomService(name, config, claimedPorts) {
670
681
  let containerPort = 80;
671
682
  if (typeof config.port === "number") {
672
683
  containerPort = config.port;
673
684
  } else if (config.port && typeof config.port === "object" && config.port.start) {
674
685
  containerPort = config.port.start;
675
686
  }
676
- const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
687
+ const { port, hostPort } = await resolveServicePorts(config.port, containerPort, claimedPorts);
677
688
  return {
678
689
  name,
679
690
  image: config.image,
@@ -706,14 +717,20 @@ async function resolveConfig(config) {
706
717
  const app = normalizeAppConfig({ ...appConfig, port: appPort });
707
718
  const definitions = pluginServiceDefinitions(finalConfig.plugins);
708
719
  const services2 = {};
720
+ const claimedPorts = /* @__PURE__ */ new Set([app.port]);
709
721
  for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
710
722
  if (serviceConfig) {
711
- services2[name] = await resolveServiceConfig(
723
+ const service = await resolveServiceConfig(
712
724
  name,
713
725
  serviceConfig,
714
726
  finalConfig.name,
715
- definitions
727
+ definitions,
728
+ claimedPorts
716
729
  );
730
+ services2[name] = service;
731
+ for (const port of [service.hostPort ?? service.port, ...service.ports ?? []]) {
732
+ claimedPorts.add(port);
733
+ }
717
734
  }
718
735
  }
719
736
  return {