@oxog/dockcraft 1.2.1 → 1.3.1

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/CHANGELOG.md CHANGED
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.3.1] - 2026-12-02
11
+
12
+ ### Documentation
13
+ - README examples are run by the test suite (`tests/readme/readme-examples.test.ts`): config
14
+ fragments are type-checked inside `defineConfig()` and generated with, ports are probed against
15
+ an in-memory port table and files are written to a temporary directory; the test fails when a
16
+ README example changes without it.
17
+
18
+ ## [1.3.0] - 2026-12-02
19
+
20
+ ### Fixed
21
+
22
+ - **`dockcraft init -p 8080` used `8080` as both the port and the preset.** Any
23
+ single-letter option stood for every long option starting with that letter
24
+ (`-p` for `--port` and `--preset`). Short options are now an explicit list
25
+ (`-n --name`, `-s --services`, `-o --output`, `-f --force`, `-y --yes`,
26
+ `-c --config` / `--check`, `-d --dry-run`, `-r --range`, `-j --json`, `-h`, `-v`,
27
+ shown in each command's `--help`), and a letter that could mean several
28
+ options of the command fails with `Option -p is ambiguous: use --preset or --port.`
29
+
30
+ ### Documentation
31
+
32
+ - `.env.example` is written in the Docker Compose `.env` format; the README now
33
+ says so and explains how the npm `dotenv` package reads its double-quoted values
34
+ differently.
35
+
10
36
  ## [1.2.1] - 2026-12-02
11
37
 
12
38
  ### Fixed
@@ -242,6 +268,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
242
268
 
243
269
  ---
244
270
 
271
+ [1.3.1]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.3.1
272
+ [1.3.0]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.3.0
245
273
  [1.2.1]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.2.1
246
274
  [1.2.0]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.2.0
247
275
  [1.1.1]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.1.1
package/README.md CHANGED
@@ -438,6 +438,16 @@ It lists the same variables the app container gets: `POSTGRES_URL` (also as
438
438
  plugin services, and the `*_HOST`, `*_PORT`, `*_USER`, `*_PASSWORD` and
439
439
  database variables of each service.
440
440
 
441
+ Values are written in the Docker Compose `.env` format: plain values as-is,
442
+ values with spaces or special characters (`#`, `$`, quotes, ...) in single
443
+ quotes (taken literally), and values that contain a single quote or a line
444
+ break in double quotes with `\\`, `"`, `$`, `\n` and `\r` escaped. Compose
445
+ (`docker compose --env-file`, `env_file:`) reads every value back exactly.
446
+ Other loaders differ only for those double-quoted values: the npm `dotenv`
447
+ package, for example, does not unescape `\\`, `\"` or `\$`, so check such
448
+ values (they only occur when a password or other value contains a `'`) if
449
+ your app loads the file with `dotenv`.
450
+
441
451
  ## License
442
452
 
443
453
  MIT
package/dist/cli/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  // src/version.ts
9
- var VERSION = "1.2.1";
9
+ var VERSION = "1.3.1";
10
10
 
11
11
  // src/cli/parser.ts
12
12
  function parseArgs(args) {
@@ -73,17 +73,48 @@ function parseArgs(args) {
73
73
  }
74
74
  return result;
75
75
  }
76
+ var SHORT_ALIASES = {
77
+ help: "h",
78
+ version: "v",
79
+ yes: "y",
80
+ force: "f",
81
+ name: "n",
82
+ services: "s",
83
+ output: "o",
84
+ config: "c",
85
+ "dry-run": "d",
86
+ check: "c",
87
+ range: "r",
88
+ json: "j"
89
+ };
90
+ var COMMAND_OPTIONS = {
91
+ init: ["preset", "name", "port", "services", "output", "force", "yes"],
92
+ generate: ["config", "dry-run", "force", "output"],
93
+ ports: ["check", "range", "json"]
94
+ };
95
+ function ambiguousShortOption(args) {
96
+ const longs = COMMAND_OPTIONS[args.command] ?? [];
97
+ for (const key of [...args.flags, ...args.options.keys()]) {
98
+ if (key.length !== 1 || key === "h" || key === "v") continue;
99
+ if (longs.some((long) => SHORT_ALIASES[long] === key)) continue;
100
+ const candidates = longs.filter((long) => long.startsWith(key));
101
+ if (candidates.length > 1) {
102
+ return `Option -${key} is ambiguous: use ${candidates.map((long) => `--${long}`).join(" or ")}.`;
103
+ }
104
+ }
105
+ return void 0;
106
+ }
76
107
  function hasFlag(args, flag) {
77
108
  if (args.flags.has(flag)) return true;
78
- const firstChar = flag[0];
79
- return firstChar !== void 0 && args.flags.has(firstChar);
109
+ const short = SHORT_ALIASES[flag];
110
+ return short !== void 0 && args.flags.has(short);
80
111
  }
81
112
  function getOption(args, option, defaultValue = "") {
82
113
  const value = args.options.get(option);
83
114
  if (value !== void 0) return value;
84
- const firstChar = option[0];
85
- if (firstChar !== void 0) {
86
- const shortValue = args.options.get(firstChar);
115
+ const short = SHORT_ALIASES[option];
116
+ if (short !== void 0) {
117
+ const shortValue = args.options.get(short);
87
118
  if (shortValue !== void 0) return shortValue;
88
119
  }
89
120
  return defaultValue;
@@ -299,7 +330,10 @@ var NoPortAvailableError = class extends DockcraftError {
299
330
  };
300
331
  var FileWriteError = class extends DockcraftError {
301
332
  constructor(path, code = "FILE_WRITE_ERROR", cause) {
302
- super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
333
+ super(
334
+ `Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`,
335
+ code
336
+ );
303
337
  this.name = "FileWriteError";
304
338
  if (cause !== void 0) {
305
339
  this.cause = cause;
@@ -706,7 +740,9 @@ async function init(args) {
706
740
  }
707
741
  const unknown = selectedServices.filter((s) => !INIT_SERVICES.includes(s));
708
742
  if (unknown.length > 0) {
709
- throw new Error(`Unknown service(s): ${unknown.join(", ")}. Available services: ${INIT_SERVICES.join(", ")}`);
743
+ throw new Error(
744
+ `Unknown service(s): ${unknown.join(", ")}. Available services: ${INIT_SERVICES.join(", ")}`
745
+ );
710
746
  }
711
747
  const configPath = join2(outputDir, INIT_CONFIG_FILE);
712
748
  if (!force && await fileExists(configPath)) {
@@ -941,11 +977,7 @@ async function findAvailablePortInRange(start, end) {
941
977
  return void 0;
942
978
  }
943
979
  async function resolvePort(options = {}) {
944
- const {
945
- preferred = 3e3,
946
- strategy = "auto",
947
- range
948
- } = options;
980
+ const { preferred = 3e3, strategy = "auto", range } = options;
949
981
  switch (strategy) {
950
982
  case "fixed":
951
983
  if (await isPortAvailable(preferred)) {
@@ -1090,7 +1122,11 @@ async function resolveServicePorts(portConfig, containerPort) {
1090
1122
  const endPort = portConfig.end ?? startPort + 100;
1091
1123
  return {
1092
1124
  port: containerPort,
1093
- hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
1125
+ hostPort: await resolvePort({
1126
+ preferred: startPort,
1127
+ strategy: "range",
1128
+ range: [startPort, endPort]
1129
+ })
1094
1130
  };
1095
1131
  }
1096
1132
  return { port: containerPort };
@@ -1126,7 +1162,8 @@ async function resolveServiceConfig(name, config, projectName, definitions) {
1126
1162
  if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
1127
1163
  if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
1128
1164
  if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
1129
- if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
1165
+ if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE)
1166
+ env.MONGO_INITDB_DATABASE = projectName;
1130
1167
  } else if (name === "redis") {
1131
1168
  const redisConfig = config;
1132
1169
  if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
@@ -1214,17 +1251,21 @@ async function resolveConfig(config) {
1214
1251
  var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
1215
1252
  function validateProjectName(name) {
1216
1253
  if (isProjectName(name)) return [];
1217
- return [{
1218
- code: "INVALID_NAME",
1219
- message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
1220
- path: "name",
1221
- value: name
1222
- }];
1254
+ return [
1255
+ {
1256
+ code: "INVALID_NAME",
1257
+ message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
1258
+ path: "name",
1259
+ value: name
1260
+ }
1261
+ ];
1223
1262
  }
1224
1263
  function validateStringList(value, path) {
1225
1264
  if (value === void 0) return [];
1226
1265
  if (!Array.isArray(value)) {
1227
- return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
1266
+ return [
1267
+ { code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }
1268
+ ];
1228
1269
  }
1229
1270
  return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
1230
1271
  code: "INVALID_VALUE",
@@ -1288,7 +1329,12 @@ function validateAppConfig(config) {
1288
1329
  }
1289
1330
  if (config.env !== void 0) {
1290
1331
  if (!Array.isArray(config.env)) {
1291
- errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
1332
+ errors.push({
1333
+ code: "INVALID_TYPE",
1334
+ message: `'app.env' must be an array of variable names.`,
1335
+ path: "app.env",
1336
+ value: config.env
1337
+ });
1292
1338
  } else {
1293
1339
  for (const envVar of config.env) {
1294
1340
  if (!isEnvVarName(envVar)) {
@@ -1313,12 +1359,14 @@ function validateServicePort(name, port) {
1313
1359
  return [];
1314
1360
  }
1315
1361
  }
1316
- return [{
1317
- code: "INVALID_PORT",
1318
- message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
1319
- path: `services.${name}.port`,
1320
- value: port
1321
- }];
1362
+ return [
1363
+ {
1364
+ code: "INVALID_PORT",
1365
+ message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
1366
+ path: `services.${name}.port`,
1367
+ value: port
1368
+ }
1369
+ ];
1322
1370
  }
1323
1371
  function validateServiceConfig(name, config) {
1324
1372
  const errors = [];
@@ -1361,7 +1409,12 @@ function validateServiceConfig(name, config) {
1361
1409
  errors.push(...validateServicePort(name, svc.port));
1362
1410
  if (svc.env !== void 0) {
1363
1411
  if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
1364
- errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
1412
+ errors.push({
1413
+ code: "INVALID_TYPE",
1414
+ message: `'services.${name}.env' must be an object of strings.`,
1415
+ path: `services.${name}.env`,
1416
+ value: svc.env
1417
+ });
1365
1418
  } else {
1366
1419
  for (const [key, value] of Object.entries(svc.env)) {
1367
1420
  if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
@@ -1375,7 +1428,14 @@ function validateServiceConfig(name, config) {
1375
1428
  }
1376
1429
  }
1377
1430
  }
1378
- for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
1431
+ for (const key of [
1432
+ "user",
1433
+ "password",
1434
+ "database",
1435
+ "rootPassword",
1436
+ "command",
1437
+ "healthcheck"
1438
+ ]) {
1379
1439
  const value = svc[key];
1380
1440
  if (value !== void 0 && typeof value !== "string") {
1381
1441
  errors.push({
@@ -1418,7 +1478,9 @@ function validateConfig(config) {
1418
1478
  if (typeof config !== "object" || config === null) {
1419
1479
  return {
1420
1480
  valid: false,
1421
- errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
1481
+ errors: [
1482
+ { code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }
1483
+ ]
1422
1484
  };
1423
1485
  }
1424
1486
  errors.push(...validateProjectName(config.name));
@@ -1445,7 +1507,10 @@ function validateResolvedConfig(config) {
1445
1507
  const usedPorts = /* @__PURE__ */ new Map();
1446
1508
  usedPorts.set(config.app.port, "app");
1447
1509
  for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
1448
- const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
1510
+ const published = [
1511
+ serviceConfig.hostPort ?? serviceConfig.port,
1512
+ ...serviceConfig.ports ?? []
1513
+ ];
1449
1514
  for (const port of published) {
1450
1515
  const owner = usedPorts.get(port);
1451
1516
  if (owner !== void 0) {
@@ -2226,7 +2291,8 @@ ${errorMessages}`, errors);
2226
2291
  }
2227
2292
  function isSafeOutputPath(fileName) {
2228
2293
  if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
2229
- if (isAbsolute3(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
2294
+ if (isAbsolute3(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\"))
2295
+ return false;
2230
2296
  const normalized = normalize(fileName.replace(/\\/g, "/"));
2231
2297
  return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
2232
2298
  }
@@ -2242,11 +2308,11 @@ async function dockcraft(config, options = {}) {
2242
2308
  }
2243
2309
  const context = {
2244
2310
  config: resolved,
2245
- ports: new Map(
2246
- Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
2247
- ),
2311
+ ports: new Map(Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])),
2248
2312
  strings: new Map(
2249
- Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
2313
+ Object.entries(resolved.services).filter(
2314
+ (entry) => Boolean(entry[1].connectionString)
2315
+ ).map(([name, svc]) => [name, svc.connectionString])
2250
2316
  )
2251
2317
  };
2252
2318
  context.ports.set("app", resolved.app.port);
@@ -2369,12 +2435,15 @@ async function ports(args) {
2369
2435
  if (asJson) {
2370
2436
  json(Object.fromEntries(results));
2371
2437
  } else {
2372
- table(["Port", "Status"], [
2373
- ...Array.from(results.entries()).map(([port, available]) => [
2374
- String(port),
2375
- available ? "\u2713 Available" : "\u2717 In use"
2376
- ])
2377
- ]);
2438
+ table(
2439
+ ["Port", "Status"],
2440
+ [
2441
+ ...Array.from(results.entries()).map(([port, available]) => [
2442
+ String(port),
2443
+ available ? "\u2713 Available" : "\u2717 In use"
2444
+ ])
2445
+ ]
2446
+ );
2378
2447
  }
2379
2448
  return;
2380
2449
  }
@@ -2383,7 +2452,9 @@ async function ports(args) {
2383
2452
  const start = Number(match?.[1]);
2384
2453
  const end = Number(match?.[2]);
2385
2454
  if (!match || !isPort(start) || !isPort(end) || start > end) {
2386
- throw new Error("Invalid range format. Use: --range START-END with ports 1-65535 (e.g., 3000-3010)");
2455
+ throw new Error(
2456
+ "Invalid range format. Use: --range START-END with ports 1-65535 (e.g., 3000-3010)"
2457
+ );
2387
2458
  }
2388
2459
  const port = await findAvailablePortInRange(start, end);
2389
2460
  if (asJson) {
@@ -2400,9 +2471,10 @@ async function ports(args) {
2400
2471
  json(defaults);
2401
2472
  } else {
2402
2473
  subtitle("Default service ports:");
2403
- table(["Service", "Port"], [
2404
- ...Object.entries(defaults).map(([service, port]) => [service, String(port)])
2405
- ]);
2474
+ table(
2475
+ ["Service", "Port"],
2476
+ [...Object.entries(defaults).map(([service, port]) => [service, String(port)])]
2477
+ );
2406
2478
  }
2407
2479
  }
2408
2480
 
@@ -2412,7 +2484,9 @@ function presets2(args) {
2412
2484
  if (presetName) {
2413
2485
  const preset = getPreset(presetName);
2414
2486
  if (!preset) {
2415
- throw new Error(`Preset '${presetName}' not found. Run "dockcraft presets" to list available presets.`);
2487
+ throw new Error(
2488
+ `Preset '${presetName}' not found. Run "dockcraft presets" to list available presets.`
2489
+ );
2416
2490
  }
2417
2491
  title(`Preset: ${presetName}`);
2418
2492
  if (preset.description) {
@@ -2484,13 +2558,13 @@ USAGE:
2484
2558
  dockcraft init [options]
2485
2559
 
2486
2560
  OPTIONS:
2487
- --preset <name> Use a preset configuration
2488
- --name <name> Project name
2489
- --port <port> App port
2490
- --services <list> Comma-separated service list
2491
- --output <dir> Directory to write dockcraft.config.mts to
2492
- --force Overwrite an existing config file
2493
- -y, --yes Skip prompts, use defaults (required without a terminal)
2561
+ --preset <name> Use a preset configuration
2562
+ -n, --name <name> Project name
2563
+ --port <port> App port
2564
+ -s, --services <list> Comma-separated service list
2565
+ -o, --output <dir> Directory to write dockcraft.config.mts to
2566
+ -f, --force Overwrite an existing config file
2567
+ -y, --yes Skip prompts, use defaults (required without a terminal)
2494
2568
  `);
2495
2569
  break;
2496
2570
  case "generate":
@@ -2499,10 +2573,10 @@ USAGE:
2499
2573
  dockcraft generate [options]
2500
2574
 
2501
2575
  OPTIONS:
2502
- --config <file> Config file path (default: first of dockcraft.config.{ts,mts,js,mjs,cjs})
2503
- --dry-run Preview without writing
2504
- --force Overwrite existing files
2505
- --output <dir> Output directory
2576
+ -c, --config <file> Config file path (default: first of dockcraft.config.{ts,mts,js,mjs,cjs})
2577
+ -d, --dry-run Preview without writing
2578
+ -f, --force Overwrite existing files
2579
+ -o, --output <dir> Output directory
2506
2580
  `);
2507
2581
  break;
2508
2582
  case "ports":
@@ -2511,9 +2585,9 @@ USAGE:
2511
2585
  dockcraft ports [options]
2512
2586
 
2513
2587
  OPTIONS:
2514
- --check <ports> Check specific ports (comma-separated)
2515
- --range <start-end> Scan port range
2516
- --json Output as JSON
2588
+ -c, --check <ports> Check specific ports (comma-separated)
2589
+ -r, --range <start-end> Scan port range
2590
+ -j, --json Output as JSON
2517
2591
  `);
2518
2592
  break;
2519
2593
  case "presets":
@@ -2563,6 +2637,12 @@ async function main(cliArgs = process.argv.slice(2)) {
2563
2637
  process.exitCode = 1;
2564
2638
  return;
2565
2639
  }
2640
+ const ambiguous = ambiguousShortOption(args);
2641
+ if (ambiguous) {
2642
+ error(ambiguous);
2643
+ process.exitCode = 1;
2644
+ return;
2645
+ }
2566
2646
  try {
2567
2647
  await handler(args);
2568
2648
  } catch (err) {