@oxog/dockcraft 1.3.0 → 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
@@ -85,7 +85,10 @@ var NoPortAvailableError = class extends DockcraftError {
85
85
  };
86
86
  var FileWriteError = class extends DockcraftError {
87
87
  constructor(path, code = "FILE_WRITE_ERROR", cause) {
88
- super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
88
+ super(
89
+ `Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`,
90
+ code
91
+ );
89
92
  this.name = "FileWriteError";
90
93
  if (cause !== void 0) {
91
94
  this.cause = cause;
@@ -538,11 +541,7 @@ async function findAvailablePortInRange(start, end) {
538
541
  return void 0;
539
542
  }
540
543
  async function resolvePort(options = {}) {
541
- const {
542
- preferred = 3e3,
543
- strategy = "auto",
544
- range
545
- } = options;
544
+ const { preferred = 3e3, strategy = "auto", range } = options;
546
545
  switch (strategy) {
547
546
  case "fixed":
548
547
  if (await isPortAvailable(preferred)) {
@@ -631,12 +630,13 @@ function mergePreset(config, presetName) {
631
630
  );
632
631
  return merged;
633
632
  }
634
- async function resolveServicePorts(portConfig, containerPort) {
633
+ async function resolveServicePorts(portConfig, containerPort, claimed = /* @__PURE__ */ new Set()) {
635
634
  if (portConfig === "auto") {
636
- return {
637
- port: containerPort,
638
- hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
639
- };
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 };
640
640
  }
641
641
  if (typeof portConfig === "number") {
642
642
  return { port: containerPort, hostPort: portConfig };
@@ -644,22 +644,36 @@ async function resolveServicePorts(portConfig, containerPort) {
644
644
  if (portConfig && typeof portConfig === "object") {
645
645
  const startPort = portConfig.start ?? containerPort;
646
646
  const endPort = portConfig.end ?? startPort + 100;
647
- return {
648
- port: containerPort,
649
- hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
650
- };
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 };
651
657
  }
652
658
  return { port: containerPort };
653
659
  }
654
- async function resolveServiceConfig(name, config, projectName, definitions) {
660
+ async function resolveServiceConfig(name, config, projectName, definitions, claimedPorts) {
655
661
  if ("image" in config && config.image) {
656
- return resolveCustomService(name, config);
662
+ return resolveCustomService(
663
+ name,
664
+ config,
665
+ claimedPorts
666
+ );
657
667
  }
658
668
  const definition = definitions?.get(name) ?? getService(name);
659
669
  if (!definition) {
660
670
  throw new ServiceNotFoundError(name);
661
671
  }
662
- const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
672
+ const { port, hostPort } = await resolveServicePorts(
673
+ config.port,
674
+ definition.defaultPort,
675
+ claimedPorts
676
+ );
663
677
  const version = config.version ?? definition.defaultVersion;
664
678
  const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
665
679
  const env = { ...definition.env, ...config.env };
@@ -682,7 +696,8 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
682
696
  if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
683
697
  if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
684
698
  if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
685
- if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
699
+ if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE)
700
+ env.MONGO_INITDB_DATABASE = projectName;
686
701
  } else if (name === "redis") {
687
702
  const redisConfig = config;
688
703
  if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
@@ -708,14 +723,14 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
708
723
  }
709
724
  return resolvedConfig;
710
725
  }
711
- async function resolveCustomService(name, config) {
726
+ async function resolveCustomService(name, config, claimedPorts) {
712
727
  let containerPort = 80;
713
728
  if (typeof config.port === "number") {
714
729
  containerPort = config.port;
715
730
  } else if (config.port && typeof config.port === "object" && config.port.start) {
716
731
  containerPort = config.port.start;
717
732
  }
718
- const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
733
+ const { port, hostPort } = await resolveServicePorts(config.port, containerPort, claimedPorts);
719
734
  return {
720
735
  name,
721
736
  image: config.image,
@@ -748,14 +763,20 @@ async function resolveConfig(config) {
748
763
  const app = normalizeAppConfig({ ...appConfig, port: appPort });
749
764
  const definitions = pluginServiceDefinitions(finalConfig.plugins);
750
765
  const services2 = {};
766
+ const claimedPorts = /* @__PURE__ */ new Set([app.port]);
751
767
  for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
752
768
  if (serviceConfig) {
753
- services2[name] = await resolveServiceConfig(
769
+ const service = await resolveServiceConfig(
754
770
  name,
755
771
  serviceConfig,
756
772
  finalConfig.name,
757
- definitions
773
+ definitions,
774
+ claimedPorts
758
775
  );
776
+ services2[name] = service;
777
+ for (const port of [service.hostPort ?? service.port, ...service.ports ?? []]) {
778
+ claimedPorts.add(port);
779
+ }
759
780
  }
760
781
  }
761
782
  return {
@@ -823,17 +844,21 @@ function namedVolume(spec) {
823
844
  var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
824
845
  function validateProjectName(name) {
825
846
  if (isProjectName(name)) return [];
826
- return [{
827
- code: "INVALID_NAME",
828
- message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
829
- path: "name",
830
- value: name
831
- }];
847
+ return [
848
+ {
849
+ code: "INVALID_NAME",
850
+ message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
851
+ path: "name",
852
+ value: name
853
+ }
854
+ ];
832
855
  }
833
856
  function validateStringList(value, path) {
834
857
  if (value === void 0) return [];
835
858
  if (!Array.isArray(value)) {
836
- return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
859
+ return [
860
+ { code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }
861
+ ];
837
862
  }
838
863
  return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
839
864
  code: "INVALID_VALUE",
@@ -897,7 +922,12 @@ function validateAppConfig(config) {
897
922
  }
898
923
  if (config.env !== void 0) {
899
924
  if (!Array.isArray(config.env)) {
900
- errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
925
+ errors.push({
926
+ code: "INVALID_TYPE",
927
+ message: `'app.env' must be an array of variable names.`,
928
+ path: "app.env",
929
+ value: config.env
930
+ });
901
931
  } else {
902
932
  for (const envVar of config.env) {
903
933
  if (!isEnvVarName(envVar)) {
@@ -922,12 +952,14 @@ function validateServicePort(name, port) {
922
952
  return [];
923
953
  }
924
954
  }
925
- return [{
926
- code: "INVALID_PORT",
927
- message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
928
- path: `services.${name}.port`,
929
- value: port
930
- }];
955
+ return [
956
+ {
957
+ code: "INVALID_PORT",
958
+ message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
959
+ path: `services.${name}.port`,
960
+ value: port
961
+ }
962
+ ];
931
963
  }
932
964
  function validateServiceConfig(name, config) {
933
965
  const errors = [];
@@ -970,7 +1002,12 @@ function validateServiceConfig(name, config) {
970
1002
  errors.push(...validateServicePort(name, svc.port));
971
1003
  if (svc.env !== void 0) {
972
1004
  if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
973
- errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
1005
+ errors.push({
1006
+ code: "INVALID_TYPE",
1007
+ message: `'services.${name}.env' must be an object of strings.`,
1008
+ path: `services.${name}.env`,
1009
+ value: svc.env
1010
+ });
974
1011
  } else {
975
1012
  for (const [key, value] of Object.entries(svc.env)) {
976
1013
  if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
@@ -984,7 +1021,14 @@ function validateServiceConfig(name, config) {
984
1021
  }
985
1022
  }
986
1023
  }
987
- for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
1024
+ for (const key of [
1025
+ "user",
1026
+ "password",
1027
+ "database",
1028
+ "rootPassword",
1029
+ "command",
1030
+ "healthcheck"
1031
+ ]) {
988
1032
  const value = svc[key];
989
1033
  if (value !== void 0 && typeof value !== "string") {
990
1034
  errors.push({
@@ -1027,7 +1071,9 @@ function validateConfig(config) {
1027
1071
  if (typeof config !== "object" || config === null) {
1028
1072
  return {
1029
1073
  valid: false,
1030
- errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
1074
+ errors: [
1075
+ { code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }
1076
+ ]
1031
1077
  };
1032
1078
  }
1033
1079
  errors.push(...validateProjectName(config.name));
@@ -1054,7 +1100,10 @@ function validateResolvedConfig(config) {
1054
1100
  const usedPorts = /* @__PURE__ */ new Map();
1055
1101
  usedPorts.set(config.app.port, "app");
1056
1102
  for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
1057
- const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
1103
+ const published = [
1104
+ serviceConfig.hostPort ?? serviceConfig.port,
1105
+ ...serviceConfig.ports ?? []
1106
+ ];
1058
1107
  for (const port of published) {
1059
1108
  const owner = usedPorts.get(port);
1060
1109
  if (owner !== void 0) {
@@ -1881,7 +1930,8 @@ ${errorMessages}`, errors);
1881
1930
  }
1882
1931
  function isSafeOutputPath(fileName) {
1883
1932
  if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
1884
- if ((0, import_path3.isAbsolute)(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
1933
+ if ((0, import_path3.isAbsolute)(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\"))
1934
+ return false;
1885
1935
  const normalized = (0, import_path3.normalize)(fileName.replace(/\\/g, "/"));
1886
1936
  return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
1887
1937
  }
@@ -1897,11 +1947,11 @@ async function dockcraft(config, options = {}) {
1897
1947
  }
1898
1948
  const context = {
1899
1949
  config: resolved,
1900
- ports: new Map(
1901
- Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
1902
- ),
1950
+ ports: new Map(Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])),
1903
1951
  strings: new Map(
1904
- Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
1952
+ Object.entries(resolved.services).filter(
1953
+ (entry) => Boolean(entry[1].connectionString)
1954
+ ).map(([name, svc]) => [name, svc.connectionString])
1905
1955
  )
1906
1956
  };
1907
1957
  context.ports.set("app", resolved.app.port);