@oxog/dockcraft 1.0.0 → 1.1.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 +220 -0
- package/README.md +132 -21
- package/dist/cli/index.js +912 -439
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +736 -386
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -12
- package/dist/index.d.ts +29 -12
- package/dist/index.js +732 -382
- package/dist/index.js.map +1 -1
- package/package.json +32 -16
- package/dist/cli/index.cjs +0 -2095
- package/dist/cli/index.cjs.map +0 -1
- package/dist/cli/index.d.cts +0 -6
- package/dist/cli/index.d.ts +0 -6
package/dist/cli/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
* @oxog/dockcraft - Zero-Dependency NPM Package
|
|
4
|
+
* GitHub: https://github.com/ersinkoc/DockCraft
|
|
5
|
+
* License: MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// src/version.ts
|
|
9
|
+
var VERSION = "1.1.1";
|
|
6
10
|
|
|
7
11
|
// src/cli/parser.ts
|
|
8
12
|
function parseArgs(args) {
|
|
@@ -168,6 +172,9 @@ function kv(pairs) {
|
|
|
168
172
|
}
|
|
169
173
|
}
|
|
170
174
|
|
|
175
|
+
// src/cli/commands/init.ts
|
|
176
|
+
import { basename as basename2, join as join2 } from "path";
|
|
177
|
+
|
|
171
178
|
// src/cli/prompts.ts
|
|
172
179
|
import { createInterface } from "readline";
|
|
173
180
|
async function prompt(question, defaultValue = "") {
|
|
@@ -179,10 +186,10 @@ async function prompt(question, defaultValue = "") {
|
|
|
179
186
|
output: process.stdout
|
|
180
187
|
});
|
|
181
188
|
const query = defaultValue ? `${question} (${defaultValue}): ` : `${question}: `;
|
|
182
|
-
return new Promise((
|
|
189
|
+
return new Promise((resolve2) => {
|
|
183
190
|
rl.question(query, (answer) => {
|
|
184
191
|
rl.close();
|
|
185
|
-
|
|
192
|
+
resolve2(answer || defaultValue);
|
|
186
193
|
});
|
|
187
194
|
});
|
|
188
195
|
}
|
|
@@ -195,10 +202,10 @@ async function confirm(question) {
|
|
|
195
202
|
output: process.stdout
|
|
196
203
|
});
|
|
197
204
|
const query = `${question} (y/N): `;
|
|
198
|
-
return new Promise((
|
|
205
|
+
return new Promise((resolve2) => {
|
|
199
206
|
rl.question(query, (answer) => {
|
|
200
207
|
rl.close();
|
|
201
|
-
|
|
208
|
+
resolve2(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes");
|
|
202
209
|
});
|
|
203
210
|
});
|
|
204
211
|
}
|
|
@@ -216,14 +223,14 @@ async function select(question, options) {
|
|
|
216
223
|
console.log(` ${i + 1}. ${options[i]}`);
|
|
217
224
|
}
|
|
218
225
|
console.log("");
|
|
219
|
-
return new Promise((
|
|
226
|
+
return new Promise((resolve2) => {
|
|
220
227
|
rl.question(`Select (1-${options.length}): `, (answer) => {
|
|
221
228
|
rl.close();
|
|
222
229
|
const index = parseInt(answer ?? "0", 10) - 1;
|
|
223
230
|
if (index >= 0 && index < options.length) {
|
|
224
|
-
|
|
231
|
+
resolve2(options[index] ?? "");
|
|
225
232
|
} else {
|
|
226
|
-
|
|
233
|
+
resolve2(options[0] ?? "");
|
|
227
234
|
}
|
|
228
235
|
});
|
|
229
236
|
});
|
|
@@ -243,12 +250,12 @@ async function multiselect(question, options) {
|
|
|
243
250
|
console.log(` ${i + 1}. ${options[i]}`);
|
|
244
251
|
}
|
|
245
252
|
console.log("");
|
|
246
|
-
return new Promise((
|
|
253
|
+
return new Promise((resolve2) => {
|
|
247
254
|
rl.question(`Select (1-${options.length}): `, (answer) => {
|
|
248
255
|
rl.close();
|
|
249
256
|
const indices = (answer ?? "").split(",").map((s) => parseInt(s.trim(), 10) - 1);
|
|
250
257
|
const selected = indices.filter((i) => i >= 0 && i < options.length).map((i) => options[i]).filter((s) => s !== void 0);
|
|
251
|
-
|
|
258
|
+
resolve2(selected.length > 0 ? selected : [options[0] ?? ""]);
|
|
252
259
|
});
|
|
253
260
|
});
|
|
254
261
|
}
|
|
@@ -256,29 +263,95 @@ async function multiselect(question, options) {
|
|
|
256
263
|
// src/utils/fs.ts
|
|
257
264
|
import { promises as fs } from "fs";
|
|
258
265
|
import { join, dirname, basename, extname, relative } from "path";
|
|
266
|
+
|
|
267
|
+
// src/types.ts
|
|
268
|
+
var DockcraftError = class extends Error {
|
|
269
|
+
constructor(message, code) {
|
|
270
|
+
super(message);
|
|
271
|
+
this.code = code;
|
|
272
|
+
this.name = "DockcraftError";
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
var ConfigError = class extends DockcraftError {
|
|
276
|
+
constructor(message, code = "CONFIG_ERROR") {
|
|
277
|
+
super(message, code);
|
|
278
|
+
this.name = "ConfigError";
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
var ValidationError = class extends DockcraftError {
|
|
282
|
+
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
283
|
+
super(message, code);
|
|
284
|
+
this.errors = errors;
|
|
285
|
+
this.name = "ValidationError";
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
var PortConflictError = class extends DockcraftError {
|
|
289
|
+
constructor(port, code = "PORT_CONFLICT") {
|
|
290
|
+
super(`Port ${port} is already in use`, code);
|
|
291
|
+
this.name = "PortConflictError";
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var NoPortAvailableError = class extends DockcraftError {
|
|
295
|
+
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
296
|
+
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
297
|
+
this.name = "NoPortAvailableError";
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
var FileWriteError = class extends DockcraftError {
|
|
301
|
+
constructor(path, code = "FILE_WRITE_ERROR", cause) {
|
|
302
|
+
super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
|
|
303
|
+
this.name = "FileWriteError";
|
|
304
|
+
if (cause !== void 0) {
|
|
305
|
+
this.cause = cause;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
var PresetNotFoundError = class extends DockcraftError {
|
|
310
|
+
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
311
|
+
super(`Preset '${name}' not found`, code);
|
|
312
|
+
this.name = "PresetNotFoundError";
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
var ServiceNotFoundError = class extends DockcraftError {
|
|
316
|
+
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
317
|
+
super(`Service '${name}' not found`, code);
|
|
318
|
+
this.name = "ServiceNotFoundError";
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// src/utils/fs.ts
|
|
259
323
|
async function ensureDir(path) {
|
|
260
324
|
try {
|
|
261
325
|
await fs.mkdir(path, { recursive: true });
|
|
262
|
-
} catch (
|
|
263
|
-
const err =
|
|
326
|
+
} catch (error2) {
|
|
327
|
+
const err = error2;
|
|
264
328
|
if (err.code !== "EEXIST") {
|
|
265
|
-
throw
|
|
329
|
+
throw error2;
|
|
266
330
|
}
|
|
267
331
|
}
|
|
268
332
|
}
|
|
269
|
-
async function writeFile(filePath, content) {
|
|
333
|
+
async function writeFile(filePath, content, mode) {
|
|
334
|
+
try {
|
|
335
|
+
await writeFileAtomic(filePath, content, mode);
|
|
336
|
+
} catch (error2) {
|
|
337
|
+
throw new FileWriteError(filePath, "FILE_WRITE_ERROR", error2);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async function writeFileAtomic(filePath, content, mode) {
|
|
270
341
|
const dir = dirname(filePath);
|
|
271
342
|
await ensureDir(dir);
|
|
272
|
-
const
|
|
343
|
+
const suffix = `${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2)}`;
|
|
344
|
+
const tempPath = join(dir, `.${basename(filePath)}.${suffix}.tmp`);
|
|
273
345
|
try {
|
|
274
|
-
await fs.writeFile(tempPath, content, "utf8");
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
try {
|
|
278
|
-
await fs.unlink(tempPath);
|
|
279
|
-
} catch {
|
|
346
|
+
await fs.writeFile(tempPath, content, { encoding: "utf8", mode });
|
|
347
|
+
if (mode !== void 0) {
|
|
348
|
+
await fs.chmod(tempPath, mode);
|
|
280
349
|
}
|
|
281
|
-
|
|
350
|
+
await fs.rename(tempPath, filePath);
|
|
351
|
+
} catch (error2) {
|
|
352
|
+
await fs.unlink(tempPath).catch(() => {
|
|
353
|
+
});
|
|
354
|
+
throw error2;
|
|
282
355
|
}
|
|
283
356
|
}
|
|
284
357
|
async function fileExists(filePath) {
|
|
@@ -528,53 +601,136 @@ registerPreset("nest-postgres", nestPostgresPreset);
|
|
|
528
601
|
registerPreset("nest-mongo", nestMongoPreset);
|
|
529
602
|
registerPreset("node-minimal", nodeMinimalPreset);
|
|
530
603
|
|
|
604
|
+
// src/utils/validation.ts
|
|
605
|
+
var MIN_PORT = 1;
|
|
606
|
+
var MAX_PORT = 65535;
|
|
607
|
+
function isPort(value) {
|
|
608
|
+
return typeof value === "number" && Number.isInteger(value) && value >= MIN_PORT && value <= MAX_PORT;
|
|
609
|
+
}
|
|
610
|
+
function isEnvVarName(value) {
|
|
611
|
+
if (typeof value !== "string") return false;
|
|
612
|
+
return /^[A-Z_][A-Z0-9_]*$/.test(value);
|
|
613
|
+
}
|
|
614
|
+
function isVersion(value) {
|
|
615
|
+
if (typeof value !== "string") return false;
|
|
616
|
+
return /^\w[\w.-]{0,127}$/.test(value);
|
|
617
|
+
}
|
|
618
|
+
function isProjectName(value) {
|
|
619
|
+
if (typeof value !== "string") return false;
|
|
620
|
+
if (value.length === 0 || value.length > 100) return false;
|
|
621
|
+
return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(value);
|
|
622
|
+
}
|
|
623
|
+
function isComposeServiceName(value) {
|
|
624
|
+
if (typeof value !== "string") return false;
|
|
625
|
+
return /^[a-zA-Z0-9][\w.-]{0,62}$/.test(value);
|
|
626
|
+
}
|
|
627
|
+
var DOMAIN_COMPONENT = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
|
|
628
|
+
var DOMAIN = `${DOMAIN_COMPONENT}(?:\\.${DOMAIN_COMPONENT})*(?::[0-9]+)?`;
|
|
629
|
+
var PATH_COMPONENT = "[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*";
|
|
630
|
+
var TAG = "\\w[\\w.-]{0,127}";
|
|
631
|
+
var DIGEST = "[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}";
|
|
632
|
+
var IMAGE_REFERENCE = new RegExp(
|
|
633
|
+
`^(?:${DOMAIN}/)?${PATH_COMPONENT}(?:/${PATH_COMPONENT})*(?::${TAG})?(?:@${DIGEST})?$`
|
|
634
|
+
);
|
|
635
|
+
function isDockerImage(value) {
|
|
636
|
+
if (typeof value !== "string") return false;
|
|
637
|
+
if (value.length === 0 || value.length > 255) return false;
|
|
638
|
+
return IMAGE_REFERENCE.test(value);
|
|
639
|
+
}
|
|
640
|
+
function isHealthcheckPath(value) {
|
|
641
|
+
if (typeof value !== "string") return false;
|
|
642
|
+
return value.length <= 2048 && /^\/[\w\-.~%/?=&+,;:@]*$/.test(value);
|
|
643
|
+
}
|
|
644
|
+
function isSingleLine(value) {
|
|
645
|
+
return typeof value === "string" && !/[\r\n\0]/.test(value);
|
|
646
|
+
}
|
|
647
|
+
function isContainerPath(value) {
|
|
648
|
+
return typeof value === "string" && /^\/[\w./-]*$/.test(value);
|
|
649
|
+
}
|
|
650
|
+
function namedVolume(spec) {
|
|
651
|
+
const index = spec.indexOf(":");
|
|
652
|
+
if (index <= 0) return void 0;
|
|
653
|
+
const source = spec.slice(0, index);
|
|
654
|
+
return /^[a-zA-Z0-9][\w.-]*$/.test(source) ? source : void 0;
|
|
655
|
+
}
|
|
656
|
+
|
|
531
657
|
// src/cli/commands/init.ts
|
|
658
|
+
var INIT_CONFIG_FILE = "dockcraft.config.mts";
|
|
659
|
+
var INIT_SERVICES = ["postgres", "mysql", "mongo", "redis"];
|
|
660
|
+
function defaultProjectName(dir) {
|
|
661
|
+
const name = basename2(dir).replace(/[^a-zA-Z0-9_-]/g, "-").replace(/^[^a-zA-Z]+/, "");
|
|
662
|
+
return isProjectName(name) ? name : "my-app";
|
|
663
|
+
}
|
|
664
|
+
function noTerminalMessage(question) {
|
|
665
|
+
return `Cannot ask "${question.replace(/:$/, "")}": stdin is not a terminal. Run "dockcraft init" in a terminal, or pass -y/--yes to use the defaults (and --name, --port, --preset, --services, --output to set values).`;
|
|
666
|
+
}
|
|
532
667
|
async function init(args) {
|
|
533
668
|
title("Dockcraft Init");
|
|
534
|
-
const name = getOption(args, "name") || await prompt("Project name");
|
|
535
|
-
const preset = getOption(args, "preset");
|
|
536
|
-
const servicesOption = getOption(args, "services");
|
|
537
|
-
const port = getOption(args, "port") || await prompt("App port", "3000");
|
|
538
|
-
const outputDir = getOption(args, "output") || await prompt("Output directory", "./");
|
|
539
669
|
const skipPrompts = getBooleanFlag(args, "yes");
|
|
670
|
+
const force = getBooleanFlag(args, "force");
|
|
671
|
+
const interactive = (question, ask2) => process.stdin.isTTY ? ask2() : Promise.reject(new Error(noTerminalMessage(question)));
|
|
672
|
+
const ask = (question, defaultValue) => skipPrompts ? Promise.resolve(defaultValue) : interactive(question, () => prompt(question, defaultValue));
|
|
673
|
+
const name = getOption(args, "name") || await ask("Project name", defaultProjectName(process.cwd()));
|
|
674
|
+
if (!isProjectName(name)) {
|
|
675
|
+
throw new Error(
|
|
676
|
+
`Invalid project name '${name}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
const preset = getOption(args, "preset");
|
|
680
|
+
const presets3 = listPresets();
|
|
681
|
+
if (preset && !presets3.includes(preset)) {
|
|
682
|
+
throw new Error(`Unknown preset '${preset}'. Available presets: ${presets3.join(", ")}`);
|
|
683
|
+
}
|
|
684
|
+
const portInput = getOption(args, "port") || await ask("App port", "3000");
|
|
685
|
+
const port = portInput === "auto" ? "auto" : /^\d+$/.test(portInput) ? Number(portInput) : NaN;
|
|
686
|
+
if (port !== "auto" && !isPort(port)) {
|
|
687
|
+
throw new Error(`Invalid port '${portInput}'. Must be a number between 1-65535 or 'auto'.`);
|
|
688
|
+
}
|
|
689
|
+
const outputDir = getOption(args, "output") || await ask("Output directory", "./");
|
|
540
690
|
info(`Creating config for "${name}"`);
|
|
541
691
|
let selectedPreset = preset;
|
|
542
692
|
if (!selectedPreset && !skipPrompts) {
|
|
543
|
-
const
|
|
544
|
-
const usePreset = await confirm("Use a preset?");
|
|
693
|
+
const usePreset = await interactive("Use a preset?", () => confirm("Use a preset?"));
|
|
545
694
|
if (usePreset) {
|
|
546
|
-
|
|
547
|
-
selectedPreset = presetName;
|
|
695
|
+
selectedPreset = await select("Select preset:", presets3);
|
|
548
696
|
}
|
|
549
697
|
}
|
|
550
698
|
let selectedServices = [];
|
|
551
|
-
if (
|
|
699
|
+
if (getOption(args, "services")) {
|
|
552
700
|
selectedServices = parseList(args, "services");
|
|
553
701
|
} else if (!skipPrompts && !selectedPreset) {
|
|
554
|
-
|
|
555
|
-
|
|
702
|
+
selectedServices = await interactive(
|
|
703
|
+
"Select services:",
|
|
704
|
+
() => multiselect("Select services:", [...INIT_SERVICES])
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
const unknown = selectedServices.filter((s) => !INIT_SERVICES.includes(s));
|
|
708
|
+
if (unknown.length > 0) {
|
|
709
|
+
throw new Error(`Unknown service(s): ${unknown.join(", ")}. Available services: ${INIT_SERVICES.join(", ")}`);
|
|
556
710
|
}
|
|
557
|
-
const
|
|
558
|
-
|
|
559
|
-
|
|
711
|
+
const configPath = join2(outputDir, INIT_CONFIG_FILE);
|
|
712
|
+
if (!force && await fileExists(configPath)) {
|
|
713
|
+
throw new Error(`${configPath} already exists. Use --force to overwrite it.`);
|
|
714
|
+
}
|
|
715
|
+
await writeFile(configPath, generateConfig(name, selectedPreset, selectedServices, port));
|
|
560
716
|
success(`Config file created: ${configPath}`);
|
|
561
717
|
info('Run "dockcraft generate" to generate Docker files');
|
|
562
718
|
}
|
|
563
719
|
function generateConfig(name, preset, services2, port) {
|
|
564
|
-
let content = `import {
|
|
720
|
+
let content = `import type { DockcraftConfig } from '@oxog/dockcraft';
|
|
565
721
|
|
|
566
722
|
`;
|
|
567
|
-
content += `
|
|
723
|
+
content += `export default {
|
|
568
724
|
`;
|
|
569
|
-
content += ` name:
|
|
725
|
+
content += ` name: ${JSON.stringify(name)},
|
|
570
726
|
`;
|
|
571
727
|
if (preset) {
|
|
572
|
-
content += ` preset:
|
|
728
|
+
content += ` preset: ${JSON.stringify(preset)},
|
|
573
729
|
`;
|
|
574
730
|
}
|
|
575
731
|
content += ` app: {
|
|
576
732
|
`;
|
|
577
|
-
content += ` port: ${port},
|
|
733
|
+
content += ` port: ${JSON.stringify(port)},
|
|
578
734
|
`;
|
|
579
735
|
content += ` },
|
|
580
736
|
`;
|
|
@@ -582,21 +738,35 @@ function generateConfig(name, preset, services2, port) {
|
|
|
582
738
|
content += ` services: {
|
|
583
739
|
`;
|
|
584
740
|
for (const service of services2) {
|
|
585
|
-
content += ` ${service}: {},
|
|
741
|
+
content += ` ${JSON.stringify(service)}: {},
|
|
586
742
|
`;
|
|
587
743
|
}
|
|
588
744
|
content += ` },
|
|
589
745
|
`;
|
|
590
746
|
}
|
|
591
|
-
content += `}
|
|
747
|
+
content += `} satisfies DockcraftConfig;
|
|
592
748
|
`;
|
|
593
749
|
return content;
|
|
594
750
|
}
|
|
595
751
|
|
|
752
|
+
// src/services/image.ts
|
|
753
|
+
function variantImage(repository, version, variant) {
|
|
754
|
+
if (version === "latest") {
|
|
755
|
+
return `${repository}:${variant}`;
|
|
756
|
+
}
|
|
757
|
+
if (version === variant || version.includes("-")) {
|
|
758
|
+
return `${repository}:${version}`;
|
|
759
|
+
}
|
|
760
|
+
return `${repository}:${version}-${variant}`;
|
|
761
|
+
}
|
|
762
|
+
function urlPart(value) {
|
|
763
|
+
return encodeURIComponent(value);
|
|
764
|
+
}
|
|
765
|
+
|
|
596
766
|
// src/services/postgres.ts
|
|
597
767
|
var postgresService = {
|
|
598
768
|
name: "postgres",
|
|
599
|
-
image: (version) =>
|
|
769
|
+
image: (version) => variantImage("postgres", version, "alpine"),
|
|
600
770
|
defaultPort: 5432,
|
|
601
771
|
defaultVersion: "16",
|
|
602
772
|
env: {
|
|
@@ -613,7 +783,7 @@ var postgresService = {
|
|
|
613
783
|
const database = config.env.POSTGRES_DB || "myapp";
|
|
614
784
|
const host = config.name;
|
|
615
785
|
const port = config.port;
|
|
616
|
-
return `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
786
|
+
return `postgres://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
617
787
|
}
|
|
618
788
|
};
|
|
619
789
|
|
|
@@ -638,7 +808,7 @@ var mysqlService = {
|
|
|
638
808
|
const database = config.env.MYSQL_DATABASE || "myapp";
|
|
639
809
|
const host = config.name;
|
|
640
810
|
const port = config.port;
|
|
641
|
-
return `mysql://${user}:${password}@${host}:${port}/${database}`;
|
|
811
|
+
return `mysql://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
642
812
|
}
|
|
643
813
|
};
|
|
644
814
|
|
|
@@ -662,14 +832,14 @@ var mongoService = {
|
|
|
662
832
|
const database = config.env.MONGO_INITDB_DATABASE || "myapp";
|
|
663
833
|
const host = config.name;
|
|
664
834
|
const port = config.port;
|
|
665
|
-
return `mongodb://${user}:${password}@${host}:${port}/${database}`;
|
|
835
|
+
return `mongodb://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}?authSource=admin`;
|
|
666
836
|
}
|
|
667
837
|
};
|
|
668
838
|
|
|
669
839
|
// src/services/redis.ts
|
|
670
840
|
var redisService = {
|
|
671
841
|
name: "redis",
|
|
672
|
-
image: (version) =>
|
|
842
|
+
image: (version) => variantImage("redis", version, "alpine"),
|
|
673
843
|
defaultPort: 6379,
|
|
674
844
|
defaultVersion: "7",
|
|
675
845
|
env: {},
|
|
@@ -679,7 +849,9 @@ var redisService = {
|
|
|
679
849
|
connectionString: (config) => {
|
|
680
850
|
const host = config.name;
|
|
681
851
|
const port = config.port;
|
|
682
|
-
|
|
852
|
+
const password = config.env.REDIS_PASSWORD;
|
|
853
|
+
const auth = password ? `:${urlPart(password)}@` : "";
|
|
854
|
+
return `redis://${auth}${host}:${port}`;
|
|
683
855
|
}
|
|
684
856
|
};
|
|
685
857
|
|
|
@@ -697,12 +869,13 @@ registerService(mongoService);
|
|
|
697
869
|
registerService(redisService);
|
|
698
870
|
|
|
699
871
|
// src/utils/merge.ts
|
|
872
|
+
var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
700
873
|
function deepMerge(...objects) {
|
|
701
874
|
const result = {};
|
|
702
875
|
for (const obj of objects) {
|
|
703
876
|
if (obj === null || typeof obj !== "object") continue;
|
|
704
877
|
for (const [key, value] of Object.entries(obj)) {
|
|
705
|
-
if (value === void 0) continue;
|
|
878
|
+
if (value === void 0 || UNSAFE_KEYS.has(key)) continue;
|
|
706
879
|
if (value && typeof value === "object" && !Array.isArray(value) && result[key] && typeof result[key] === "object" && !Array.isArray(result[key])) {
|
|
707
880
|
result[key] = deepMerge(result[key], value);
|
|
708
881
|
} else {
|
|
@@ -716,21 +889,30 @@ function deepMerge(...objects) {
|
|
|
716
889
|
// src/ports/scanner.ts
|
|
717
890
|
import { createServer } from "net";
|
|
718
891
|
async function isPortAvailable(port) {
|
|
719
|
-
|
|
892
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
893
|
+
throw new RangeError(`Invalid port: ${String(port)}. Must be an integer between 1 and 65535.`);
|
|
894
|
+
}
|
|
895
|
+
return new Promise((resolve2) => {
|
|
720
896
|
const server = createServer();
|
|
897
|
+
let settled = false;
|
|
898
|
+
const finish = (available) => {
|
|
899
|
+
if (settled) return;
|
|
900
|
+
settled = true;
|
|
901
|
+
clearTimeout(timer);
|
|
902
|
+
resolve2(available);
|
|
903
|
+
};
|
|
904
|
+
const timer = setTimeout(() => {
|
|
905
|
+
server.close();
|
|
906
|
+
finish(false);
|
|
907
|
+
}, 1e3);
|
|
908
|
+
timer.unref();
|
|
721
909
|
server.once("error", () => {
|
|
722
|
-
|
|
910
|
+
finish(false);
|
|
723
911
|
});
|
|
724
912
|
server.once("listening", () => {
|
|
725
|
-
server.close(() =>
|
|
726
|
-
resolve(true);
|
|
727
|
-
});
|
|
913
|
+
server.close(() => finish(true));
|
|
728
914
|
});
|
|
729
915
|
server.listen(port, "127.0.0.1");
|
|
730
|
-
setTimeout(() => {
|
|
731
|
-
server.close();
|
|
732
|
-
resolve(false);
|
|
733
|
-
}, 1e3);
|
|
734
916
|
});
|
|
735
917
|
}
|
|
736
918
|
async function checkPorts(ports2) {
|
|
@@ -769,28 +951,32 @@ async function resolvePort(options = {}) {
|
|
|
769
951
|
if (await isPortAvailable(preferred)) {
|
|
770
952
|
return preferred;
|
|
771
953
|
}
|
|
772
|
-
throw new
|
|
954
|
+
throw new PortConflictError(preferred);
|
|
773
955
|
case "range":
|
|
774
956
|
if (range) {
|
|
775
957
|
const [start, end = start + 100] = range;
|
|
776
|
-
const
|
|
777
|
-
if (
|
|
778
|
-
return
|
|
958
|
+
const port = await findAvailablePortInRange(start, end);
|
|
959
|
+
if (port !== void 0) {
|
|
960
|
+
return port;
|
|
779
961
|
}
|
|
780
|
-
throw new
|
|
962
|
+
throw new NoPortAvailableError([start, end]);
|
|
781
963
|
}
|
|
782
964
|
// Fall through to auto if no range specified
|
|
783
965
|
case "auto":
|
|
784
|
-
default:
|
|
966
|
+
default: {
|
|
785
967
|
const port = await findNextAvailablePort(preferred);
|
|
786
968
|
if (port !== void 0) {
|
|
787
969
|
return port;
|
|
788
970
|
}
|
|
789
|
-
throw new
|
|
971
|
+
throw new NoPortAvailableError([preferred, 65535]);
|
|
972
|
+
}
|
|
790
973
|
}
|
|
791
974
|
}
|
|
792
975
|
|
|
793
976
|
// src/core/config.ts
|
|
977
|
+
import { existsSync } from "fs";
|
|
978
|
+
import { isAbsolute, resolve } from "path";
|
|
979
|
+
import { pathToFileURL } from "url";
|
|
794
980
|
var DEFAULT_APP_CONFIG = {
|
|
795
981
|
port: 3e3,
|
|
796
982
|
node: "20-alpine",
|
|
@@ -801,9 +987,9 @@ var DEFAULT_APP_CONFIG = {
|
|
|
801
987
|
volumes: [],
|
|
802
988
|
multiStage: true
|
|
803
989
|
};
|
|
804
|
-
function normalizeAppConfig(config) {
|
|
990
|
+
function normalizeAppConfig(config = {}) {
|
|
805
991
|
return {
|
|
806
|
-
port: config.port === "
|
|
992
|
+
port: typeof config.port === "number" ? config.port : DEFAULT_APP_CONFIG.port,
|
|
807
993
|
node: config.node ?? DEFAULT_APP_CONFIG.node,
|
|
808
994
|
workdir: config.workdir ?? DEFAULT_APP_CONFIG.workdir,
|
|
809
995
|
command: config.command ?? DEFAULT_APP_CONFIG.command,
|
|
@@ -825,29 +1011,57 @@ function normalizeConfig(config) {
|
|
|
825
1011
|
return {
|
|
826
1012
|
name: config.name,
|
|
827
1013
|
preset: config.preset,
|
|
828
|
-
app: config.app,
|
|
1014
|
+
app: config.app ?? {},
|
|
829
1015
|
services: config.services ?? {},
|
|
830
1016
|
output: normalizeOutputConfig(config.output),
|
|
831
1017
|
plugins: config.plugins ?? []
|
|
832
1018
|
};
|
|
833
1019
|
}
|
|
834
|
-
|
|
1020
|
+
var DEFAULT_CONFIG_FILES = [
|
|
1021
|
+
"dockcraft.config.ts",
|
|
1022
|
+
"dockcraft.config.mts",
|
|
1023
|
+
"dockcraft.config.js",
|
|
1024
|
+
"dockcraft.config.mjs",
|
|
1025
|
+
"dockcraft.config.cjs"
|
|
1026
|
+
];
|
|
1027
|
+
function findConfigFile(cwd = process.cwd()) {
|
|
1028
|
+
for (const name of DEFAULT_CONFIG_FILES) {
|
|
1029
|
+
const candidate = resolve(cwd, name);
|
|
1030
|
+
if (existsSync(candidate)) return candidate;
|
|
1031
|
+
}
|
|
1032
|
+
return void 0;
|
|
1033
|
+
}
|
|
1034
|
+
async function loadConfig(configPath, cwd = process.cwd()) {
|
|
1035
|
+
const url = configPath instanceof URL ? configPath : pathToFileURL(isAbsolute(configPath) ? configPath : resolve(cwd, configPath));
|
|
1036
|
+
let module;
|
|
835
1037
|
try {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1038
|
+
module = await import(url.href);
|
|
1039
|
+
} catch (error2) {
|
|
1040
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1041
|
+
const code = error2.code;
|
|
1042
|
+
let hint = "";
|
|
1043
|
+
if (code === "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
1044
|
+
hint = " (this Node.js version cannot load TypeScript files; use dockcraft.config.mjs or Node.js >= 22.18)";
|
|
1045
|
+
} else if (/Cannot use import statement outside a module|Unexpected token 'export'/.test(message)) {
|
|
1046
|
+
hint = " (the file uses ES module syntax in a CommonJS package; rename it to .mts / .mjs)";
|
|
1047
|
+
}
|
|
1048
|
+
throw new Error(`Failed to load config from ${String(configPath)}: ${message}${hint}`);
|
|
1049
|
+
}
|
|
1050
|
+
const config = module.default;
|
|
1051
|
+
if (!config || typeof config !== "object") {
|
|
840
1052
|
throw new Error(
|
|
841
|
-
`
|
|
1053
|
+
`Config file ${String(configPath)} must export the configuration as its default export, e.g. \`export default defineConfig({ name: "my-app" })\``
|
|
842
1054
|
);
|
|
843
1055
|
}
|
|
1056
|
+
return config;
|
|
844
1057
|
}
|
|
845
1058
|
|
|
846
1059
|
// src/core/resolver.ts
|
|
1060
|
+
var REDIS_AUTH_COMMAND = `sh -c 'exec redis-server --requirepass "$$REDIS_PASSWORD"'`;
|
|
847
1061
|
function mergePreset(config, presetName) {
|
|
848
1062
|
const preset = getPreset(presetName);
|
|
849
1063
|
if (!preset) {
|
|
850
|
-
throw new
|
|
1064
|
+
throw new PresetNotFoundError(presetName);
|
|
851
1065
|
}
|
|
852
1066
|
const merged = deepMerge(
|
|
853
1067
|
{
|
|
@@ -860,55 +1074,65 @@ function mergePreset(config, presetName) {
|
|
|
860
1074
|
);
|
|
861
1075
|
return merged;
|
|
862
1076
|
}
|
|
863
|
-
async function
|
|
1077
|
+
async function resolveServicePorts(portConfig, containerPort) {
|
|
1078
|
+
if (portConfig === "auto") {
|
|
1079
|
+
return {
|
|
1080
|
+
port: containerPort,
|
|
1081
|
+
hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
if (typeof portConfig === "number") {
|
|
1085
|
+
return { port: containerPort, hostPort: portConfig };
|
|
1086
|
+
}
|
|
1087
|
+
if (portConfig && typeof portConfig === "object") {
|
|
1088
|
+
const startPort = portConfig.start ?? containerPort;
|
|
1089
|
+
const endPort = portConfig.end ?? startPort + 100;
|
|
1090
|
+
return {
|
|
1091
|
+
port: containerPort,
|
|
1092
|
+
hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
return { port: containerPort };
|
|
1096
|
+
}
|
|
1097
|
+
async function resolveServiceConfig(name, config, projectName, definitions) {
|
|
864
1098
|
if ("image" in config && config.image) {
|
|
865
1099
|
return resolveCustomService(name, config);
|
|
866
1100
|
}
|
|
867
|
-
const definition = getService(name);
|
|
1101
|
+
const definition = definitions?.get(name) ?? getService(name);
|
|
868
1102
|
if (!definition) {
|
|
869
|
-
throw new
|
|
870
|
-
}
|
|
871
|
-
const defaultPort = definition.defaultPort;
|
|
872
|
-
let port = defaultPort;
|
|
873
|
-
let hostPort;
|
|
874
|
-
if (config.port === "auto") {
|
|
875
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
876
|
-
hostPort = port;
|
|
877
|
-
} else if (typeof config.port === "number") {
|
|
878
|
-
port = config.port;
|
|
879
|
-
hostPort = config.port;
|
|
880
|
-
} else if (config.port && typeof config.port === "object") {
|
|
881
|
-
const { start, end } = config.port;
|
|
882
|
-
const startPort = start ?? defaultPort;
|
|
883
|
-
const endPort = end ?? startPort + 100;
|
|
884
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
885
|
-
hostPort = port;
|
|
1103
|
+
throw new ServiceNotFoundError(name);
|
|
886
1104
|
}
|
|
1105
|
+
const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
|
|
887
1106
|
const version = config.version ?? definition.defaultVersion;
|
|
888
1107
|
const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
|
|
889
1108
|
const env = { ...definition.env, ...config.env };
|
|
1109
|
+
let command = config.command;
|
|
890
1110
|
if (name === "postgres") {
|
|
891
1111
|
const pgConfig = config;
|
|
892
1112
|
if (pgConfig.database) env.POSTGRES_DB = pgConfig.database;
|
|
893
1113
|
if (pgConfig.user) env.POSTGRES_USER = pgConfig.user;
|
|
894
1114
|
if (pgConfig.password) env.POSTGRES_PASSWORD = pgConfig.password;
|
|
895
|
-
if (!env
|
|
1115
|
+
if (!pgConfig.database && !config.env?.POSTGRES_DB) env.POSTGRES_DB = projectName;
|
|
896
1116
|
} else if (name === "mysql") {
|
|
897
1117
|
const mysqlConfig = config;
|
|
898
1118
|
if (mysqlConfig.database) env.MYSQL_DATABASE = mysqlConfig.database;
|
|
899
1119
|
if (mysqlConfig.user) env.MYSQL_USER = mysqlConfig.user;
|
|
900
1120
|
if (mysqlConfig.password) env.MYSQL_PASSWORD = mysqlConfig.password;
|
|
901
1121
|
if (mysqlConfig.rootPassword) env.MYSQL_ROOT_PASSWORD = mysqlConfig.rootPassword;
|
|
902
|
-
if (!env
|
|
1122
|
+
if (!mysqlConfig.database && !config.env?.MYSQL_DATABASE) env.MYSQL_DATABASE = projectName;
|
|
903
1123
|
} else if (name === "mongo") {
|
|
904
1124
|
const mongoConfig = config;
|
|
905
1125
|
if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
|
|
906
1126
|
if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
|
|
907
1127
|
if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
|
|
908
|
-
if (!env
|
|
1128
|
+
if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
|
|
909
1129
|
} else if (name === "redis") {
|
|
910
1130
|
const redisConfig = config;
|
|
911
1131
|
if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
|
|
1132
|
+
if (env.REDIS_PASSWORD) {
|
|
1133
|
+
env.REDISCLI_AUTH = env.REDIS_PASSWORD;
|
|
1134
|
+
command ??= REDIS_AUTH_COMMAND;
|
|
1135
|
+
}
|
|
912
1136
|
}
|
|
913
1137
|
const volumes = [...definition.volumes ?? [], ...config.volumes ?? []];
|
|
914
1138
|
const resolvedConfig = {
|
|
@@ -918,7 +1142,7 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
918
1142
|
hostPort,
|
|
919
1143
|
env,
|
|
920
1144
|
volumes,
|
|
921
|
-
command
|
|
1145
|
+
command,
|
|
922
1146
|
healthcheck: definition.healthcheck,
|
|
923
1147
|
ports: definition.ports
|
|
924
1148
|
};
|
|
@@ -928,53 +1152,53 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
928
1152
|
return resolvedConfig;
|
|
929
1153
|
}
|
|
930
1154
|
async function resolveCustomService(name, config) {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
937
|
-
hostPort = port;
|
|
938
|
-
} else if (typeof config.port === "number") {
|
|
939
|
-
port = config.port;
|
|
940
|
-
hostPort = config.port;
|
|
941
|
-
} else if (config.port && typeof config.port === "object") {
|
|
942
|
-
const { start, end } = config.port;
|
|
943
|
-
const startPort = start ?? defaultPort;
|
|
944
|
-
const endPort = end ?? startPort + 100;
|
|
945
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
946
|
-
hostPort = port;
|
|
1155
|
+
let containerPort = 80;
|
|
1156
|
+
if (typeof config.port === "number") {
|
|
1157
|
+
containerPort = config.port;
|
|
1158
|
+
} else if (config.port && typeof config.port === "object" && config.port.start) {
|
|
1159
|
+
containerPort = config.port.start;
|
|
947
1160
|
}
|
|
1161
|
+
const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
|
|
948
1162
|
return {
|
|
949
1163
|
name,
|
|
950
1164
|
image: config.image,
|
|
951
1165
|
port,
|
|
952
1166
|
hostPort,
|
|
953
|
-
env: config.env
|
|
954
|
-
volumes: config.volumes ?? [],
|
|
1167
|
+
env: { ...config.env },
|
|
1168
|
+
volumes: [...config.volumes ?? []],
|
|
955
1169
|
command: config.command,
|
|
956
|
-
healthcheck: config.
|
|
1170
|
+
healthcheck: config.healthcheck,
|
|
957
1171
|
ports: []
|
|
958
1172
|
};
|
|
959
1173
|
}
|
|
1174
|
+
function pluginServiceDefinitions(plugins) {
|
|
1175
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
1176
|
+
for (const plugin of plugins ?? []) {
|
|
1177
|
+
for (const [name, def] of Object.entries(plugin.services ?? {})) {
|
|
1178
|
+
definitions.set(name, { ...def, name });
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return definitions;
|
|
1182
|
+
}
|
|
960
1183
|
async function resolveConfig(config) {
|
|
961
1184
|
let finalConfig = config;
|
|
962
1185
|
if (config.preset) {
|
|
963
1186
|
finalConfig = mergePreset(config, config.preset);
|
|
964
1187
|
}
|
|
965
1188
|
finalConfig = normalizeConfig(finalConfig);
|
|
966
|
-
const
|
|
967
|
-
const
|
|
1189
|
+
const appConfig = finalConfig.app ?? {};
|
|
1190
|
+
const appPort = appConfig.port === "auto" ? await resolvePort({ preferred: 3e3, strategy: "auto" }) : appConfig.port;
|
|
1191
|
+
const app = normalizeAppConfig({ ...appConfig, port: appPort });
|
|
1192
|
+
const definitions = pluginServiceDefinitions(finalConfig.plugins);
|
|
968
1193
|
const services2 = {};
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
}
|
|
1194
|
+
for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
|
|
1195
|
+
if (serviceConfig) {
|
|
1196
|
+
services2[name] = await resolveServiceConfig(
|
|
1197
|
+
name,
|
|
1198
|
+
serviceConfig,
|
|
1199
|
+
finalConfig.name,
|
|
1200
|
+
definitions
|
|
1201
|
+
);
|
|
978
1202
|
}
|
|
979
1203
|
}
|
|
980
1204
|
return {
|
|
@@ -985,43 +1209,32 @@ async function resolveConfig(config) {
|
|
|
985
1209
|
};
|
|
986
1210
|
}
|
|
987
1211
|
|
|
988
|
-
// src/utils/validation.ts
|
|
989
|
-
var MIN_PORT = 1;
|
|
990
|
-
var MAX_PORT = 65535;
|
|
991
|
-
function isPort(value) {
|
|
992
|
-
return typeof value === "number" && Number.isInteger(value) && value >= MIN_PORT && value <= MAX_PORT;
|
|
993
|
-
}
|
|
994
|
-
function isEnvVarName(value) {
|
|
995
|
-
if (typeof value !== "string") return false;
|
|
996
|
-
return /^[A-Z_][A-Z0-9_]*$/.test(value);
|
|
997
|
-
}
|
|
998
|
-
function isVersion(value) {
|
|
999
|
-
if (typeof value !== "string") return false;
|
|
1000
|
-
if (value.length === 0) return false;
|
|
1001
|
-
return /^[a-zA-Z0-9.\-]+$/.test(value);
|
|
1002
|
-
}
|
|
1003
|
-
function isProjectName(value) {
|
|
1004
|
-
if (typeof value !== "string") return false;
|
|
1005
|
-
if (value.length === 0 || value.length > 100) return false;
|
|
1006
|
-
return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(value);
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
1212
|
// src/core/validator.ts
|
|
1213
|
+
var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
|
|
1010
1214
|
function validateProjectName(name) {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
return
|
|
1215
|
+
if (isProjectName(name)) return [];
|
|
1216
|
+
return [{
|
|
1217
|
+
code: "INVALID_NAME",
|
|
1218
|
+
message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
|
|
1219
|
+
path: "name",
|
|
1220
|
+
value: name
|
|
1221
|
+
}];
|
|
1222
|
+
}
|
|
1223
|
+
function validateStringList(value, path) {
|
|
1224
|
+
if (value === void 0) return [];
|
|
1225
|
+
if (!Array.isArray(value)) {
|
|
1226
|
+
return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
|
|
1227
|
+
}
|
|
1228
|
+
return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
|
|
1229
|
+
code: "INVALID_VALUE",
|
|
1230
|
+
message: `Invalid entry ${JSON.stringify(item)} in '${path}'. Entries must be non-empty single-line strings.`,
|
|
1231
|
+
path,
|
|
1232
|
+
value: item
|
|
1233
|
+
}));
|
|
1021
1234
|
}
|
|
1022
1235
|
function validateAppConfig(config) {
|
|
1023
1236
|
const errors = [];
|
|
1024
|
-
if (config.port !== "auto" && !isPort(config.port)) {
|
|
1237
|
+
if (config.port !== void 0 && config.port !== "auto" && !isPort(config.port)) {
|
|
1025
1238
|
errors.push({
|
|
1026
1239
|
code: "INVALID_PORT",
|
|
1027
1240
|
message: `Invalid app port '${String(config.port)}'. Must be a number between 1-65535 or 'auto'.`,
|
|
@@ -1032,54 +1245,163 @@ function validateAppConfig(config) {
|
|
|
1032
1245
|
if (config.node !== void 0 && !isVersion(config.node)) {
|
|
1033
1246
|
errors.push({
|
|
1034
1247
|
code: "INVALID_VERSION",
|
|
1035
|
-
message: `Invalid Node.js version '${config.node}'.`,
|
|
1248
|
+
message: `Invalid Node.js version '${String(config.node)}'. Must be a valid Docker image tag.`,
|
|
1036
1249
|
path: "app.node",
|
|
1037
1250
|
value: config.node
|
|
1038
1251
|
});
|
|
1039
1252
|
}
|
|
1040
|
-
if (config.
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1253
|
+
if (config.workdir !== void 0 && !isContainerPath(config.workdir)) {
|
|
1254
|
+
errors.push({
|
|
1255
|
+
code: "INVALID_WORKDIR",
|
|
1256
|
+
message: `Invalid workdir '${String(config.workdir)}'. Must be an absolute path made of letters, numbers, '.', '_', '-' and '/'.`,
|
|
1257
|
+
path: "app.workdir",
|
|
1258
|
+
value: config.workdir
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
for (const key of ["command", "buildCommand"]) {
|
|
1262
|
+
const value = config[key];
|
|
1263
|
+
if (value !== void 0 && !isSingleLine(value)) {
|
|
1264
|
+
errors.push({
|
|
1265
|
+
code: "INVALID_COMMAND",
|
|
1266
|
+
message: `Invalid app.${key}: must be a single-line string.`,
|
|
1267
|
+
path: `app.${key}`,
|
|
1268
|
+
value
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
if (config.command !== void 0 && config.command.trim() === "") {
|
|
1273
|
+
errors.push({
|
|
1274
|
+
code: "INVALID_COMMAND",
|
|
1275
|
+
message: "Invalid app.command: must not be empty.",
|
|
1276
|
+
path: "app.command",
|
|
1277
|
+
value: config.command
|
|
1278
|
+
});
|
|
1279
|
+
}
|
|
1280
|
+
if (config.healthcheck !== void 0 && !isHealthcheckPath(config.healthcheck)) {
|
|
1281
|
+
errors.push({
|
|
1282
|
+
code: "INVALID_HEALTHCHECK",
|
|
1283
|
+
message: `Invalid health check path '${String(config.healthcheck)}'. Must start with '/' and contain only URL path characters (no quotes, spaces, '$' or backslashes).`,
|
|
1284
|
+
path: "app.healthcheck",
|
|
1285
|
+
value: config.healthcheck
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
if (config.env !== void 0) {
|
|
1289
|
+
if (!Array.isArray(config.env)) {
|
|
1290
|
+
errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
|
|
1291
|
+
} else {
|
|
1292
|
+
for (const envVar of config.env) {
|
|
1293
|
+
if (!isEnvVarName(envVar)) {
|
|
1294
|
+
errors.push({
|
|
1295
|
+
code: "INVALID_ENV_VAR",
|
|
1296
|
+
message: `Invalid environment variable name '${String(envVar)}'. Must be uppercase letters, numbers, and underscores.`,
|
|
1297
|
+
path: "app.env",
|
|
1298
|
+
value: envVar
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1049
1301
|
}
|
|
1050
1302
|
}
|
|
1051
1303
|
}
|
|
1304
|
+
errors.push(...validateStringList(config.volumes, "app.volumes"));
|
|
1052
1305
|
return errors;
|
|
1053
1306
|
}
|
|
1307
|
+
function validateServicePort(name, port) {
|
|
1308
|
+
if (port === void 0 || port === "auto" || isPort(port)) return [];
|
|
1309
|
+
if (port && typeof port === "object" && !Array.isArray(port)) {
|
|
1310
|
+
const { start, end } = port;
|
|
1311
|
+
if (isPort(start) && (end === void 0 || isPort(end) && end >= start)) {
|
|
1312
|
+
return [];
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
return [{
|
|
1316
|
+
code: "INVALID_PORT",
|
|
1317
|
+
message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
|
|
1318
|
+
path: `services.${name}.port`,
|
|
1319
|
+
value: port
|
|
1320
|
+
}];
|
|
1321
|
+
}
|
|
1054
1322
|
function validateServiceConfig(name, config) {
|
|
1055
1323
|
const errors = [];
|
|
1056
|
-
if (
|
|
1324
|
+
if (!isComposeServiceName(name)) {
|
|
1325
|
+
errors.push({
|
|
1326
|
+
code: "INVALID_SERVICE_NAME",
|
|
1327
|
+
message: `Invalid service name '${name}'. Must start with a letter or digit and contain only letters, digits, '.', '_' and '-'.`,
|
|
1328
|
+
path: `services.${name}`,
|
|
1329
|
+
value: name
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
1333
|
+
errors.push({
|
|
1334
|
+
code: "INVALID_TYPE",
|
|
1335
|
+
message: `Service '${name}' must be configured with an object.`,
|
|
1336
|
+
path: `services.${name}`,
|
|
1337
|
+
value: config
|
|
1338
|
+
});
|
|
1339
|
+
return errors;
|
|
1340
|
+
}
|
|
1341
|
+
const svc = config;
|
|
1342
|
+
if (svc.version !== void 0 && !isVersion(svc.version)) {
|
|
1057
1343
|
errors.push({
|
|
1058
1344
|
code: "INVALID_VERSION",
|
|
1059
|
-
message: `Invalid version '${
|
|
1345
|
+
message: `Invalid version '${String(svc.version)}' for service '${name}'. Must be a valid Docker image tag.`,
|
|
1060
1346
|
path: `services.${name}.version`,
|
|
1061
|
-
value:
|
|
1347
|
+
value: svc.version
|
|
1062
1348
|
});
|
|
1063
1349
|
}
|
|
1064
|
-
if (
|
|
1350
|
+
if (svc.image !== void 0 && !isDockerImage(svc.image)) {
|
|
1065
1351
|
errors.push({
|
|
1066
|
-
code: "
|
|
1067
|
-
message: `Invalid
|
|
1068
|
-
path: `services.${name}.
|
|
1069
|
-
value:
|
|
1352
|
+
code: "INVALID_IMAGE",
|
|
1353
|
+
message: `Invalid image '${String(svc.image)}' for service '${name}'. Must be a Docker image reference such as 'elasticsearch:8.11.0'.`,
|
|
1354
|
+
path: `services.${name}.image`,
|
|
1355
|
+
value: svc.image
|
|
1070
1356
|
});
|
|
1071
1357
|
}
|
|
1358
|
+
errors.push(...validateServicePort(name, svc.port));
|
|
1359
|
+
if (svc.env !== void 0) {
|
|
1360
|
+
if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
|
|
1361
|
+
errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
|
|
1362
|
+
} else {
|
|
1363
|
+
for (const [key, value] of Object.entries(svc.env)) {
|
|
1364
|
+
if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
|
|
1365
|
+
errors.push({
|
|
1366
|
+
code: "INVALID_ENV_VAR",
|
|
1367
|
+
message: `Invalid environment variable '${key}' for service '${name}'. Names must not be empty or contain '=' or whitespace, and values must be strings.`,
|
|
1368
|
+
path: `services.${name}.env.${key}`,
|
|
1369
|
+
value
|
|
1370
|
+
});
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
|
|
1376
|
+
const value = svc[key];
|
|
1377
|
+
if (value !== void 0 && typeof value !== "string") {
|
|
1378
|
+
errors.push({
|
|
1379
|
+
code: "INVALID_TYPE",
|
|
1380
|
+
message: `'services.${name}.${key}' must be a string.`,
|
|
1381
|
+
path: `services.${name}.${key}`,
|
|
1382
|
+
value
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
errors.push(...validateStringList(svc.volumes, `services.${name}.volumes`));
|
|
1072
1387
|
return errors;
|
|
1073
1388
|
}
|
|
1074
1389
|
function validateOutputConfig(config) {
|
|
1075
1390
|
const errors = [];
|
|
1076
|
-
if (config
|
|
1077
|
-
|
|
1391
|
+
if (config.dir !== void 0 && (typeof config.dir !== "string" || config.dir === "" || config.dir.includes("\0"))) {
|
|
1392
|
+
errors.push({
|
|
1393
|
+
code: "INVALID_OUTPUT_DIR",
|
|
1394
|
+
message: "output.dir must be a non-empty path.",
|
|
1395
|
+
path: "output.dir",
|
|
1396
|
+
value: config.dir
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
if (config.files) {
|
|
1078
1400
|
for (const file of config.files) {
|
|
1079
|
-
if (!
|
|
1401
|
+
if (!VALID_OUTPUT_FILES.includes(file)) {
|
|
1080
1402
|
errors.push({
|
|
1081
1403
|
code: "INVALID_OUTPUT_FILE",
|
|
1082
|
-
message: `Invalid output file '${file}'. Must be one of: ${
|
|
1404
|
+
message: `Invalid output file '${String(file)}'. Must be one of: ${VALID_OUTPUT_FILES.join(", ")}.`,
|
|
1083
1405
|
path: "output.files",
|
|
1084
1406
|
value: file
|
|
1085
1407
|
});
|
|
@@ -1090,13 +1412,19 @@ function validateOutputConfig(config) {
|
|
|
1090
1412
|
}
|
|
1091
1413
|
function validateConfig(config) {
|
|
1092
1414
|
const errors = [];
|
|
1415
|
+
if (typeof config !== "object" || config === null) {
|
|
1416
|
+
return {
|
|
1417
|
+
valid: false,
|
|
1418
|
+
errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1093
1421
|
errors.push(...validateProjectName(config.name));
|
|
1094
1422
|
if (config.app) {
|
|
1095
1423
|
errors.push(...validateAppConfig(config.app));
|
|
1096
1424
|
}
|
|
1097
1425
|
if (config.services) {
|
|
1098
1426
|
for (const [name, serviceConfig] of Object.entries(config.services)) {
|
|
1099
|
-
if (serviceConfig) {
|
|
1427
|
+
if (serviceConfig !== void 0) {
|
|
1100
1428
|
errors.push(...validateServiceConfig(name, serviceConfig));
|
|
1101
1429
|
}
|
|
1102
1430
|
}
|
|
@@ -1111,18 +1439,23 @@ function validateConfig(config) {
|
|
|
1111
1439
|
}
|
|
1112
1440
|
function validateResolvedConfig(config) {
|
|
1113
1441
|
const errors = [];
|
|
1114
|
-
const usedPorts = /* @__PURE__ */ new
|
|
1115
|
-
usedPorts.
|
|
1442
|
+
const usedPorts = /* @__PURE__ */ new Map();
|
|
1443
|
+
usedPorts.set(config.app.port, "app");
|
|
1116
1444
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1445
|
+
const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
|
|
1446
|
+
for (const port of published) {
|
|
1447
|
+
const owner = usedPorts.get(port);
|
|
1448
|
+
if (owner !== void 0) {
|
|
1449
|
+
errors.push({
|
|
1450
|
+
code: "PORT_CONFLICT",
|
|
1451
|
+
message: `Host port ${port} of service '${serviceName}' is already published by '${owner}'.`,
|
|
1452
|
+
path: `services.${serviceName}.port`,
|
|
1453
|
+
value: port
|
|
1454
|
+
});
|
|
1455
|
+
} else {
|
|
1456
|
+
usedPorts.set(port, serviceName);
|
|
1457
|
+
}
|
|
1124
1458
|
}
|
|
1125
|
-
usedPorts.add(serviceConfig.port);
|
|
1126
1459
|
}
|
|
1127
1460
|
return {
|
|
1128
1461
|
valid: errors.length === 0,
|
|
@@ -1130,11 +1463,24 @@ function validateResolvedConfig(config) {
|
|
|
1130
1463
|
};
|
|
1131
1464
|
}
|
|
1132
1465
|
|
|
1466
|
+
// src/generators/healthcheck.ts
|
|
1467
|
+
function nodeHealthcheckScript(port, path) {
|
|
1468
|
+
return `require('http').get('http://localhost:${port}${path}', (r) => process.exit(r.statusCode >= 200 && r.statusCode < 300 ? 0 : 1)).on('error', () => process.exit(1)).setTimeout(2000, () => process.exit(1))`;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1133
1471
|
// src/generators/dockerfile.ts
|
|
1472
|
+
function dockerCmd(command) {
|
|
1473
|
+
const words = command.trim().split(/\s+/);
|
|
1474
|
+
if (words.every((word) => /^[\w@%+=:,./-]+$/.test(word))) {
|
|
1475
|
+
return `CMD ${JSON.stringify(words).replace(/","/g, '", "')}`;
|
|
1476
|
+
}
|
|
1477
|
+
return `CMD ${command.trim()}`;
|
|
1478
|
+
}
|
|
1134
1479
|
function generateDockerfile(config, _context) {
|
|
1135
|
-
const { app
|
|
1480
|
+
const { app } = config;
|
|
1136
1481
|
const { node, workdir, command, buildCommand, multiStage, healthcheck } = app;
|
|
1137
1482
|
const port = app.port;
|
|
1483
|
+
const hasBuild = buildCommand.trim() !== "";
|
|
1138
1484
|
let dockerfile = "";
|
|
1139
1485
|
if (multiStage) {
|
|
1140
1486
|
dockerfile += `# Builder stage
|
|
@@ -1160,11 +1506,13 @@ function generateDockerfile(config, _context) {
|
|
|
1160
1506
|
dockerfile += `COPY . .
|
|
1161
1507
|
|
|
1162
1508
|
`;
|
|
1163
|
-
|
|
1509
|
+
if (hasBuild) {
|
|
1510
|
+
dockerfile += `# Build application
|
|
1164
1511
|
`;
|
|
1165
|
-
|
|
1512
|
+
dockerfile += `RUN ${buildCommand.trim()}
|
|
1166
1513
|
|
|
1167
1514
|
`;
|
|
1515
|
+
}
|
|
1168
1516
|
dockerfile += `# Production stage
|
|
1169
1517
|
`;
|
|
1170
1518
|
dockerfile += `FROM node:${node}
|
|
@@ -1178,13 +1526,19 @@ function generateDockerfile(config, _context) {
|
|
|
1178
1526
|
`;
|
|
1179
1527
|
dockerfile += `# Copy from builder
|
|
1180
1528
|
`;
|
|
1181
|
-
|
|
1529
|
+
if (hasBuild) {
|
|
1530
|
+
dockerfile += `COPY --from=builder ${workdir}/node_modules ./node_modules
|
|
1182
1531
|
`;
|
|
1183
|
-
|
|
1532
|
+
dockerfile += `COPY --from=builder ${workdir}/dist ./dist
|
|
1184
1533
|
`;
|
|
1185
|
-
|
|
1534
|
+
dockerfile += `COPY --from=builder ${workdir}/package*.json ./
|
|
1186
1535
|
|
|
1187
1536
|
`;
|
|
1537
|
+
} else {
|
|
1538
|
+
dockerfile += `COPY --from=builder ${workdir} ./
|
|
1539
|
+
|
|
1540
|
+
`;
|
|
1541
|
+
}
|
|
1188
1542
|
} else {
|
|
1189
1543
|
dockerfile += `FROM node:${node}
|
|
1190
1544
|
|
|
@@ -1215,180 +1569,232 @@ function generateDockerfile(config, _context) {
|
|
|
1215
1569
|
|
|
1216
1570
|
`;
|
|
1217
1571
|
if (healthcheck) {
|
|
1572
|
+
const script = JSON.stringify(nodeHealthcheckScript(port, healthcheck));
|
|
1218
1573
|
dockerfile += `HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \\
|
|
1219
|
-
|
|
1574
|
+
`;
|
|
1575
|
+
dockerfile += ` CMD ["node", "-e", ${script}]
|
|
1220
1576
|
|
|
1221
1577
|
`;
|
|
1222
1578
|
}
|
|
1223
|
-
dockerfile +=
|
|
1579
|
+
dockerfile += `${dockerCmd(command)}
|
|
1224
1580
|
`;
|
|
1225
1581
|
return dockerfile;
|
|
1226
1582
|
}
|
|
1227
1583
|
|
|
1228
1584
|
// src/utils/yaml.ts
|
|
1585
|
+
var PLAIN_SCALAR = /^[A-Za-z_/.][\w./=@+~-]*$/;
|
|
1586
|
+
var NUMBER_LIKE = /^\.(?:\d|inf$|nan$)/i;
|
|
1587
|
+
var DURATION = /^\d+(?:\.\d+)?(?:ns|us|ms|s|m|h)$/;
|
|
1588
|
+
var RESERVED_WORD = /^(?:y|n|yes|no|on|off|true|false|null)$/i;
|
|
1229
1589
|
function toYaml(obj, indent = 0) {
|
|
1230
|
-
|
|
1231
|
-
|
|
1590
|
+
if (isInline(obj)) {
|
|
1591
|
+
return inlineValue(obj);
|
|
1592
|
+
}
|
|
1593
|
+
if (Array.isArray(obj)) {
|
|
1594
|
+
return arrayToYaml(obj, indent);
|
|
1595
|
+
}
|
|
1596
|
+
return objectToYaml(obj, indent);
|
|
1597
|
+
}
|
|
1598
|
+
function isInline(value) {
|
|
1599
|
+
if (value === null || typeof value !== "object") {
|
|
1600
|
+
return true;
|
|
1601
|
+
}
|
|
1602
|
+
if (Array.isArray(value)) {
|
|
1603
|
+
return value.every((item) => item === null || item === void 0 || typeof item !== "object");
|
|
1604
|
+
}
|
|
1605
|
+
return Object.values(value).every((item) => item === void 0);
|
|
1606
|
+
}
|
|
1607
|
+
function inlineValue(value) {
|
|
1608
|
+
if (value === null || value === void 0) {
|
|
1232
1609
|
return "null";
|
|
1233
1610
|
}
|
|
1234
|
-
if (typeof
|
|
1235
|
-
return
|
|
1611
|
+
if (typeof value === "string") {
|
|
1612
|
+
return yamlScalar(value);
|
|
1236
1613
|
}
|
|
1237
|
-
if (typeof
|
|
1238
|
-
return String(
|
|
1614
|
+
if (typeof value === "number") {
|
|
1615
|
+
if (Number.isFinite(value)) return String(value);
|
|
1616
|
+
if (Number.isNaN(value)) return ".nan";
|
|
1617
|
+
return value > 0 ? ".inf" : "-.inf";
|
|
1239
1618
|
}
|
|
1240
|
-
if (
|
|
1241
|
-
return
|
|
1619
|
+
if (typeof value === "boolean") {
|
|
1620
|
+
return String(value);
|
|
1242
1621
|
}
|
|
1243
|
-
if (
|
|
1244
|
-
return
|
|
1622
|
+
if (Array.isArray(value)) {
|
|
1623
|
+
return `[${value.map((item) => inlineValue(item)).join(", ")}]`;
|
|
1245
1624
|
}
|
|
1246
|
-
|
|
1625
|
+
if (typeof value === "object") {
|
|
1626
|
+
return "{}";
|
|
1627
|
+
}
|
|
1628
|
+
return JSON.stringify(String(value));
|
|
1247
1629
|
}
|
|
1248
1630
|
function arrayToYaml(arr, indent) {
|
|
1249
1631
|
const spaces = " ".repeat(indent);
|
|
1250
|
-
const innerSpaces = " ".repeat(indent + 2);
|
|
1251
|
-
if (arr.length === 0) {
|
|
1252
|
-
return "[]";
|
|
1253
|
-
}
|
|
1254
|
-
if (arr.every(
|
|
1255
|
-
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null
|
|
1256
|
-
)) {
|
|
1257
|
-
const items = arr.map((item) => toYaml(item, 0)).join(", ");
|
|
1258
|
-
return `[${items}]`;
|
|
1259
|
-
}
|
|
1260
1632
|
const lines = [];
|
|
1261
1633
|
for (const item of arr) {
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
lines.push(`${spaces}- ${value.slice(indent + 2)}`);
|
|
1634
|
+
if (isInline(item)) {
|
|
1635
|
+
lines.push(`${spaces}- ${inlineValue(item)}`);
|
|
1265
1636
|
} else {
|
|
1266
|
-
|
|
1637
|
+
const block = toYaml(item, indent + 2);
|
|
1638
|
+
lines.push(`${spaces}- ${block.slice(indent + 2)}`);
|
|
1267
1639
|
}
|
|
1268
1640
|
}
|
|
1269
1641
|
return lines.join("\n");
|
|
1270
1642
|
}
|
|
1271
1643
|
function objectToYaml(obj, indent) {
|
|
1272
|
-
const keys = Object.keys(obj);
|
|
1273
|
-
if (keys.length === 0) {
|
|
1274
|
-
return "{}";
|
|
1275
|
-
}
|
|
1276
1644
|
const spaces = " ".repeat(indent);
|
|
1277
1645
|
const lines = [];
|
|
1278
|
-
for (const key of
|
|
1279
|
-
|
|
1280
|
-
const
|
|
1281
|
-
if (
|
|
1282
|
-
lines.push(`${spaces}${
|
|
1283
|
-
lines.push(valueStr);
|
|
1646
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1647
|
+
if (value === void 0) continue;
|
|
1648
|
+
const yamlKey = yamlScalar(key);
|
|
1649
|
+
if (isInline(value)) {
|
|
1650
|
+
lines.push(`${spaces}${yamlKey}: ${inlineValue(value)}`);
|
|
1284
1651
|
} else {
|
|
1285
|
-
lines.push(`${spaces}${
|
|
1652
|
+
lines.push(`${spaces}${yamlKey}:`);
|
|
1653
|
+
lines.push(toYaml(value, indent + 2));
|
|
1286
1654
|
}
|
|
1287
1655
|
}
|
|
1288
1656
|
return lines.join("\n");
|
|
1289
1657
|
}
|
|
1290
|
-
function
|
|
1291
|
-
if (str
|
|
1292
|
-
return
|
|
1293
|
-
}
|
|
1294
|
-
if (/^[\-\?:\[\]{},&*#|!<>@%'"`]/.test(str)) {
|
|
1295
|
-
return `"${escapeString(str)}"`;
|
|
1658
|
+
function yamlScalar(str) {
|
|
1659
|
+
if ((PLAIN_SCALAR.test(str) || DURATION.test(str)) && !RESERVED_WORD.test(str) && !NUMBER_LIKE.test(str)) {
|
|
1660
|
+
return str;
|
|
1296
1661
|
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1662
|
+
return JSON.stringify(str);
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
// src/generators/paths.ts
|
|
1666
|
+
import { isAbsolute as isAbsolute2, join as join3, relative as relative2 } from "path";
|
|
1667
|
+
function currentDir() {
|
|
1668
|
+
return typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : "/";
|
|
1669
|
+
}
|
|
1670
|
+
function outputSubdir(dir) {
|
|
1671
|
+
if (!dir) {
|
|
1672
|
+
return void 0;
|
|
1299
1673
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1674
|
+
const root = currentDir();
|
|
1675
|
+
const subdir = relative2(root, isAbsolute2(dir) ? dir : join3(root, dir)).replace(/\\/g, "/");
|
|
1676
|
+
if (subdir === "" || subdir === ".." || subdir.startsWith("../") || isAbsolute2(subdir)) {
|
|
1677
|
+
return void 0;
|
|
1302
1678
|
}
|
|
1303
|
-
return
|
|
1679
|
+
return subdir;
|
|
1304
1680
|
}
|
|
1305
|
-
function
|
|
1306
|
-
return
|
|
1681
|
+
function pathToRoot(subdir) {
|
|
1682
|
+
return subdir.split("/").map(() => "..").join("/");
|
|
1307
1683
|
}
|
|
1308
1684
|
|
|
1309
1685
|
// src/generators/compose.ts
|
|
1310
|
-
function
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1686
|
+
function composeLiteral(value) {
|
|
1687
|
+
return value.replace(/\$/g, "$$$$");
|
|
1688
|
+
}
|
|
1689
|
+
function composeBuild(outputDir) {
|
|
1690
|
+
const subdir = outputSubdir(outputDir);
|
|
1691
|
+
if (subdir === void 0) {
|
|
1692
|
+
return ".";
|
|
1693
|
+
}
|
|
1694
|
+
return { context: pathToRoot(subdir), dockerfile: `${subdir}/Dockerfile` };
|
|
1695
|
+
}
|
|
1696
|
+
function generateCompose(config, _context) {
|
|
1697
|
+
const { app, services: services2 } = config;
|
|
1698
|
+
const appService = {
|
|
1699
|
+
build: composeBuild(config.output.dir),
|
|
1700
|
+
ports: [`${app.port}:${app.port}`],
|
|
1701
|
+
environment: generateAppEnvironment(config),
|
|
1702
|
+
depends_on: Object.keys(services2),
|
|
1703
|
+
volumes: app.volumes.length > 0 ? app.volumes : void 0
|
|
1324
1704
|
};
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
test: [`CMD`, `curl`, `-f`, `http://localhost:${app.port}${app.healthcheck}`],
|
|
1705
|
+
if (app.healthcheck) {
|
|
1706
|
+
appService.healthcheck = {
|
|
1707
|
+
test: ["CMD", "node", "-e", nodeHealthcheckScript(app.port, app.healthcheck)],
|
|
1329
1708
|
interval: "30s",
|
|
1330
1709
|
timeout: "3s",
|
|
1331
1710
|
retries: 3
|
|
1332
1711
|
};
|
|
1333
1712
|
}
|
|
1713
|
+
const composeServices = { app: appService };
|
|
1334
1714
|
for (const [serviceName, serviceConfig] of Object.entries(services2)) {
|
|
1335
|
-
|
|
1715
|
+
composeServices[serviceName] = generateServiceConfig(serviceConfig);
|
|
1336
1716
|
}
|
|
1337
|
-
|
|
1717
|
+
const compose = {
|
|
1718
|
+
services: composeServices,
|
|
1719
|
+
volumes: generateVolumeDefinitions(config)
|
|
1720
|
+
};
|
|
1721
|
+
return `${toYaml(compose)}
|
|
1722
|
+
`;
|
|
1338
1723
|
}
|
|
1339
|
-
function
|
|
1724
|
+
function connectionStringVar(serviceName) {
|
|
1725
|
+
return `${serviceName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}_URL`;
|
|
1726
|
+
}
|
|
1727
|
+
function hasDatabaseUrlAlias(config) {
|
|
1728
|
+
return config.services.postgres !== void 0 && !Object.entries(config.services).some(
|
|
1729
|
+
([name, service]) => Boolean(service.connectionString) && connectionStringVar(name) === "DATABASE_URL"
|
|
1730
|
+
);
|
|
1731
|
+
}
|
|
1732
|
+
function generateAppEnvironment(config) {
|
|
1340
1733
|
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1734
|
+
const add = (key, value) => {
|
|
1735
|
+
env.push(`${key}=${composeLiteral(String(value))}`);
|
|
1736
|
+
};
|
|
1341
1737
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1342
1738
|
if (serviceConfig.connectionString) {
|
|
1343
|
-
|
|
1344
|
-
env.push(`${key}=${serviceConfig.connectionString}`);
|
|
1739
|
+
add(connectionStringVar(serviceName), serviceConfig.connectionString);
|
|
1345
1740
|
}
|
|
1346
1741
|
}
|
|
1347
1742
|
if (config.services.postgres) {
|
|
1348
1743
|
const pg = config.services.postgres;
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1744
|
+
if (pg.connectionString && hasDatabaseUrlAlias(config)) {
|
|
1745
|
+
add("DATABASE_URL", pg.connectionString);
|
|
1746
|
+
}
|
|
1747
|
+
add("POSTGRES_HOST", pg.name);
|
|
1748
|
+
add("POSTGRES_PORT", pg.port);
|
|
1749
|
+
add("POSTGRES_USER", pg.env.POSTGRES_USER || "postgres");
|
|
1750
|
+
add("POSTGRES_PASSWORD", pg.env.POSTGRES_PASSWORD || "postgres");
|
|
1751
|
+
add("POSTGRES_DB", pg.env.POSTGRES_DB || config.name);
|
|
1354
1752
|
}
|
|
1355
1753
|
if (config.services.mysql) {
|
|
1356
1754
|
const mysql = config.services.mysql;
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1755
|
+
add("MYSQL_HOST", mysql.name);
|
|
1756
|
+
add("MYSQL_PORT", mysql.port);
|
|
1757
|
+
add("MYSQL_USER", mysql.env.MYSQL_USER || "root");
|
|
1758
|
+
add("MYSQL_PASSWORD", mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root");
|
|
1759
|
+
add("MYSQL_DATABASE", mysql.env.MYSQL_DATABASE || config.name);
|
|
1362
1760
|
}
|
|
1363
1761
|
if (config.services.mongo) {
|
|
1364
1762
|
const mongo = config.services.mongo;
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1763
|
+
add("MONGO_HOST", mongo.name);
|
|
1764
|
+
add("MONGO_PORT", mongo.port);
|
|
1765
|
+
add("MONGO_USER", mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo");
|
|
1766
|
+
add("MONGO_PASSWORD", mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo");
|
|
1767
|
+
add("MONGO_DATABASE", mongo.env.MONGO_INITDB_DATABASE || config.name);
|
|
1370
1768
|
}
|
|
1371
1769
|
if (config.services.redis) {
|
|
1372
1770
|
const redis = config.services.redis;
|
|
1373
|
-
|
|
1374
|
-
|
|
1771
|
+
add("REDIS_HOST", redis.name);
|
|
1772
|
+
add("REDIS_PORT", redis.port);
|
|
1773
|
+
if (redis.env.REDIS_PASSWORD) {
|
|
1774
|
+
add("REDIS_PASSWORD", redis.env.REDIS_PASSWORD);
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
for (const name of config.app.env) {
|
|
1778
|
+
if (!env.some((entry) => entry.startsWith(`${name}=`))) {
|
|
1779
|
+
env.push(name);
|
|
1780
|
+
}
|
|
1375
1781
|
}
|
|
1376
1782
|
return env;
|
|
1377
1783
|
}
|
|
1378
|
-
function generateServiceConfig(
|
|
1784
|
+
function generateServiceConfig(config) {
|
|
1785
|
+
const environment = {};
|
|
1786
|
+
for (const [key, value] of Object.entries(config.env)) {
|
|
1787
|
+
environment[key] = composeLiteral(value);
|
|
1788
|
+
}
|
|
1789
|
+
const portMappings = [`${config.hostPort ?? config.port}:${config.port}`];
|
|
1790
|
+
for (const extraPort of config.ports ?? []) {
|
|
1791
|
+
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1792
|
+
}
|
|
1379
1793
|
const service = {
|
|
1380
1794
|
image: config.image,
|
|
1381
|
-
ports:
|
|
1382
|
-
environment
|
|
1795
|
+
ports: portMappings,
|
|
1796
|
+
environment
|
|
1383
1797
|
};
|
|
1384
|
-
const portMappings = [];
|
|
1385
|
-
portMappings.push(`${config.hostPort ?? config.port}:${config.port}`);
|
|
1386
|
-
if (config.ports) {
|
|
1387
|
-
for (const extraPort of config.ports) {
|
|
1388
|
-
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
service.ports = portMappings;
|
|
1392
1798
|
if (config.volumes.length > 0) {
|
|
1393
1799
|
service.volumes = config.volumes;
|
|
1394
1800
|
}
|
|
@@ -1405,23 +1811,35 @@ function generateServiceConfig(name, config) {
|
|
|
1405
1811
|
}
|
|
1406
1812
|
return service;
|
|
1407
1813
|
}
|
|
1408
|
-
function generateVolumeDefinitions(
|
|
1814
|
+
function generateVolumeDefinitions(config) {
|
|
1409
1815
|
const volumes = {};
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
}
|
|
1816
|
+
const specs = [
|
|
1817
|
+
...config.app.volumes,
|
|
1818
|
+
...Object.values(config.services).flatMap((service) => service.volumes)
|
|
1819
|
+
];
|
|
1820
|
+
for (const spec of specs) {
|
|
1821
|
+
const name = namedVolume(spec);
|
|
1822
|
+
if (name) {
|
|
1823
|
+
volumes[name] = {};
|
|
1418
1824
|
}
|
|
1419
1825
|
}
|
|
1420
1826
|
return volumes;
|
|
1421
1827
|
}
|
|
1422
1828
|
|
|
1423
1829
|
// src/generators/dockerignore.ts
|
|
1424
|
-
|
|
1830
|
+
var DOCKERFILE_IGNORE_FILE = "Dockerfile.dockerignore";
|
|
1831
|
+
function dockerignoreFileName(outputDir) {
|
|
1832
|
+
return outputSubdir(outputDir) === void 0 ? ".dockerignore" : DOCKERFILE_IGNORE_FILE;
|
|
1833
|
+
}
|
|
1834
|
+
function outputDirPatterns(outputDir) {
|
|
1835
|
+
const subdir = outputSubdir(outputDir);
|
|
1836
|
+
if (subdir === void 0) {
|
|
1837
|
+
return "";
|
|
1838
|
+
}
|
|
1839
|
+
return ["Dockerfile", "docker-compose.yml", DOCKERFILE_IGNORE_FILE, ".env", ".env.*"].map((file) => `${subdir}/${file}
|
|
1840
|
+
`).join("");
|
|
1841
|
+
}
|
|
1842
|
+
function generateDockerignore(config) {
|
|
1425
1843
|
return `# Dependencies
|
|
1426
1844
|
node_modules/
|
|
1427
1845
|
npm-debug.log*
|
|
@@ -1429,10 +1847,10 @@ yarn-debug.log*
|
|
|
1429
1847
|
yarn-error.log*
|
|
1430
1848
|
pnpm-debug.log*
|
|
1431
1849
|
|
|
1432
|
-
# Environment
|
|
1850
|
+
# Environment (secrets must never be copied into the image)
|
|
1433
1851
|
.env
|
|
1434
|
-
.env
|
|
1435
|
-
.
|
|
1852
|
+
.env.*
|
|
1853
|
+
!.env.example
|
|
1436
1854
|
|
|
1437
1855
|
# Git
|
|
1438
1856
|
.git/
|
|
@@ -1483,7 +1901,7 @@ temp/
|
|
|
1483
1901
|
Dockerfile
|
|
1484
1902
|
docker-compose.yml
|
|
1485
1903
|
.dockerignore
|
|
1486
|
-
|
|
1904
|
+
${outputDirPatterns(config.output.dir)}
|
|
1487
1905
|
# Package manager lockfiles (optional - uncomment if you want to exclude)
|
|
1488
1906
|
# package-lock.json
|
|
1489
1907
|
# yarn.lock
|
|
@@ -1492,6 +1910,17 @@ docker-compose.yml
|
|
|
1492
1910
|
}
|
|
1493
1911
|
|
|
1494
1912
|
// src/generators/env.ts
|
|
1913
|
+
var BUILT_IN_SERVICES = /* @__PURE__ */ new Set(["postgres", "mysql", "mongo", "redis"]);
|
|
1914
|
+
function envValue(value) {
|
|
1915
|
+
if (/^[\w@%+=:,./?&-]*$/.test(value)) {
|
|
1916
|
+
return value;
|
|
1917
|
+
}
|
|
1918
|
+
if (!/['\r\n]/.test(value)) {
|
|
1919
|
+
return `'${value}'`;
|
|
1920
|
+
}
|
|
1921
|
+
const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
|
1922
|
+
return `"${escaped}"`;
|
|
1923
|
+
}
|
|
1495
1924
|
function generateEnvExample(config) {
|
|
1496
1925
|
let content = `# Application Configuration
|
|
1497
1926
|
NODE_ENV=production
|
|
@@ -1511,17 +1940,21 @@ PORT=${config.app.port}
|
|
|
1511
1940
|
content += `
|
|
1512
1941
|
# PostgreSQL
|
|
1513
1942
|
`;
|
|
1514
|
-
|
|
1943
|
+
if (hasDatabaseUrlAlias(config)) {
|
|
1944
|
+
content += `DATABASE_URL=${envValue(String(pg.connectionString || ""))}
|
|
1515
1945
|
`;
|
|
1516
|
-
|
|
1946
|
+
}
|
|
1947
|
+
content += `POSTGRES_URL=${envValue(String(pg.connectionString || ""))}
|
|
1517
1948
|
`;
|
|
1518
|
-
content += `
|
|
1949
|
+
content += `POSTGRES_HOST=${envValue(String(pg.name))}
|
|
1519
1950
|
`;
|
|
1520
|
-
content += `
|
|
1951
|
+
content += `POSTGRES_PORT=${envValue(String(pg.port))}
|
|
1521
1952
|
`;
|
|
1522
|
-
content += `
|
|
1953
|
+
content += `POSTGRES_USER=${envValue(String(pg.env.POSTGRES_USER || "postgres"))}
|
|
1523
1954
|
`;
|
|
1524
|
-
content += `
|
|
1955
|
+
content += `POSTGRES_PASSWORD=${envValue(String(pg.env.POSTGRES_PASSWORD || "postgres"))}
|
|
1956
|
+
`;
|
|
1957
|
+
content += `POSTGRES_DB=${envValue(String(pg.env.POSTGRES_DB || config.name))}
|
|
1525
1958
|
`;
|
|
1526
1959
|
}
|
|
1527
1960
|
if (config.services.mysql) {
|
|
@@ -1529,17 +1962,17 @@ PORT=${config.app.port}
|
|
|
1529
1962
|
content += `
|
|
1530
1963
|
# MySQL
|
|
1531
1964
|
`;
|
|
1532
|
-
content += `MYSQL_URL=${mysql.connectionString || ""}
|
|
1965
|
+
content += `MYSQL_URL=${envValue(String(mysql.connectionString || ""))}
|
|
1533
1966
|
`;
|
|
1534
|
-
content += `MYSQL_HOST=${mysql.name}
|
|
1967
|
+
content += `MYSQL_HOST=${envValue(String(mysql.name))}
|
|
1535
1968
|
`;
|
|
1536
|
-
content += `MYSQL_PORT=${mysql.port}
|
|
1969
|
+
content += `MYSQL_PORT=${envValue(String(mysql.port))}
|
|
1537
1970
|
`;
|
|
1538
|
-
content += `MYSQL_USER=${mysql.env.MYSQL_USER || "root"}
|
|
1971
|
+
content += `MYSQL_USER=${envValue(String(mysql.env.MYSQL_USER || "root"))}
|
|
1539
1972
|
`;
|
|
1540
|
-
content += `MYSQL_PASSWORD=${mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"}
|
|
1973
|
+
content += `MYSQL_PASSWORD=${envValue(String(mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"))}
|
|
1541
1974
|
`;
|
|
1542
|
-
content += `MYSQL_DATABASE=${mysql.env.MYSQL_DATABASE || config.name}
|
|
1975
|
+
content += `MYSQL_DATABASE=${envValue(String(mysql.env.MYSQL_DATABASE || config.name))}
|
|
1543
1976
|
`;
|
|
1544
1977
|
}
|
|
1545
1978
|
if (config.services.mongo) {
|
|
@@ -1547,17 +1980,17 @@ PORT=${config.app.port}
|
|
|
1547
1980
|
content += `
|
|
1548
1981
|
# MongoDB
|
|
1549
1982
|
`;
|
|
1550
|
-
content += `MONGO_URL=${mongo.connectionString || ""}
|
|
1983
|
+
content += `MONGO_URL=${envValue(String(mongo.connectionString || ""))}
|
|
1551
1984
|
`;
|
|
1552
|
-
content += `MONGO_HOST=${mongo.name}
|
|
1985
|
+
content += `MONGO_HOST=${envValue(String(mongo.name))}
|
|
1553
1986
|
`;
|
|
1554
|
-
content += `MONGO_PORT=${mongo.port}
|
|
1987
|
+
content += `MONGO_PORT=${envValue(String(mongo.port))}
|
|
1555
1988
|
`;
|
|
1556
|
-
content += `MONGO_USER=${mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"}
|
|
1989
|
+
content += `MONGO_USER=${envValue(String(mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"))}
|
|
1557
1990
|
`;
|
|
1558
|
-
content += `MONGO_PASSWORD=${mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"}
|
|
1991
|
+
content += `MONGO_PASSWORD=${envValue(String(mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"))}
|
|
1559
1992
|
`;
|
|
1560
|
-
content += `MONGO_DATABASE=${mongo.env.MONGO_INITDB_DATABASE || config.name}
|
|
1993
|
+
content += `MONGO_DATABASE=${envValue(String(mongo.env.MONGO_INITDB_DATABASE || config.name))}
|
|
1561
1994
|
`;
|
|
1562
1995
|
}
|
|
1563
1996
|
if (config.services.redis) {
|
|
@@ -1565,14 +1998,23 @@ PORT=${config.app.port}
|
|
|
1565
1998
|
content += `
|
|
1566
1999
|
# Redis
|
|
1567
2000
|
`;
|
|
1568
|
-
content += `REDIS_URL=${redis.connectionString || ""}
|
|
2001
|
+
content += `REDIS_URL=${envValue(String(redis.connectionString || ""))}
|
|
1569
2002
|
`;
|
|
1570
|
-
content += `REDIS_HOST=${redis.name}
|
|
2003
|
+
content += `REDIS_HOST=${envValue(String(redis.name))}
|
|
1571
2004
|
`;
|
|
1572
|
-
content += `REDIS_PORT=${redis.port}
|
|
2005
|
+
content += `REDIS_PORT=${envValue(String(redis.port))}
|
|
1573
2006
|
`;
|
|
1574
2007
|
if (redis.env.REDIS_PASSWORD) {
|
|
1575
|
-
content += `REDIS_PASSWORD=${redis.env.REDIS_PASSWORD}
|
|
2008
|
+
content += `REDIS_PASSWORD=${envValue(String(redis.env.REDIS_PASSWORD))}
|
|
2009
|
+
`;
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
for (const [serviceName, service] of Object.entries(config.services)) {
|
|
2013
|
+
if (!BUILT_IN_SERVICES.has(serviceName) && service.connectionString) {
|
|
2014
|
+
content += `
|
|
2015
|
+
# ${serviceName}
|
|
2016
|
+
`;
|
|
2017
|
+
content += `${connectionStringVar(serviceName)}=${envValue(service.connectionString)}
|
|
1576
2018
|
`;
|
|
1577
2019
|
}
|
|
1578
2020
|
}
|
|
@@ -1587,7 +2029,7 @@ function generateBuildScript(_config) {
|
|
|
1587
2029
|
set -e
|
|
1588
2030
|
|
|
1589
2031
|
echo "Building Docker image..."
|
|
1590
|
-
docker
|
|
2032
|
+
docker compose build
|
|
1591
2033
|
|
|
1592
2034
|
echo "Build complete!"
|
|
1593
2035
|
`;
|
|
@@ -1600,7 +2042,7 @@ function generateUpScript(config) {
|
|
|
1600
2042
|
set -e
|
|
1601
2043
|
|
|
1602
2044
|
echo "Starting services..."
|
|
1603
|
-
docker
|
|
2045
|
+
docker compose up -d ${services2.join(" ")}
|
|
1604
2046
|
|
|
1605
2047
|
echo "Services started!"
|
|
1606
2048
|
echo ""
|
|
@@ -1614,7 +2056,7 @@ function generateDownScript(_config) {
|
|
|
1614
2056
|
set -e
|
|
1615
2057
|
|
|
1616
2058
|
echo "Stopping services..."
|
|
1617
|
-
docker
|
|
2059
|
+
docker compose down
|
|
1618
2060
|
|
|
1619
2061
|
echo "Services stopped!"
|
|
1620
2062
|
`;
|
|
@@ -1626,9 +2068,9 @@ function generateLogsScript(_config) {
|
|
|
1626
2068
|
set -e
|
|
1627
2069
|
|
|
1628
2070
|
if [ -n "$1" ]; then
|
|
1629
|
-
docker
|
|
2071
|
+
docker compose logs -f "$1"
|
|
1630
2072
|
else
|
|
1631
|
-
docker
|
|
2073
|
+
docker compose logs -f
|
|
1632
2074
|
fi
|
|
1633
2075
|
`;
|
|
1634
2076
|
}
|
|
@@ -1655,28 +2097,28 @@ help:
|
|
|
1655
2097
|
@echo " make restart - Restart all services"
|
|
1656
2098
|
@echo " make logs - Show logs from all services"
|
|
1657
2099
|
@echo " make shell - Open shell in app container"
|
|
1658
|
-
@echo " make clean - Remove containers and volumes"
|
|
2100
|
+
@echo " make clean - Remove this project's containers and volumes"
|
|
1659
2101
|
@echo " make install - Install dependencies"
|
|
1660
2102
|
|
|
1661
2103
|
# Build Docker images
|
|
1662
2104
|
build:
|
|
1663
|
-
docker
|
|
2105
|
+
docker compose build
|
|
1664
2106
|
|
|
1665
2107
|
# Start all services
|
|
1666
2108
|
up:
|
|
1667
|
-
docker
|
|
2109
|
+
docker compose up -d
|
|
1668
2110
|
@echo "App running at http://localhost:${config.app.port}"
|
|
1669
2111
|
|
|
1670
2112
|
# Stop all services
|
|
1671
2113
|
down:
|
|
1672
|
-
docker
|
|
2114
|
+
docker compose down
|
|
1673
2115
|
|
|
1674
2116
|
# Restart all services
|
|
1675
2117
|
restart: down up
|
|
1676
2118
|
|
|
1677
2119
|
# Show logs from all services
|
|
1678
2120
|
logs:
|
|
1679
|
-
docker
|
|
2121
|
+
docker compose logs -f
|
|
1680
2122
|
`;
|
|
1681
2123
|
if (services2.length > 0) {
|
|
1682
2124
|
content += "\n";
|
|
@@ -1684,23 +2126,22 @@ logs:
|
|
|
1684
2126
|
content += `
|
|
1685
2127
|
logs-${service}:
|
|
1686
2128
|
`;
|
|
1687
|
-
content += ` docker
|
|
2129
|
+
content += ` docker compose logs -f ${service}
|
|
1688
2130
|
`;
|
|
1689
2131
|
}
|
|
1690
2132
|
}
|
|
1691
2133
|
content += `
|
|
1692
|
-
#
|
|
2134
|
+
# Remove this project's containers, networks and volumes
|
|
1693
2135
|
clean:
|
|
1694
|
-
docker
|
|
1695
|
-
docker system prune -f
|
|
2136
|
+
docker compose down -v --remove-orphans
|
|
1696
2137
|
|
|
1697
2138
|
# Install dependencies (in running container)
|
|
1698
2139
|
shell:
|
|
1699
|
-
docker
|
|
2140
|
+
docker compose exec app sh
|
|
1700
2141
|
|
|
1701
2142
|
# Install dependencies
|
|
1702
2143
|
install:
|
|
1703
|
-
docker
|
|
2144
|
+
docker compose exec app npm install
|
|
1704
2145
|
`;
|
|
1705
2146
|
return content;
|
|
1706
2147
|
}
|
|
@@ -1727,7 +2168,8 @@ function generateAll(config, context) {
|
|
|
1727
2168
|
} else {
|
|
1728
2169
|
const generator = GENERATORS[fileType];
|
|
1729
2170
|
if (generator) {
|
|
1730
|
-
|
|
2171
|
+
const file = fileType === "dockerignore" ? dockerignoreFileName(config.output.dir) : generator.file;
|
|
2172
|
+
result[file] = generator.generate(config, context);
|
|
1731
2173
|
}
|
|
1732
2174
|
}
|
|
1733
2175
|
}
|
|
@@ -1735,24 +2177,27 @@ function generateAll(config, context) {
|
|
|
1735
2177
|
}
|
|
1736
2178
|
|
|
1737
2179
|
// src/core/dockcraft.ts
|
|
1738
|
-
import { join as
|
|
2180
|
+
import { isAbsolute as isAbsolute3, join as join4, normalize } from "path";
|
|
2181
|
+
function validationError(prefix, errors) {
|
|
2182
|
+
const errorMessages = errors.map((e) => ` - ${e.message}`).join("\n");
|
|
2183
|
+
return new ValidationError(`${prefix}:
|
|
2184
|
+
${errorMessages}`, errors);
|
|
2185
|
+
}
|
|
2186
|
+
function isSafeOutputPath(fileName) {
|
|
2187
|
+
if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
|
|
2188
|
+
if (isAbsolute3(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
|
|
2189
|
+
const normalized = normalize(fileName.replace(/\\/g, "/"));
|
|
2190
|
+
return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
|
|
2191
|
+
}
|
|
1739
2192
|
async function dockcraft(config, options = {}) {
|
|
1740
2193
|
const validation = validateConfig(config);
|
|
1741
2194
|
if (!validation.valid) {
|
|
1742
|
-
|
|
1743
|
-
throw new Error(
|
|
1744
|
-
`Configuration validation failed:
|
|
1745
|
-
${errorMessages}`
|
|
1746
|
-
);
|
|
2195
|
+
throw validationError("Configuration validation failed", validation.errors);
|
|
1747
2196
|
}
|
|
1748
2197
|
const resolved = await resolveConfig(config);
|
|
1749
2198
|
const resolvedValidation = validateResolvedConfig(resolved);
|
|
1750
2199
|
if (!resolvedValidation.valid) {
|
|
1751
|
-
|
|
1752
|
-
throw new Error(
|
|
1753
|
-
`Resolved configuration validation failed:
|
|
1754
|
-
${errorMessages}`
|
|
1755
|
-
);
|
|
2200
|
+
throw validationError("Resolved configuration validation failed", resolvedValidation.errors);
|
|
1756
2201
|
}
|
|
1757
2202
|
const context = {
|
|
1758
2203
|
config: resolved,
|
|
@@ -1760,22 +2205,38 @@ ${errorMessages}`
|
|
|
1760
2205
|
Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
|
|
1761
2206
|
),
|
|
1762
2207
|
strings: new Map(
|
|
1763
|
-
Object.entries(resolved.services).filter((
|
|
2208
|
+
Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
|
|
1764
2209
|
)
|
|
1765
2210
|
};
|
|
1766
2211
|
context.ports.set("app", resolved.app.port);
|
|
1767
2212
|
const generated = generateAll(resolved, context);
|
|
2213
|
+
for (const plugin of config.plugins ?? []) {
|
|
2214
|
+
for (const output of plugin.outputs ?? []) {
|
|
2215
|
+
if (!isSafeOutputPath(output.file)) {
|
|
2216
|
+
throw new ConfigError(
|
|
2217
|
+
`Plugin '${plugin.name}' output '${output.name}' has an unsafe file name '${output.file}'. Use a relative path inside the output directory.`,
|
|
2218
|
+
"UNSAFE_OUTPUT_PATH"
|
|
2219
|
+
);
|
|
2220
|
+
}
|
|
2221
|
+
if (output.file in generated) {
|
|
2222
|
+
throw new ConfigError(
|
|
2223
|
+
`Plugin '${plugin.name}' output '${output.name}' would overwrite generated file '${output.file}'.`,
|
|
2224
|
+
"DUPLICATE_OUTPUT"
|
|
2225
|
+
);
|
|
2226
|
+
}
|
|
2227
|
+
generated[output.file] = output.generate(resolved, context);
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
1768
2230
|
const written = [];
|
|
1769
2231
|
const outputDir = resolved.output.dir ?? "./";
|
|
1770
2232
|
if (!options.dryRun) {
|
|
1771
2233
|
for (const [fileName, content] of Object.entries(generated)) {
|
|
1772
|
-
|
|
1773
|
-
const filePath = join2(outputDir, fileName);
|
|
2234
|
+
const filePath = join4(outputDir, fileName);
|
|
1774
2235
|
const exists = await fileExists(filePath);
|
|
1775
2236
|
if (exists && !resolved.output.overwrite) {
|
|
1776
2237
|
continue;
|
|
1777
2238
|
}
|
|
1778
|
-
await writeFile(filePath, content);
|
|
2239
|
+
await writeFile(filePath, content, fileName.endsWith(".sh") ? 493 : void 0);
|
|
1779
2240
|
written.push(filePath);
|
|
1780
2241
|
}
|
|
1781
2242
|
}
|
|
@@ -1789,41 +2250,45 @@ ${errorMessages}`
|
|
|
1789
2250
|
// src/cli/commands/generate.ts
|
|
1790
2251
|
async function generate(args) {
|
|
1791
2252
|
title("Dockcraft Generate");
|
|
1792
|
-
const configPath = getOption(args, "config"
|
|
2253
|
+
const configPath = getOption(args, "config") || findConfigFile();
|
|
1793
2254
|
const dryRun = getBooleanFlag(args, "dry-run");
|
|
1794
2255
|
const force = getBooleanFlag(args, "force");
|
|
1795
|
-
const outputDir = getOption(args, "output"
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
console.log("\u2500".repeat(50));
|
|
1814
|
-
console.log(content);
|
|
1815
|
-
console.log("\u2500".repeat(50));
|
|
1816
|
-
}
|
|
1817
|
-
} else {
|
|
1818
|
-
success(`Generated ${result.written.length} files:`);
|
|
1819
|
-
list(result.written);
|
|
2256
|
+
const outputDir = getOption(args, "output");
|
|
2257
|
+
if (!configPath) {
|
|
2258
|
+
throw new Error(
|
|
2259
|
+
`No config file found. Create one with "dockcraft init" or pass --config <file> (looked for ${DEFAULT_CONFIG_FILES.join(", ")}).`
|
|
2260
|
+
);
|
|
2261
|
+
}
|
|
2262
|
+
subtitle(`Loading config from: ${configPath}`);
|
|
2263
|
+
const config = await loadConfig(configPath);
|
|
2264
|
+
if (force) {
|
|
2265
|
+
config.output = { ...config.output, overwrite: true };
|
|
2266
|
+
}
|
|
2267
|
+
if (outputDir) {
|
|
2268
|
+
config.output = { ...config.output, dir: outputDir };
|
|
2269
|
+
}
|
|
2270
|
+
const result = await dockcraft(config, { dryRun });
|
|
2271
|
+
if (dryRun) {
|
|
2272
|
+
subtitle("Preview (dry-run mode):");
|
|
2273
|
+
for (const [fileName, content] of result.files) {
|
|
1820
2274
|
console.log("");
|
|
1821
|
-
|
|
2275
|
+
console.log(`File: ${fileName}`);
|
|
2276
|
+
console.log("\u2500".repeat(50));
|
|
2277
|
+
console.log(content);
|
|
2278
|
+
console.log("\u2500".repeat(50));
|
|
1822
2279
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
2280
|
+
return;
|
|
2281
|
+
}
|
|
2282
|
+
success(`Generated ${result.written.length} files:`);
|
|
2283
|
+
list(result.written);
|
|
2284
|
+
const skipped = result.files.size - result.written.length;
|
|
2285
|
+
if (skipped > 0) {
|
|
2286
|
+
info(`Skipped ${skipped} existing file(s); use --force to overwrite them.`);
|
|
1826
2287
|
}
|
|
2288
|
+
console.log("");
|
|
2289
|
+
const subdir = outputSubdir(result.config.output.dir);
|
|
2290
|
+
const composeFile = subdir === void 0 ? "" : ` -f ${subdir}/docker-compose.yml`;
|
|
2291
|
+
success(`Done! Run "docker compose${composeFile} up -d" to start services`);
|
|
1827
2292
|
}
|
|
1828
2293
|
|
|
1829
2294
|
// src/ports/defaults.ts
|
|
@@ -1845,12 +2310,20 @@ function listDefaultPorts() {
|
|
|
1845
2310
|
|
|
1846
2311
|
// src/cli/commands/ports.ts
|
|
1847
2312
|
async function ports(args) {
|
|
1848
|
-
title("Port Utilities");
|
|
1849
2313
|
const checkOption = getOption(args, "check");
|
|
1850
2314
|
const rangeOption = getOption(args, "range");
|
|
1851
2315
|
const asJson = getBooleanFlag(args, "json");
|
|
2316
|
+
if (!asJson) {
|
|
2317
|
+
title("Port Utilities");
|
|
2318
|
+
}
|
|
1852
2319
|
if (checkOption) {
|
|
1853
|
-
const portsToCheck = checkOption.split(",").map((s) =>
|
|
2320
|
+
const portsToCheck = checkOption.split(",").map((s) => s.trim()).filter(Boolean).map((value) => {
|
|
2321
|
+
const port = /^\d+$/.test(value) ? Number(value) : NaN;
|
|
2322
|
+
if (!isPort(port)) {
|
|
2323
|
+
throw new Error(`Invalid port '${value}'. Ports must be numbers between 1-65535.`);
|
|
2324
|
+
}
|
|
2325
|
+
return port;
|
|
2326
|
+
});
|
|
1854
2327
|
const results = await checkPorts(portsToCheck);
|
|
1855
2328
|
if (asJson) {
|
|
1856
2329
|
json(Object.fromEntries(results));
|
|
@@ -1866,21 +2339,18 @@ async function ports(args) {
|
|
|
1866
2339
|
}
|
|
1867
2340
|
if (rangeOption) {
|
|
1868
2341
|
const match = rangeOption.match(/^(\d+)-(\d+)$/);
|
|
1869
|
-
|
|
1870
|
-
|
|
2342
|
+
const start = Number(match?.[1]);
|
|
2343
|
+
const end = Number(match?.[2]);
|
|
2344
|
+
if (!match || !isPort(start) || !isPort(end) || start > end) {
|
|
2345
|
+
throw new Error("Invalid range format. Use: --range START-END with ports 1-65535 (e.g., 3000-3010)");
|
|
1871
2346
|
}
|
|
1872
|
-
const
|
|
1873
|
-
const end = parseInt(match[2], 10);
|
|
2347
|
+
const port = await findAvailablePortInRange(start, end);
|
|
1874
2348
|
if (asJson) {
|
|
1875
|
-
|
|
1876
|
-
|
|
2349
|
+
json({ available: port ?? null });
|
|
2350
|
+
} else if (port !== void 0) {
|
|
2351
|
+
success(`Found available port: ${port} in range ${start}-${end}`);
|
|
1877
2352
|
} else {
|
|
1878
|
-
|
|
1879
|
-
if (port) {
|
|
1880
|
-
success(`Found available port: ${port} in range ${start}-${end}`);
|
|
1881
|
-
} else {
|
|
1882
|
-
console.log(`No available port in range ${start}-${end}`);
|
|
1883
|
-
}
|
|
2353
|
+
console.log(`No available port in range ${start}-${end}`);
|
|
1884
2354
|
}
|
|
1885
2355
|
return;
|
|
1886
2356
|
}
|
|
@@ -1901,8 +2371,7 @@ function presets2(args) {
|
|
|
1901
2371
|
if (presetName) {
|
|
1902
2372
|
const preset = getPreset(presetName);
|
|
1903
2373
|
if (!preset) {
|
|
1904
|
-
|
|
1905
|
-
return;
|
|
2374
|
+
throw new Error(`Preset '${presetName}' not found. Run "dockcraft presets" to list available presets.`);
|
|
1906
2375
|
}
|
|
1907
2376
|
title(`Preset: ${presetName}`);
|
|
1908
2377
|
if (preset.description) {
|
|
@@ -1931,7 +2400,6 @@ function presets2(args) {
|
|
|
1931
2400
|
}
|
|
1932
2401
|
|
|
1933
2402
|
// src/cli/index.ts
|
|
1934
|
-
var VERSION = "1.0.0";
|
|
1935
2403
|
var commands = /* @__PURE__ */ new Map();
|
|
1936
2404
|
commands.set("init", init);
|
|
1937
2405
|
commands.set("generate", generate);
|
|
@@ -1979,8 +2447,9 @@ OPTIONS:
|
|
|
1979
2447
|
--name <name> Project name
|
|
1980
2448
|
--port <port> App port
|
|
1981
2449
|
--services <list> Comma-separated service list
|
|
1982
|
-
--output <dir>
|
|
1983
|
-
|
|
2450
|
+
--output <dir> Directory to write dockcraft.config.mts to
|
|
2451
|
+
--force Overwrite an existing config file
|
|
2452
|
+
-y, --yes Skip prompts, use defaults (required without a terminal)
|
|
1984
2453
|
`);
|
|
1985
2454
|
break;
|
|
1986
2455
|
case "generate":
|
|
@@ -1989,7 +2458,7 @@ USAGE:
|
|
|
1989
2458
|
dockcraft generate [options]
|
|
1990
2459
|
|
|
1991
2460
|
OPTIONS:
|
|
1992
|
-
--config <file> Config file path (default: dockcraft.config.ts)
|
|
2461
|
+
--config <file> Config file path (default: first of dockcraft.config.{ts,mts,js,mjs,cjs})
|
|
1993
2462
|
--dry-run Preview without writing
|
|
1994
2463
|
--force Overwrite existing files
|
|
1995
2464
|
--output <dir> Output directory
|
|
@@ -2016,7 +2485,7 @@ ARGUMENTS:
|
|
|
2016
2485
|
`);
|
|
2017
2486
|
break;
|
|
2018
2487
|
default:
|
|
2019
|
-
|
|
2488
|
+
showGeneralHelp();
|
|
2020
2489
|
}
|
|
2021
2490
|
}
|
|
2022
2491
|
function showVersion() {
|
|
@@ -2024,19 +2493,25 @@ function showVersion() {
|
|
|
2024
2493
|
}
|
|
2025
2494
|
async function main(cliArgs = process.argv.slice(2)) {
|
|
2026
2495
|
const args = parseArgs(cliArgs);
|
|
2496
|
+
const wantsHelp = args.flags.has("help") || args.flags.has("h");
|
|
2497
|
+
const wantsVersion = args.flags.has("version") || args.flags.has("v");
|
|
2027
2498
|
if (!args.command) {
|
|
2028
|
-
if (
|
|
2499
|
+
if (wantsVersion && !wantsHelp) {
|
|
2029
2500
|
showVersion();
|
|
2030
2501
|
return;
|
|
2031
2502
|
}
|
|
2032
2503
|
showHelp();
|
|
2033
2504
|
return;
|
|
2034
2505
|
}
|
|
2035
|
-
if (args.
|
|
2506
|
+
if (args.command === "help") {
|
|
2507
|
+
showHelp(args.positionals[0]);
|
|
2508
|
+
return;
|
|
2509
|
+
}
|
|
2510
|
+
if (wantsHelp) {
|
|
2036
2511
|
showHelp(args.command);
|
|
2037
2512
|
return;
|
|
2038
2513
|
}
|
|
2039
|
-
if (
|
|
2514
|
+
if (wantsVersion) {
|
|
2040
2515
|
showVersion();
|
|
2041
2516
|
return;
|
|
2042
2517
|
}
|
|
@@ -2044,22 +2519,20 @@ async function main(cliArgs = process.argv.slice(2)) {
|
|
|
2044
2519
|
if (!handler) {
|
|
2045
2520
|
error(`Unknown command: ${args.command}`);
|
|
2046
2521
|
console.log('Run "dockcraft --help" for usage information.');
|
|
2047
|
-
process.
|
|
2522
|
+
process.exitCode = 1;
|
|
2523
|
+
return;
|
|
2048
2524
|
}
|
|
2049
2525
|
try {
|
|
2050
2526
|
await handler(args);
|
|
2051
2527
|
} catch (err) {
|
|
2052
2528
|
error(err instanceof Error ? err.message : String(err));
|
|
2053
|
-
process.
|
|
2529
|
+
process.exitCode = 1;
|
|
2054
2530
|
}
|
|
2055
2531
|
}
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
}
|
|
2062
|
-
export {
|
|
2063
|
-
main
|
|
2064
|
-
};
|
|
2532
|
+
|
|
2533
|
+
// src/cli/bin.ts
|
|
2534
|
+
main().catch((err) => {
|
|
2535
|
+
error(err instanceof Error ? err.message : String(err));
|
|
2536
|
+
process.exitCode = 1;
|
|
2537
|
+
});
|
|
2065
2538
|
//# sourceMappingURL=index.js.map
|