@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.cjs CHANGED
@@ -630,12 +630,13 @@ function mergePreset(config, presetName) {
630
630
  );
631
631
  return merged;
632
632
  }
633
- async function resolveServicePorts(portConfig, containerPort) {
633
+ async function resolveServicePorts(portConfig, containerPort, claimed = /* @__PURE__ */ new Set()) {
634
634
  if (portConfig === "auto") {
635
- return {
636
- port: containerPort,
637
- hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
638
- };
635
+ let hostPort = await resolvePort({ preferred: containerPort, strategy: "auto" });
636
+ while (claimed.has(hostPort)) {
637
+ hostPort = await resolvePort({ preferred: hostPort + 1, strategy: "auto" });
638
+ }
639
+ return { port: containerPort, hostPort };
639
640
  }
640
641
  if (typeof portConfig === "number") {
641
642
  return { port: containerPort, hostPort: portConfig };
@@ -643,26 +644,36 @@ async function resolveServicePorts(portConfig, containerPort) {
643
644
  if (portConfig && typeof portConfig === "object") {
644
645
  const startPort = portConfig.start ?? containerPort;
645
646
  const endPort = portConfig.end ?? startPort + 100;
646
- return {
647
- port: containerPort,
648
- hostPort: await resolvePort({
649
- preferred: startPort,
650
- strategy: "range",
651
- range: [startPort, endPort]
652
- })
653
- };
647
+ let hostPort = await resolvePort({
648
+ preferred: startPort,
649
+ strategy: "range",
650
+ range: [startPort, endPort]
651
+ });
652
+ while (hostPort !== void 0 && claimed.has(hostPort)) {
653
+ hostPort = hostPort < endPort ? await findAvailablePortInRange(hostPort + 1, endPort) : void 0;
654
+ }
655
+ if (hostPort === void 0) throw new NoPortAvailableError([startPort, endPort]);
656
+ return { port: containerPort, hostPort };
654
657
  }
655
658
  return { port: containerPort };
656
659
  }
657
- async function resolveServiceConfig(name, config, projectName, definitions) {
660
+ async function resolveServiceConfig(name, config, projectName, definitions, claimedPorts) {
658
661
  if ("image" in config && config.image) {
659
- return resolveCustomService(name, config);
662
+ return resolveCustomService(
663
+ name,
664
+ config,
665
+ claimedPorts
666
+ );
660
667
  }
661
668
  const definition = definitions?.get(name) ?? getService(name);
662
669
  if (!definition) {
663
670
  throw new ServiceNotFoundError(name);
664
671
  }
665
- const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
672
+ const { port, hostPort } = await resolveServicePorts(
673
+ config.port,
674
+ definition.defaultPort,
675
+ claimedPorts
676
+ );
666
677
  const version = config.version ?? definition.defaultVersion;
667
678
  const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
668
679
  const env = { ...definition.env, ...config.env };
@@ -712,14 +723,14 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
712
723
  }
713
724
  return resolvedConfig;
714
725
  }
715
- async function resolveCustomService(name, config) {
726
+ async function resolveCustomService(name, config, claimedPorts) {
716
727
  let containerPort = 80;
717
728
  if (typeof config.port === "number") {
718
729
  containerPort = config.port;
719
730
  } else if (config.port && typeof config.port === "object" && config.port.start) {
720
731
  containerPort = config.port.start;
721
732
  }
722
- const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
733
+ const { port, hostPort } = await resolveServicePorts(config.port, containerPort, claimedPorts);
723
734
  return {
724
735
  name,
725
736
  image: config.image,
@@ -752,14 +763,20 @@ async function resolveConfig(config) {
752
763
  const app = normalizeAppConfig({ ...appConfig, port: appPort });
753
764
  const definitions = pluginServiceDefinitions(finalConfig.plugins);
754
765
  const services2 = {};
766
+ const claimedPorts = /* @__PURE__ */ new Set([app.port]);
755
767
  for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
756
768
  if (serviceConfig) {
757
- services2[name] = await resolveServiceConfig(
769
+ const service = await resolveServiceConfig(
758
770
  name,
759
771
  serviceConfig,
760
772
  finalConfig.name,
761
- definitions
773
+ definitions,
774
+ claimedPorts
762
775
  );
776
+ services2[name] = service;
777
+ for (const port of [service.hostPort ?? service.port, ...service.ports ?? []]) {
778
+ claimedPorts.add(port);
779
+ }
763
780
  }
764
781
  }
765
782
  return {