@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.js CHANGED
@@ -39,7 +39,10 @@ var NoPortAvailableError = class extends DockcraftError {
39
39
  };
40
40
  var FileWriteError = class extends DockcraftError {
41
41
  constructor(path, code = "FILE_WRITE_ERROR", cause) {
42
- super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
42
+ super(
43
+ `Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`,
44
+ code
45
+ );
43
46
  this.name = "FileWriteError";
44
47
  if (cause !== void 0) {
45
48
  this.cause = cause;
@@ -492,11 +495,7 @@ async function findAvailablePortInRange(start, end) {
492
495
  return void 0;
493
496
  }
494
497
  async function resolvePort(options = {}) {
495
- const {
496
- preferred = 3e3,
497
- strategy = "auto",
498
- range
499
- } = options;
498
+ const { preferred = 3e3, strategy = "auto", range } = options;
500
499
  switch (strategy) {
501
500
  case "fixed":
502
501
  if (await isPortAvailable(preferred)) {
@@ -585,12 +584,13 @@ function mergePreset(config, presetName) {
585
584
  );
586
585
  return merged;
587
586
  }
588
- async function resolveServicePorts(portConfig, containerPort) {
587
+ async function resolveServicePorts(portConfig, containerPort, claimed = /* @__PURE__ */ new Set()) {
589
588
  if (portConfig === "auto") {
590
- return {
591
- port: containerPort,
592
- hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
593
- };
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 };
594
594
  }
595
595
  if (typeof portConfig === "number") {
596
596
  return { port: containerPort, hostPort: portConfig };
@@ -598,22 +598,36 @@ async function resolveServicePorts(portConfig, containerPort) {
598
598
  if (portConfig && typeof portConfig === "object") {
599
599
  const startPort = portConfig.start ?? containerPort;
600
600
  const endPort = portConfig.end ?? startPort + 100;
601
- return {
602
- port: containerPort,
603
- hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
604
- };
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 };
605
611
  }
606
612
  return { port: containerPort };
607
613
  }
608
- async function resolveServiceConfig(name, config, projectName, definitions) {
614
+ async function resolveServiceConfig(name, config, projectName, definitions, claimedPorts) {
609
615
  if ("image" in config && config.image) {
610
- return resolveCustomService(name, config);
616
+ return resolveCustomService(
617
+ name,
618
+ config,
619
+ claimedPorts
620
+ );
611
621
  }
612
622
  const definition = definitions?.get(name) ?? getService(name);
613
623
  if (!definition) {
614
624
  throw new ServiceNotFoundError(name);
615
625
  }
616
- const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
626
+ const { port, hostPort } = await resolveServicePorts(
627
+ config.port,
628
+ definition.defaultPort,
629
+ claimedPorts
630
+ );
617
631
  const version = config.version ?? definition.defaultVersion;
618
632
  const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
619
633
  const env = { ...definition.env, ...config.env };
@@ -636,7 +650,8 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
636
650
  if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
637
651
  if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
638
652
  if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
639
- if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
653
+ if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE)
654
+ env.MONGO_INITDB_DATABASE = projectName;
640
655
  } else if (name === "redis") {
641
656
  const redisConfig = config;
642
657
  if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
@@ -662,14 +677,14 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
662
677
  }
663
678
  return resolvedConfig;
664
679
  }
665
- async function resolveCustomService(name, config) {
680
+ async function resolveCustomService(name, config, claimedPorts) {
666
681
  let containerPort = 80;
667
682
  if (typeof config.port === "number") {
668
683
  containerPort = config.port;
669
684
  } else if (config.port && typeof config.port === "object" && config.port.start) {
670
685
  containerPort = config.port.start;
671
686
  }
672
- const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
687
+ const { port, hostPort } = await resolveServicePorts(config.port, containerPort, claimedPorts);
673
688
  return {
674
689
  name,
675
690
  image: config.image,
@@ -702,14 +717,20 @@ async function resolveConfig(config) {
702
717
  const app = normalizeAppConfig({ ...appConfig, port: appPort });
703
718
  const definitions = pluginServiceDefinitions(finalConfig.plugins);
704
719
  const services2 = {};
720
+ const claimedPorts = /* @__PURE__ */ new Set([app.port]);
705
721
  for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
706
722
  if (serviceConfig) {
707
- services2[name] = await resolveServiceConfig(
723
+ const service = await resolveServiceConfig(
708
724
  name,
709
725
  serviceConfig,
710
726
  finalConfig.name,
711
- definitions
727
+ definitions,
728
+ claimedPorts
712
729
  );
730
+ services2[name] = service;
731
+ for (const port of [service.hostPort ?? service.port, ...service.ports ?? []]) {
732
+ claimedPorts.add(port);
733
+ }
713
734
  }
714
735
  }
715
736
  return {
@@ -777,17 +798,21 @@ function namedVolume(spec) {
777
798
  var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
778
799
  function validateProjectName(name) {
779
800
  if (isProjectName(name)) return [];
780
- return [{
781
- code: "INVALID_NAME",
782
- message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
783
- path: "name",
784
- value: name
785
- }];
801
+ return [
802
+ {
803
+ code: "INVALID_NAME",
804
+ message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
805
+ path: "name",
806
+ value: name
807
+ }
808
+ ];
786
809
  }
787
810
  function validateStringList(value, path) {
788
811
  if (value === void 0) return [];
789
812
  if (!Array.isArray(value)) {
790
- return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
813
+ return [
814
+ { code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }
815
+ ];
791
816
  }
792
817
  return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
793
818
  code: "INVALID_VALUE",
@@ -851,7 +876,12 @@ function validateAppConfig(config) {
851
876
  }
852
877
  if (config.env !== void 0) {
853
878
  if (!Array.isArray(config.env)) {
854
- errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
879
+ errors.push({
880
+ code: "INVALID_TYPE",
881
+ message: `'app.env' must be an array of variable names.`,
882
+ path: "app.env",
883
+ value: config.env
884
+ });
855
885
  } else {
856
886
  for (const envVar of config.env) {
857
887
  if (!isEnvVarName(envVar)) {
@@ -876,12 +906,14 @@ function validateServicePort(name, port) {
876
906
  return [];
877
907
  }
878
908
  }
879
- return [{
880
- code: "INVALID_PORT",
881
- message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
882
- path: `services.${name}.port`,
883
- value: port
884
- }];
909
+ return [
910
+ {
911
+ code: "INVALID_PORT",
912
+ message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
913
+ path: `services.${name}.port`,
914
+ value: port
915
+ }
916
+ ];
885
917
  }
886
918
  function validateServiceConfig(name, config) {
887
919
  const errors = [];
@@ -924,7 +956,12 @@ function validateServiceConfig(name, config) {
924
956
  errors.push(...validateServicePort(name, svc.port));
925
957
  if (svc.env !== void 0) {
926
958
  if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
927
- errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
959
+ errors.push({
960
+ code: "INVALID_TYPE",
961
+ message: `'services.${name}.env' must be an object of strings.`,
962
+ path: `services.${name}.env`,
963
+ value: svc.env
964
+ });
928
965
  } else {
929
966
  for (const [key, value] of Object.entries(svc.env)) {
930
967
  if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
@@ -938,7 +975,14 @@ function validateServiceConfig(name, config) {
938
975
  }
939
976
  }
940
977
  }
941
- for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
978
+ for (const key of [
979
+ "user",
980
+ "password",
981
+ "database",
982
+ "rootPassword",
983
+ "command",
984
+ "healthcheck"
985
+ ]) {
942
986
  const value = svc[key];
943
987
  if (value !== void 0 && typeof value !== "string") {
944
988
  errors.push({
@@ -981,7 +1025,9 @@ function validateConfig(config) {
981
1025
  if (typeof config !== "object" || config === null) {
982
1026
  return {
983
1027
  valid: false,
984
- errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
1028
+ errors: [
1029
+ { code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }
1030
+ ]
985
1031
  };
986
1032
  }
987
1033
  errors.push(...validateProjectName(config.name));
@@ -1008,7 +1054,10 @@ function validateResolvedConfig(config) {
1008
1054
  const usedPorts = /* @__PURE__ */ new Map();
1009
1055
  usedPorts.set(config.app.port, "app");
1010
1056
  for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
1011
- const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
1057
+ const published = [
1058
+ serviceConfig.hostPort ?? serviceConfig.port,
1059
+ ...serviceConfig.ports ?? []
1060
+ ];
1012
1061
  for (const port of published) {
1013
1062
  const owner = usedPorts.get(port);
1014
1063
  if (owner !== void 0) {
@@ -1835,7 +1884,8 @@ ${errorMessages}`, errors);
1835
1884
  }
1836
1885
  function isSafeOutputPath(fileName) {
1837
1886
  if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
1838
- if (isAbsolute2(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
1887
+ if (isAbsolute2(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\"))
1888
+ return false;
1839
1889
  const normalized = normalize(fileName.replace(/\\/g, "/"));
1840
1890
  return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
1841
1891
  }
@@ -1851,11 +1901,11 @@ async function dockcraft(config, options = {}) {
1851
1901
  }
1852
1902
  const context = {
1853
1903
  config: resolved,
1854
- ports: new Map(
1855
- Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
1856
- ),
1904
+ ports: new Map(Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])),
1857
1905
  strings: new Map(
1858
- Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
1906
+ Object.entries(resolved.services).filter(
1907
+ (entry) => Boolean(entry[1].connectionString)
1908
+ ).map(([name, svc]) => [name, svc.connectionString])
1859
1909
  )
1860
1910
  };
1861
1911
  context.ports.set("app", resolved.app.port);