@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/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
* @oxog/dockcraft - Zero-Dependency NPM Package
|
|
3
|
+
* GitHub: https://github.com/ersinkoc/DockCraft
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
6
|
"use strict";
|
|
7
7
|
var __defProp = Object.defineProperty;
|
|
8
8
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -23,8 +23,8 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
23
23
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
24
|
|
|
25
25
|
// src/index.ts
|
|
26
|
-
var
|
|
27
|
-
__export(
|
|
26
|
+
var src_exports = {};
|
|
27
|
+
__export(src_exports, {
|
|
28
28
|
ConfigError: () => ConfigError,
|
|
29
29
|
DockcraftError: () => DockcraftError,
|
|
30
30
|
FileWriteError: () => FileWriteError,
|
|
@@ -48,7 +48,62 @@ __export(index_exports, {
|
|
|
48
48
|
registerService: () => registerService,
|
|
49
49
|
resolvePort: () => resolvePort
|
|
50
50
|
});
|
|
51
|
-
module.exports = __toCommonJS(
|
|
51
|
+
module.exports = __toCommonJS(src_exports);
|
|
52
|
+
|
|
53
|
+
// src/types.ts
|
|
54
|
+
var DockcraftError = class extends Error {
|
|
55
|
+
constructor(message, code) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.code = code;
|
|
58
|
+
this.name = "DockcraftError";
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var ConfigError = class extends DockcraftError {
|
|
62
|
+
constructor(message, code = "CONFIG_ERROR") {
|
|
63
|
+
super(message, code);
|
|
64
|
+
this.name = "ConfigError";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var ValidationError = class extends DockcraftError {
|
|
68
|
+
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
69
|
+
super(message, code);
|
|
70
|
+
this.errors = errors;
|
|
71
|
+
this.name = "ValidationError";
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var PortConflictError = class extends DockcraftError {
|
|
75
|
+
constructor(port, code = "PORT_CONFLICT") {
|
|
76
|
+
super(`Port ${port} is already in use`, code);
|
|
77
|
+
this.name = "PortConflictError";
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var NoPortAvailableError = class extends DockcraftError {
|
|
81
|
+
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
82
|
+
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
83
|
+
this.name = "NoPortAvailableError";
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var FileWriteError = class extends DockcraftError {
|
|
87
|
+
constructor(path, code = "FILE_WRITE_ERROR", cause) {
|
|
88
|
+
super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
|
|
89
|
+
this.name = "FileWriteError";
|
|
90
|
+
if (cause !== void 0) {
|
|
91
|
+
this.cause = cause;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var PresetNotFoundError = class extends DockcraftError {
|
|
96
|
+
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
97
|
+
super(`Preset '${name}' not found`, code);
|
|
98
|
+
this.name = "PresetNotFoundError";
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var ServiceNotFoundError = class extends DockcraftError {
|
|
102
|
+
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
103
|
+
super(`Service '${name}' not found`, code);
|
|
104
|
+
this.name = "ServiceNotFoundError";
|
|
105
|
+
}
|
|
106
|
+
};
|
|
52
107
|
|
|
53
108
|
// src/presets/express-postgres.ts
|
|
54
109
|
var expressPostgresPreset = {
|
|
@@ -288,10 +343,24 @@ registerPreset("nest-postgres", nestPostgresPreset);
|
|
|
288
343
|
registerPreset("nest-mongo", nestMongoPreset);
|
|
289
344
|
registerPreset("node-minimal", nodeMinimalPreset);
|
|
290
345
|
|
|
346
|
+
// src/services/image.ts
|
|
347
|
+
function variantImage(repository, version, variant) {
|
|
348
|
+
if (version === "latest") {
|
|
349
|
+
return `${repository}:${variant}`;
|
|
350
|
+
}
|
|
351
|
+
if (version === variant || version.includes("-")) {
|
|
352
|
+
return `${repository}:${version}`;
|
|
353
|
+
}
|
|
354
|
+
return `${repository}:${version}-${variant}`;
|
|
355
|
+
}
|
|
356
|
+
function urlPart(value) {
|
|
357
|
+
return encodeURIComponent(value);
|
|
358
|
+
}
|
|
359
|
+
|
|
291
360
|
// src/services/postgres.ts
|
|
292
361
|
var postgresService = {
|
|
293
362
|
name: "postgres",
|
|
294
|
-
image: (version) =>
|
|
363
|
+
image: (version) => variantImage("postgres", version, "alpine"),
|
|
295
364
|
defaultPort: 5432,
|
|
296
365
|
defaultVersion: "16",
|
|
297
366
|
env: {
|
|
@@ -308,7 +377,7 @@ var postgresService = {
|
|
|
308
377
|
const database = config.env.POSTGRES_DB || "myapp";
|
|
309
378
|
const host = config.name;
|
|
310
379
|
const port = config.port;
|
|
311
|
-
return `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
380
|
+
return `postgres://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
312
381
|
}
|
|
313
382
|
};
|
|
314
383
|
|
|
@@ -333,7 +402,7 @@ var mysqlService = {
|
|
|
333
402
|
const database = config.env.MYSQL_DATABASE || "myapp";
|
|
334
403
|
const host = config.name;
|
|
335
404
|
const port = config.port;
|
|
336
|
-
return `mysql://${user}:${password}@${host}:${port}/${database}`;
|
|
405
|
+
return `mysql://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
337
406
|
}
|
|
338
407
|
};
|
|
339
408
|
|
|
@@ -357,14 +426,14 @@ var mongoService = {
|
|
|
357
426
|
const database = config.env.MONGO_INITDB_DATABASE || "myapp";
|
|
358
427
|
const host = config.name;
|
|
359
428
|
const port = config.port;
|
|
360
|
-
return `mongodb://${user}:${password}@${host}:${port}/${database}`;
|
|
429
|
+
return `mongodb://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}?authSource=admin`;
|
|
361
430
|
}
|
|
362
431
|
};
|
|
363
432
|
|
|
364
433
|
// src/services/redis.ts
|
|
365
434
|
var redisService = {
|
|
366
435
|
name: "redis",
|
|
367
|
-
image: (version) =>
|
|
436
|
+
image: (version) => variantImage("redis", version, "alpine"),
|
|
368
437
|
defaultPort: 6379,
|
|
369
438
|
defaultVersion: "7",
|
|
370
439
|
env: {},
|
|
@@ -374,7 +443,9 @@ var redisService = {
|
|
|
374
443
|
connectionString: (config) => {
|
|
375
444
|
const host = config.name;
|
|
376
445
|
const port = config.port;
|
|
377
|
-
|
|
446
|
+
const password = config.env.REDIS_PASSWORD;
|
|
447
|
+
const auth = password ? `:${urlPart(password)}@` : "";
|
|
448
|
+
return `redis://${auth}${host}:${port}`;
|
|
378
449
|
}
|
|
379
450
|
};
|
|
380
451
|
|
|
@@ -395,12 +466,13 @@ registerService(mongoService);
|
|
|
395
466
|
registerService(redisService);
|
|
396
467
|
|
|
397
468
|
// src/utils/merge.ts
|
|
469
|
+
var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
398
470
|
function deepMerge(...objects) {
|
|
399
471
|
const result = {};
|
|
400
472
|
for (const obj of objects) {
|
|
401
473
|
if (obj === null || typeof obj !== "object") continue;
|
|
402
474
|
for (const [key, value] of Object.entries(obj)) {
|
|
403
|
-
if (value === void 0) continue;
|
|
475
|
+
if (value === void 0 || UNSAFE_KEYS.has(key)) continue;
|
|
404
476
|
if (value && typeof value === "object" && !Array.isArray(value) && result[key] && typeof result[key] === "object" && !Array.isArray(result[key])) {
|
|
405
477
|
result[key] = deepMerge(result[key], value);
|
|
406
478
|
} else {
|
|
@@ -414,21 +486,30 @@ function deepMerge(...objects) {
|
|
|
414
486
|
// src/ports/scanner.ts
|
|
415
487
|
var import_net = require("net");
|
|
416
488
|
async function isPortAvailable(port) {
|
|
489
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
490
|
+
throw new RangeError(`Invalid port: ${String(port)}. Must be an integer between 1 and 65535.`);
|
|
491
|
+
}
|
|
417
492
|
return new Promise((resolve) => {
|
|
418
493
|
const server = (0, import_net.createServer)();
|
|
494
|
+
let settled = false;
|
|
495
|
+
const finish = (available) => {
|
|
496
|
+
if (settled) return;
|
|
497
|
+
settled = true;
|
|
498
|
+
clearTimeout(timer);
|
|
499
|
+
resolve(available);
|
|
500
|
+
};
|
|
501
|
+
const timer = setTimeout(() => {
|
|
502
|
+
server.close();
|
|
503
|
+
finish(false);
|
|
504
|
+
}, 1e3);
|
|
505
|
+
timer.unref();
|
|
419
506
|
server.once("error", () => {
|
|
420
|
-
|
|
507
|
+
finish(false);
|
|
421
508
|
});
|
|
422
509
|
server.once("listening", () => {
|
|
423
|
-
server.close(() =>
|
|
424
|
-
resolve(true);
|
|
425
|
-
});
|
|
510
|
+
server.close(() => finish(true));
|
|
426
511
|
});
|
|
427
512
|
server.listen(port, "127.0.0.1");
|
|
428
|
-
setTimeout(() => {
|
|
429
|
-
server.close();
|
|
430
|
-
resolve(false);
|
|
431
|
-
}, 1e3);
|
|
432
513
|
});
|
|
433
514
|
}
|
|
434
515
|
async function checkPorts(ports) {
|
|
@@ -467,24 +548,25 @@ async function resolvePort(options = {}) {
|
|
|
467
548
|
if (await isPortAvailable(preferred)) {
|
|
468
549
|
return preferred;
|
|
469
550
|
}
|
|
470
|
-
throw new
|
|
551
|
+
throw new PortConflictError(preferred);
|
|
471
552
|
case "range":
|
|
472
553
|
if (range) {
|
|
473
554
|
const [start, end = start + 100] = range;
|
|
474
|
-
const
|
|
475
|
-
if (
|
|
476
|
-
return
|
|
555
|
+
const port = await findAvailablePortInRange(start, end);
|
|
556
|
+
if (port !== void 0) {
|
|
557
|
+
return port;
|
|
477
558
|
}
|
|
478
|
-
throw new
|
|
559
|
+
throw new NoPortAvailableError([start, end]);
|
|
479
560
|
}
|
|
480
561
|
// Fall through to auto if no range specified
|
|
481
562
|
case "auto":
|
|
482
|
-
default:
|
|
563
|
+
default: {
|
|
483
564
|
const port = await findNextAvailablePort(preferred);
|
|
484
565
|
if (port !== void 0) {
|
|
485
566
|
return port;
|
|
486
567
|
}
|
|
487
|
-
throw new
|
|
568
|
+
throw new NoPortAvailableError([preferred, 65535]);
|
|
569
|
+
}
|
|
488
570
|
}
|
|
489
571
|
}
|
|
490
572
|
|
|
@@ -499,9 +581,9 @@ var DEFAULT_APP_CONFIG = {
|
|
|
499
581
|
volumes: [],
|
|
500
582
|
multiStage: true
|
|
501
583
|
};
|
|
502
|
-
function normalizeAppConfig(config) {
|
|
584
|
+
function normalizeAppConfig(config = {}) {
|
|
503
585
|
return {
|
|
504
|
-
port: config.port === "
|
|
586
|
+
port: typeof config.port === "number" ? config.port : DEFAULT_APP_CONFIG.port,
|
|
505
587
|
node: config.node ?? DEFAULT_APP_CONFIG.node,
|
|
506
588
|
workdir: config.workdir ?? DEFAULT_APP_CONFIG.workdir,
|
|
507
589
|
command: config.command ?? DEFAULT_APP_CONFIG.command,
|
|
@@ -523,7 +605,7 @@ function normalizeConfig(config) {
|
|
|
523
605
|
return {
|
|
524
606
|
name: config.name,
|
|
525
607
|
preset: config.preset,
|
|
526
|
-
app: config.app,
|
|
608
|
+
app: config.app ?? {},
|
|
527
609
|
services: config.services ?? {},
|
|
528
610
|
output: normalizeOutputConfig(config.output),
|
|
529
611
|
plugins: config.plugins ?? []
|
|
@@ -531,10 +613,11 @@ function normalizeConfig(config) {
|
|
|
531
613
|
}
|
|
532
614
|
|
|
533
615
|
// src/core/resolver.ts
|
|
616
|
+
var REDIS_AUTH_COMMAND = `sh -c 'exec redis-server --requirepass "$$REDIS_PASSWORD"'`;
|
|
534
617
|
function mergePreset(config, presetName) {
|
|
535
618
|
const preset = getPreset(presetName);
|
|
536
619
|
if (!preset) {
|
|
537
|
-
throw new
|
|
620
|
+
throw new PresetNotFoundError(presetName);
|
|
538
621
|
}
|
|
539
622
|
const merged = deepMerge(
|
|
540
623
|
{
|
|
@@ -547,55 +630,65 @@ function mergePreset(config, presetName) {
|
|
|
547
630
|
);
|
|
548
631
|
return merged;
|
|
549
632
|
}
|
|
550
|
-
async function
|
|
633
|
+
async function resolveServicePorts(portConfig, containerPort) {
|
|
634
|
+
if (portConfig === "auto") {
|
|
635
|
+
return {
|
|
636
|
+
port: containerPort,
|
|
637
|
+
hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
if (typeof portConfig === "number") {
|
|
641
|
+
return { port: containerPort, hostPort: portConfig };
|
|
642
|
+
}
|
|
643
|
+
if (portConfig && typeof portConfig === "object") {
|
|
644
|
+
const startPort = portConfig.start ?? containerPort;
|
|
645
|
+
const endPort = portConfig.end ?? startPort + 100;
|
|
646
|
+
return {
|
|
647
|
+
port: containerPort,
|
|
648
|
+
hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
return { port: containerPort };
|
|
652
|
+
}
|
|
653
|
+
async function resolveServiceConfig(name, config, projectName, definitions) {
|
|
551
654
|
if ("image" in config && config.image) {
|
|
552
655
|
return resolveCustomService(name, config);
|
|
553
656
|
}
|
|
554
|
-
const definition = getService(name);
|
|
657
|
+
const definition = definitions?.get(name) ?? getService(name);
|
|
555
658
|
if (!definition) {
|
|
556
|
-
throw new
|
|
557
|
-
}
|
|
558
|
-
const defaultPort = definition.defaultPort;
|
|
559
|
-
let port = defaultPort;
|
|
560
|
-
let hostPort;
|
|
561
|
-
if (config.port === "auto") {
|
|
562
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
563
|
-
hostPort = port;
|
|
564
|
-
} else if (typeof config.port === "number") {
|
|
565
|
-
port = config.port;
|
|
566
|
-
hostPort = config.port;
|
|
567
|
-
} else if (config.port && typeof config.port === "object") {
|
|
568
|
-
const { start, end } = config.port;
|
|
569
|
-
const startPort = start ?? defaultPort;
|
|
570
|
-
const endPort = end ?? startPort + 100;
|
|
571
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
572
|
-
hostPort = port;
|
|
659
|
+
throw new ServiceNotFoundError(name);
|
|
573
660
|
}
|
|
661
|
+
const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
|
|
574
662
|
const version = config.version ?? definition.defaultVersion;
|
|
575
663
|
const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
|
|
576
664
|
const env = { ...definition.env, ...config.env };
|
|
665
|
+
let command = config.command;
|
|
577
666
|
if (name === "postgres") {
|
|
578
667
|
const pgConfig = config;
|
|
579
668
|
if (pgConfig.database) env.POSTGRES_DB = pgConfig.database;
|
|
580
669
|
if (pgConfig.user) env.POSTGRES_USER = pgConfig.user;
|
|
581
670
|
if (pgConfig.password) env.POSTGRES_PASSWORD = pgConfig.password;
|
|
582
|
-
if (!env
|
|
671
|
+
if (!pgConfig.database && !config.env?.POSTGRES_DB) env.POSTGRES_DB = projectName;
|
|
583
672
|
} else if (name === "mysql") {
|
|
584
673
|
const mysqlConfig = config;
|
|
585
674
|
if (mysqlConfig.database) env.MYSQL_DATABASE = mysqlConfig.database;
|
|
586
675
|
if (mysqlConfig.user) env.MYSQL_USER = mysqlConfig.user;
|
|
587
676
|
if (mysqlConfig.password) env.MYSQL_PASSWORD = mysqlConfig.password;
|
|
588
677
|
if (mysqlConfig.rootPassword) env.MYSQL_ROOT_PASSWORD = mysqlConfig.rootPassword;
|
|
589
|
-
if (!env
|
|
678
|
+
if (!mysqlConfig.database && !config.env?.MYSQL_DATABASE) env.MYSQL_DATABASE = projectName;
|
|
590
679
|
} else if (name === "mongo") {
|
|
591
680
|
const mongoConfig = config;
|
|
592
681
|
if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
|
|
593
682
|
if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
|
|
594
683
|
if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
|
|
595
|
-
if (!env
|
|
684
|
+
if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
|
|
596
685
|
} else if (name === "redis") {
|
|
597
686
|
const redisConfig = config;
|
|
598
687
|
if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
|
|
688
|
+
if (env.REDIS_PASSWORD) {
|
|
689
|
+
env.REDISCLI_AUTH = env.REDIS_PASSWORD;
|
|
690
|
+
command ??= REDIS_AUTH_COMMAND;
|
|
691
|
+
}
|
|
599
692
|
}
|
|
600
693
|
const volumes = [...definition.volumes ?? [], ...config.volumes ?? []];
|
|
601
694
|
const resolvedConfig = {
|
|
@@ -605,7 +698,7 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
605
698
|
hostPort,
|
|
606
699
|
env,
|
|
607
700
|
volumes,
|
|
608
|
-
command
|
|
701
|
+
command,
|
|
609
702
|
healthcheck: definition.healthcheck,
|
|
610
703
|
ports: definition.ports
|
|
611
704
|
};
|
|
@@ -615,53 +708,53 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
615
708
|
return resolvedConfig;
|
|
616
709
|
}
|
|
617
710
|
async function resolveCustomService(name, config) {
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
624
|
-
hostPort = port;
|
|
625
|
-
} else if (typeof config.port === "number") {
|
|
626
|
-
port = config.port;
|
|
627
|
-
hostPort = config.port;
|
|
628
|
-
} else if (config.port && typeof config.port === "object") {
|
|
629
|
-
const { start, end } = config.port;
|
|
630
|
-
const startPort = start ?? defaultPort;
|
|
631
|
-
const endPort = end ?? startPort + 100;
|
|
632
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
633
|
-
hostPort = port;
|
|
711
|
+
let containerPort = 80;
|
|
712
|
+
if (typeof config.port === "number") {
|
|
713
|
+
containerPort = config.port;
|
|
714
|
+
} else if (config.port && typeof config.port === "object" && config.port.start) {
|
|
715
|
+
containerPort = config.port.start;
|
|
634
716
|
}
|
|
717
|
+
const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
|
|
635
718
|
return {
|
|
636
719
|
name,
|
|
637
720
|
image: config.image,
|
|
638
721
|
port,
|
|
639
722
|
hostPort,
|
|
640
|
-
env: config.env
|
|
641
|
-
volumes: config.volumes ?? [],
|
|
723
|
+
env: { ...config.env },
|
|
724
|
+
volumes: [...config.volumes ?? []],
|
|
642
725
|
command: config.command,
|
|
643
|
-
healthcheck: config.
|
|
726
|
+
healthcheck: config.healthcheck,
|
|
644
727
|
ports: []
|
|
645
728
|
};
|
|
646
729
|
}
|
|
730
|
+
function pluginServiceDefinitions(plugins) {
|
|
731
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
732
|
+
for (const plugin of plugins ?? []) {
|
|
733
|
+
for (const [name, def] of Object.entries(plugin.services ?? {})) {
|
|
734
|
+
definitions.set(name, { ...def, name });
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return definitions;
|
|
738
|
+
}
|
|
647
739
|
async function resolveConfig(config) {
|
|
648
740
|
let finalConfig = config;
|
|
649
741
|
if (config.preset) {
|
|
650
742
|
finalConfig = mergePreset(config, config.preset);
|
|
651
743
|
}
|
|
652
744
|
finalConfig = normalizeConfig(finalConfig);
|
|
653
|
-
const
|
|
654
|
-
const
|
|
745
|
+
const appConfig = finalConfig.app ?? {};
|
|
746
|
+
const appPort = appConfig.port === "auto" ? await resolvePort({ preferred: 3e3, strategy: "auto" }) : appConfig.port;
|
|
747
|
+
const app = normalizeAppConfig({ ...appConfig, port: appPort });
|
|
748
|
+
const definitions = pluginServiceDefinitions(finalConfig.plugins);
|
|
655
749
|
const services2 = {};
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
}
|
|
750
|
+
for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
|
|
751
|
+
if (serviceConfig) {
|
|
752
|
+
services2[name] = await resolveServiceConfig(
|
|
753
|
+
name,
|
|
754
|
+
serviceConfig,
|
|
755
|
+
finalConfig.name,
|
|
756
|
+
definitions
|
|
757
|
+
);
|
|
665
758
|
}
|
|
666
759
|
}
|
|
667
760
|
return {
|
|
@@ -684,31 +777,73 @@ function isEnvVarName(value) {
|
|
|
684
777
|
}
|
|
685
778
|
function isVersion(value) {
|
|
686
779
|
if (typeof value !== "string") return false;
|
|
687
|
-
|
|
688
|
-
return /^[a-zA-Z0-9.\-]+$/.test(value);
|
|
780
|
+
return /^\w[\w.-]{0,127}$/.test(value);
|
|
689
781
|
}
|
|
690
782
|
function isProjectName(value) {
|
|
691
783
|
if (typeof value !== "string") return false;
|
|
692
784
|
if (value.length === 0 || value.length > 100) return false;
|
|
693
785
|
return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(value);
|
|
694
786
|
}
|
|
787
|
+
function isComposeServiceName(value) {
|
|
788
|
+
if (typeof value !== "string") return false;
|
|
789
|
+
return /^[a-zA-Z0-9][\w.-]{0,62}$/.test(value);
|
|
790
|
+
}
|
|
791
|
+
var DOMAIN_COMPONENT = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
|
|
792
|
+
var DOMAIN = `${DOMAIN_COMPONENT}(?:\\.${DOMAIN_COMPONENT})*(?::[0-9]+)?`;
|
|
793
|
+
var PATH_COMPONENT = "[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*";
|
|
794
|
+
var TAG = "\\w[\\w.-]{0,127}";
|
|
795
|
+
var DIGEST = "[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}";
|
|
796
|
+
var IMAGE_REFERENCE = new RegExp(
|
|
797
|
+
`^(?:${DOMAIN}/)?${PATH_COMPONENT}(?:/${PATH_COMPONENT})*(?::${TAG})?(?:@${DIGEST})?$`
|
|
798
|
+
);
|
|
799
|
+
function isDockerImage(value) {
|
|
800
|
+
if (typeof value !== "string") return false;
|
|
801
|
+
if (value.length === 0 || value.length > 255) return false;
|
|
802
|
+
return IMAGE_REFERENCE.test(value);
|
|
803
|
+
}
|
|
804
|
+
function isHealthcheckPath(value) {
|
|
805
|
+
if (typeof value !== "string") return false;
|
|
806
|
+
return value.length <= 2048 && /^\/[\w\-.~%/?=&+,;:@]*$/.test(value);
|
|
807
|
+
}
|
|
808
|
+
function isSingleLine(value) {
|
|
809
|
+
return typeof value === "string" && !/[\r\n\0]/.test(value);
|
|
810
|
+
}
|
|
811
|
+
function isContainerPath(value) {
|
|
812
|
+
return typeof value === "string" && /^\/[\w./-]*$/.test(value);
|
|
813
|
+
}
|
|
814
|
+
function namedVolume(spec) {
|
|
815
|
+
const index = spec.indexOf(":");
|
|
816
|
+
if (index <= 0) return void 0;
|
|
817
|
+
const source = spec.slice(0, index);
|
|
818
|
+
return /^[a-zA-Z0-9][\w.-]*$/.test(source) ? source : void 0;
|
|
819
|
+
}
|
|
695
820
|
|
|
696
821
|
// src/core/validator.ts
|
|
822
|
+
var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
|
|
697
823
|
function validateProjectName(name) {
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
return
|
|
824
|
+
if (isProjectName(name)) return [];
|
|
825
|
+
return [{
|
|
826
|
+
code: "INVALID_NAME",
|
|
827
|
+
message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
|
|
828
|
+
path: "name",
|
|
829
|
+
value: name
|
|
830
|
+
}];
|
|
831
|
+
}
|
|
832
|
+
function validateStringList(value, path) {
|
|
833
|
+
if (value === void 0) return [];
|
|
834
|
+
if (!Array.isArray(value)) {
|
|
835
|
+
return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
|
|
836
|
+
}
|
|
837
|
+
return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
|
|
838
|
+
code: "INVALID_VALUE",
|
|
839
|
+
message: `Invalid entry ${JSON.stringify(item)} in '${path}'. Entries must be non-empty single-line strings.`,
|
|
840
|
+
path,
|
|
841
|
+
value: item
|
|
842
|
+
}));
|
|
708
843
|
}
|
|
709
844
|
function validateAppConfig(config) {
|
|
710
845
|
const errors = [];
|
|
711
|
-
if (config.port !== "auto" && !isPort(config.port)) {
|
|
846
|
+
if (config.port !== void 0 && config.port !== "auto" && !isPort(config.port)) {
|
|
712
847
|
errors.push({
|
|
713
848
|
code: "INVALID_PORT",
|
|
714
849
|
message: `Invalid app port '${String(config.port)}'. Must be a number between 1-65535 or 'auto'.`,
|
|
@@ -719,54 +854,163 @@ function validateAppConfig(config) {
|
|
|
719
854
|
if (config.node !== void 0 && !isVersion(config.node)) {
|
|
720
855
|
errors.push({
|
|
721
856
|
code: "INVALID_VERSION",
|
|
722
|
-
message: `Invalid Node.js version '${config.node}'.`,
|
|
857
|
+
message: `Invalid Node.js version '${String(config.node)}'. Must be a valid Docker image tag.`,
|
|
723
858
|
path: "app.node",
|
|
724
859
|
value: config.node
|
|
725
860
|
});
|
|
726
861
|
}
|
|
727
|
-
if (config.
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
862
|
+
if (config.workdir !== void 0 && !isContainerPath(config.workdir)) {
|
|
863
|
+
errors.push({
|
|
864
|
+
code: "INVALID_WORKDIR",
|
|
865
|
+
message: `Invalid workdir '${String(config.workdir)}'. Must be an absolute path made of letters, numbers, '.', '_', '-' and '/'.`,
|
|
866
|
+
path: "app.workdir",
|
|
867
|
+
value: config.workdir
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
for (const key of ["command", "buildCommand"]) {
|
|
871
|
+
const value = config[key];
|
|
872
|
+
if (value !== void 0 && !isSingleLine(value)) {
|
|
873
|
+
errors.push({
|
|
874
|
+
code: "INVALID_COMMAND",
|
|
875
|
+
message: `Invalid app.${key}: must be a single-line string.`,
|
|
876
|
+
path: `app.${key}`,
|
|
877
|
+
value
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
if (config.command !== void 0 && config.command.trim() === "") {
|
|
882
|
+
errors.push({
|
|
883
|
+
code: "INVALID_COMMAND",
|
|
884
|
+
message: "Invalid app.command: must not be empty.",
|
|
885
|
+
path: "app.command",
|
|
886
|
+
value: config.command
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
if (config.healthcheck !== void 0 && !isHealthcheckPath(config.healthcheck)) {
|
|
890
|
+
errors.push({
|
|
891
|
+
code: "INVALID_HEALTHCHECK",
|
|
892
|
+
message: `Invalid health check path '${String(config.healthcheck)}'. Must start with '/' and contain only URL path characters (no quotes, spaces, '$' or backslashes).`,
|
|
893
|
+
path: "app.healthcheck",
|
|
894
|
+
value: config.healthcheck
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
if (config.env !== void 0) {
|
|
898
|
+
if (!Array.isArray(config.env)) {
|
|
899
|
+
errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
|
|
900
|
+
} else {
|
|
901
|
+
for (const envVar of config.env) {
|
|
902
|
+
if (!isEnvVarName(envVar)) {
|
|
903
|
+
errors.push({
|
|
904
|
+
code: "INVALID_ENV_VAR",
|
|
905
|
+
message: `Invalid environment variable name '${String(envVar)}'. Must be uppercase letters, numbers, and underscores.`,
|
|
906
|
+
path: "app.env",
|
|
907
|
+
value: envVar
|
|
908
|
+
});
|
|
909
|
+
}
|
|
736
910
|
}
|
|
737
911
|
}
|
|
738
912
|
}
|
|
913
|
+
errors.push(...validateStringList(config.volumes, "app.volumes"));
|
|
739
914
|
return errors;
|
|
740
915
|
}
|
|
916
|
+
function validateServicePort(name, port) {
|
|
917
|
+
if (port === void 0 || port === "auto" || isPort(port)) return [];
|
|
918
|
+
if (port && typeof port === "object" && !Array.isArray(port)) {
|
|
919
|
+
const { start, end } = port;
|
|
920
|
+
if (isPort(start) && (end === void 0 || isPort(end) && end >= start)) {
|
|
921
|
+
return [];
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return [{
|
|
925
|
+
code: "INVALID_PORT",
|
|
926
|
+
message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
|
|
927
|
+
path: `services.${name}.port`,
|
|
928
|
+
value: port
|
|
929
|
+
}];
|
|
930
|
+
}
|
|
741
931
|
function validateServiceConfig(name, config) {
|
|
742
932
|
const errors = [];
|
|
743
|
-
if (
|
|
933
|
+
if (!isComposeServiceName(name)) {
|
|
934
|
+
errors.push({
|
|
935
|
+
code: "INVALID_SERVICE_NAME",
|
|
936
|
+
message: `Invalid service name '${name}'. Must start with a letter or digit and contain only letters, digits, '.', '_' and '-'.`,
|
|
937
|
+
path: `services.${name}`,
|
|
938
|
+
value: name
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
942
|
+
errors.push({
|
|
943
|
+
code: "INVALID_TYPE",
|
|
944
|
+
message: `Service '${name}' must be configured with an object.`,
|
|
945
|
+
path: `services.${name}`,
|
|
946
|
+
value: config
|
|
947
|
+
});
|
|
948
|
+
return errors;
|
|
949
|
+
}
|
|
950
|
+
const svc = config;
|
|
951
|
+
if (svc.version !== void 0 && !isVersion(svc.version)) {
|
|
744
952
|
errors.push({
|
|
745
953
|
code: "INVALID_VERSION",
|
|
746
|
-
message: `Invalid version '${
|
|
954
|
+
message: `Invalid version '${String(svc.version)}' for service '${name}'. Must be a valid Docker image tag.`,
|
|
747
955
|
path: `services.${name}.version`,
|
|
748
|
-
value:
|
|
956
|
+
value: svc.version
|
|
749
957
|
});
|
|
750
958
|
}
|
|
751
|
-
if (
|
|
959
|
+
if (svc.image !== void 0 && !isDockerImage(svc.image)) {
|
|
752
960
|
errors.push({
|
|
753
|
-
code: "
|
|
754
|
-
message: `Invalid
|
|
755
|
-
path: `services.${name}.
|
|
756
|
-
value:
|
|
961
|
+
code: "INVALID_IMAGE",
|
|
962
|
+
message: `Invalid image '${String(svc.image)}' for service '${name}'. Must be a Docker image reference such as 'elasticsearch:8.11.0'.`,
|
|
963
|
+
path: `services.${name}.image`,
|
|
964
|
+
value: svc.image
|
|
757
965
|
});
|
|
758
966
|
}
|
|
967
|
+
errors.push(...validateServicePort(name, svc.port));
|
|
968
|
+
if (svc.env !== void 0) {
|
|
969
|
+
if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
|
|
970
|
+
errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
|
|
971
|
+
} else {
|
|
972
|
+
for (const [key, value] of Object.entries(svc.env)) {
|
|
973
|
+
if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
|
|
974
|
+
errors.push({
|
|
975
|
+
code: "INVALID_ENV_VAR",
|
|
976
|
+
message: `Invalid environment variable '${key}' for service '${name}'. Names must not be empty or contain '=' or whitespace, and values must be strings.`,
|
|
977
|
+
path: `services.${name}.env.${key}`,
|
|
978
|
+
value
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
|
|
985
|
+
const value = svc[key];
|
|
986
|
+
if (value !== void 0 && typeof value !== "string") {
|
|
987
|
+
errors.push({
|
|
988
|
+
code: "INVALID_TYPE",
|
|
989
|
+
message: `'services.${name}.${key}' must be a string.`,
|
|
990
|
+
path: `services.${name}.${key}`,
|
|
991
|
+
value
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
errors.push(...validateStringList(svc.volumes, `services.${name}.volumes`));
|
|
759
996
|
return errors;
|
|
760
997
|
}
|
|
761
998
|
function validateOutputConfig(config) {
|
|
762
999
|
const errors = [];
|
|
763
|
-
if (config
|
|
764
|
-
|
|
1000
|
+
if (config.dir !== void 0 && (typeof config.dir !== "string" || config.dir === "" || config.dir.includes("\0"))) {
|
|
1001
|
+
errors.push({
|
|
1002
|
+
code: "INVALID_OUTPUT_DIR",
|
|
1003
|
+
message: "output.dir must be a non-empty path.",
|
|
1004
|
+
path: "output.dir",
|
|
1005
|
+
value: config.dir
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
if (config.files) {
|
|
765
1009
|
for (const file of config.files) {
|
|
766
|
-
if (!
|
|
1010
|
+
if (!VALID_OUTPUT_FILES.includes(file)) {
|
|
767
1011
|
errors.push({
|
|
768
1012
|
code: "INVALID_OUTPUT_FILE",
|
|
769
|
-
message: `Invalid output file '${file}'. Must be one of: ${
|
|
1013
|
+
message: `Invalid output file '${String(file)}'. Must be one of: ${VALID_OUTPUT_FILES.join(", ")}.`,
|
|
770
1014
|
path: "output.files",
|
|
771
1015
|
value: file
|
|
772
1016
|
});
|
|
@@ -777,13 +1021,19 @@ function validateOutputConfig(config) {
|
|
|
777
1021
|
}
|
|
778
1022
|
function validateConfig(config) {
|
|
779
1023
|
const errors = [];
|
|
1024
|
+
if (typeof config !== "object" || config === null) {
|
|
1025
|
+
return {
|
|
1026
|
+
valid: false,
|
|
1027
|
+
errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
780
1030
|
errors.push(...validateProjectName(config.name));
|
|
781
1031
|
if (config.app) {
|
|
782
1032
|
errors.push(...validateAppConfig(config.app));
|
|
783
1033
|
}
|
|
784
1034
|
if (config.services) {
|
|
785
1035
|
for (const [name, serviceConfig] of Object.entries(config.services)) {
|
|
786
|
-
if (serviceConfig) {
|
|
1036
|
+
if (serviceConfig !== void 0) {
|
|
787
1037
|
errors.push(...validateServiceConfig(name, serviceConfig));
|
|
788
1038
|
}
|
|
789
1039
|
}
|
|
@@ -798,18 +1048,23 @@ function validateConfig(config) {
|
|
|
798
1048
|
}
|
|
799
1049
|
function validateResolvedConfig(config) {
|
|
800
1050
|
const errors = [];
|
|
801
|
-
const usedPorts = /* @__PURE__ */ new
|
|
802
|
-
usedPorts.
|
|
1051
|
+
const usedPorts = /* @__PURE__ */ new Map();
|
|
1052
|
+
usedPorts.set(config.app.port, "app");
|
|
803
1053
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1054
|
+
const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
|
|
1055
|
+
for (const port of published) {
|
|
1056
|
+
const owner = usedPorts.get(port);
|
|
1057
|
+
if (owner !== void 0) {
|
|
1058
|
+
errors.push({
|
|
1059
|
+
code: "PORT_CONFLICT",
|
|
1060
|
+
message: `Host port ${port} of service '${serviceName}' is already published by '${owner}'.`,
|
|
1061
|
+
path: `services.${serviceName}.port`,
|
|
1062
|
+
value: port
|
|
1063
|
+
});
|
|
1064
|
+
} else {
|
|
1065
|
+
usedPorts.set(port, serviceName);
|
|
1066
|
+
}
|
|
811
1067
|
}
|
|
812
|
-
usedPorts.add(serviceConfig.port);
|
|
813
1068
|
}
|
|
814
1069
|
return {
|
|
815
1070
|
valid: errors.length === 0,
|
|
@@ -817,11 +1072,24 @@ function validateResolvedConfig(config) {
|
|
|
817
1072
|
};
|
|
818
1073
|
}
|
|
819
1074
|
|
|
1075
|
+
// src/generators/healthcheck.ts
|
|
1076
|
+
function nodeHealthcheckScript(port, path) {
|
|
1077
|
+
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))`;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
820
1080
|
// src/generators/dockerfile.ts
|
|
1081
|
+
function dockerCmd(command) {
|
|
1082
|
+
const words = command.trim().split(/\s+/);
|
|
1083
|
+
if (words.every((word) => /^[\w@%+=:,./-]+$/.test(word))) {
|
|
1084
|
+
return `CMD ${JSON.stringify(words).replace(/","/g, '", "')}`;
|
|
1085
|
+
}
|
|
1086
|
+
return `CMD ${command.trim()}`;
|
|
1087
|
+
}
|
|
821
1088
|
function generateDockerfile(config, _context) {
|
|
822
|
-
const { app
|
|
1089
|
+
const { app } = config;
|
|
823
1090
|
const { node, workdir, command, buildCommand, multiStage, healthcheck } = app;
|
|
824
1091
|
const port = app.port;
|
|
1092
|
+
const hasBuild = buildCommand.trim() !== "";
|
|
825
1093
|
let dockerfile = "";
|
|
826
1094
|
if (multiStage) {
|
|
827
1095
|
dockerfile += `# Builder stage
|
|
@@ -847,11 +1115,13 @@ function generateDockerfile(config, _context) {
|
|
|
847
1115
|
dockerfile += `COPY . .
|
|
848
1116
|
|
|
849
1117
|
`;
|
|
850
|
-
|
|
1118
|
+
if (hasBuild) {
|
|
1119
|
+
dockerfile += `# Build application
|
|
851
1120
|
`;
|
|
852
|
-
|
|
1121
|
+
dockerfile += `RUN ${buildCommand.trim()}
|
|
853
1122
|
|
|
854
1123
|
`;
|
|
1124
|
+
}
|
|
855
1125
|
dockerfile += `# Production stage
|
|
856
1126
|
`;
|
|
857
1127
|
dockerfile += `FROM node:${node}
|
|
@@ -865,13 +1135,19 @@ function generateDockerfile(config, _context) {
|
|
|
865
1135
|
`;
|
|
866
1136
|
dockerfile += `# Copy from builder
|
|
867
1137
|
`;
|
|
868
|
-
|
|
1138
|
+
if (hasBuild) {
|
|
1139
|
+
dockerfile += `COPY --from=builder ${workdir}/node_modules ./node_modules
|
|
869
1140
|
`;
|
|
870
|
-
|
|
1141
|
+
dockerfile += `COPY --from=builder ${workdir}/dist ./dist
|
|
871
1142
|
`;
|
|
872
|
-
|
|
1143
|
+
dockerfile += `COPY --from=builder ${workdir}/package*.json ./
|
|
873
1144
|
|
|
874
1145
|
`;
|
|
1146
|
+
} else {
|
|
1147
|
+
dockerfile += `COPY --from=builder ${workdir} ./
|
|
1148
|
+
|
|
1149
|
+
`;
|
|
1150
|
+
}
|
|
875
1151
|
} else {
|
|
876
1152
|
dockerfile += `FROM node:${node}
|
|
877
1153
|
|
|
@@ -902,180 +1178,232 @@ function generateDockerfile(config, _context) {
|
|
|
902
1178
|
|
|
903
1179
|
`;
|
|
904
1180
|
if (healthcheck) {
|
|
1181
|
+
const script = JSON.stringify(nodeHealthcheckScript(port, healthcheck));
|
|
905
1182
|
dockerfile += `HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \\
|
|
906
|
-
|
|
1183
|
+
`;
|
|
1184
|
+
dockerfile += ` CMD ["node", "-e", ${script}]
|
|
907
1185
|
|
|
908
1186
|
`;
|
|
909
1187
|
}
|
|
910
|
-
dockerfile +=
|
|
1188
|
+
dockerfile += `${dockerCmd(command)}
|
|
911
1189
|
`;
|
|
912
1190
|
return dockerfile;
|
|
913
1191
|
}
|
|
914
1192
|
|
|
915
1193
|
// src/utils/yaml.ts
|
|
1194
|
+
var PLAIN_SCALAR = /^[A-Za-z_/.][\w./=@+~-]*$/;
|
|
1195
|
+
var NUMBER_LIKE = /^\.(?:\d|inf$|nan$)/i;
|
|
1196
|
+
var DURATION = /^\d+(?:\.\d+)?(?:ns|us|ms|s|m|h)$/;
|
|
1197
|
+
var RESERVED_WORD = /^(?:y|n|yes|no|on|off|true|false|null)$/i;
|
|
916
1198
|
function toYaml(obj, indent = 0) {
|
|
917
|
-
|
|
918
|
-
|
|
1199
|
+
if (isInline(obj)) {
|
|
1200
|
+
return inlineValue(obj);
|
|
1201
|
+
}
|
|
1202
|
+
if (Array.isArray(obj)) {
|
|
1203
|
+
return arrayToYaml(obj, indent);
|
|
1204
|
+
}
|
|
1205
|
+
return objectToYaml(obj, indent);
|
|
1206
|
+
}
|
|
1207
|
+
function isInline(value) {
|
|
1208
|
+
if (value === null || typeof value !== "object") {
|
|
1209
|
+
return true;
|
|
1210
|
+
}
|
|
1211
|
+
if (Array.isArray(value)) {
|
|
1212
|
+
return value.every((item) => item === null || item === void 0 || typeof item !== "object");
|
|
1213
|
+
}
|
|
1214
|
+
return Object.values(value).every((item) => item === void 0);
|
|
1215
|
+
}
|
|
1216
|
+
function inlineValue(value) {
|
|
1217
|
+
if (value === null || value === void 0) {
|
|
919
1218
|
return "null";
|
|
920
1219
|
}
|
|
921
|
-
if (typeof
|
|
922
|
-
return
|
|
1220
|
+
if (typeof value === "string") {
|
|
1221
|
+
return yamlScalar(value);
|
|
923
1222
|
}
|
|
924
|
-
if (typeof
|
|
925
|
-
return String(
|
|
1223
|
+
if (typeof value === "number") {
|
|
1224
|
+
if (Number.isFinite(value)) return String(value);
|
|
1225
|
+
if (Number.isNaN(value)) return ".nan";
|
|
1226
|
+
return value > 0 ? ".inf" : "-.inf";
|
|
926
1227
|
}
|
|
927
|
-
if (
|
|
928
|
-
return
|
|
1228
|
+
if (typeof value === "boolean") {
|
|
1229
|
+
return String(value);
|
|
929
1230
|
}
|
|
930
|
-
if (
|
|
931
|
-
return
|
|
1231
|
+
if (Array.isArray(value)) {
|
|
1232
|
+
return `[${value.map((item) => inlineValue(item)).join(", ")}]`;
|
|
932
1233
|
}
|
|
933
|
-
|
|
1234
|
+
if (typeof value === "object") {
|
|
1235
|
+
return "{}";
|
|
1236
|
+
}
|
|
1237
|
+
return JSON.stringify(String(value));
|
|
934
1238
|
}
|
|
935
1239
|
function arrayToYaml(arr, indent) {
|
|
936
1240
|
const spaces = " ".repeat(indent);
|
|
937
|
-
const innerSpaces = " ".repeat(indent + 2);
|
|
938
|
-
if (arr.length === 0) {
|
|
939
|
-
return "[]";
|
|
940
|
-
}
|
|
941
|
-
if (arr.every(
|
|
942
|
-
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null
|
|
943
|
-
)) {
|
|
944
|
-
const items = arr.map((item) => toYaml(item, 0)).join(", ");
|
|
945
|
-
return `[${items}]`;
|
|
946
|
-
}
|
|
947
1241
|
const lines = [];
|
|
948
1242
|
for (const item of arr) {
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
lines.push(`${spaces}- ${value.slice(indent + 2)}`);
|
|
1243
|
+
if (isInline(item)) {
|
|
1244
|
+
lines.push(`${spaces}- ${inlineValue(item)}`);
|
|
952
1245
|
} else {
|
|
953
|
-
|
|
1246
|
+
const block = toYaml(item, indent + 2);
|
|
1247
|
+
lines.push(`${spaces}- ${block.slice(indent + 2)}`);
|
|
954
1248
|
}
|
|
955
1249
|
}
|
|
956
1250
|
return lines.join("\n");
|
|
957
1251
|
}
|
|
958
1252
|
function objectToYaml(obj, indent) {
|
|
959
|
-
const keys = Object.keys(obj);
|
|
960
|
-
if (keys.length === 0) {
|
|
961
|
-
return "{}";
|
|
962
|
-
}
|
|
963
1253
|
const spaces = " ".repeat(indent);
|
|
964
1254
|
const lines = [];
|
|
965
|
-
for (const key of
|
|
966
|
-
|
|
967
|
-
const
|
|
968
|
-
if (
|
|
969
|
-
lines.push(`${spaces}${
|
|
970
|
-
lines.push(valueStr);
|
|
1255
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1256
|
+
if (value === void 0) continue;
|
|
1257
|
+
const yamlKey = yamlScalar(key);
|
|
1258
|
+
if (isInline(value)) {
|
|
1259
|
+
lines.push(`${spaces}${yamlKey}: ${inlineValue(value)}`);
|
|
971
1260
|
} else {
|
|
972
|
-
lines.push(`${spaces}${
|
|
1261
|
+
lines.push(`${spaces}${yamlKey}:`);
|
|
1262
|
+
lines.push(toYaml(value, indent + 2));
|
|
973
1263
|
}
|
|
974
1264
|
}
|
|
975
1265
|
return lines.join("\n");
|
|
976
1266
|
}
|
|
977
|
-
function
|
|
978
|
-
if (str
|
|
979
|
-
return
|
|
980
|
-
}
|
|
981
|
-
if (/^[\-\?:\[\]{},&*#|!<>@%'"`]/.test(str)) {
|
|
982
|
-
return `"${escapeString(str)}"`;
|
|
1267
|
+
function yamlScalar(str) {
|
|
1268
|
+
if ((PLAIN_SCALAR.test(str) || DURATION.test(str)) && !RESERVED_WORD.test(str) && !NUMBER_LIKE.test(str)) {
|
|
1269
|
+
return str;
|
|
983
1270
|
}
|
|
984
|
-
|
|
985
|
-
|
|
1271
|
+
return JSON.stringify(str);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// src/generators/paths.ts
|
|
1275
|
+
var import_path = require("path");
|
|
1276
|
+
function currentDir() {
|
|
1277
|
+
return typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : "/";
|
|
1278
|
+
}
|
|
1279
|
+
function outputSubdir(dir) {
|
|
1280
|
+
if (!dir) {
|
|
1281
|
+
return void 0;
|
|
986
1282
|
}
|
|
987
|
-
|
|
988
|
-
|
|
1283
|
+
const root = currentDir();
|
|
1284
|
+
const subdir = (0, import_path.relative)(root, (0, import_path.isAbsolute)(dir) ? dir : (0, import_path.join)(root, dir)).replace(/\\/g, "/");
|
|
1285
|
+
if (subdir === "" || subdir === ".." || subdir.startsWith("../") || (0, import_path.isAbsolute)(subdir)) {
|
|
1286
|
+
return void 0;
|
|
989
1287
|
}
|
|
990
|
-
return
|
|
1288
|
+
return subdir;
|
|
991
1289
|
}
|
|
992
|
-
function
|
|
993
|
-
return
|
|
1290
|
+
function pathToRoot(subdir) {
|
|
1291
|
+
return subdir.split("/").map(() => "..").join("/");
|
|
994
1292
|
}
|
|
995
1293
|
|
|
996
1294
|
// src/generators/compose.ts
|
|
997
|
-
function
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1295
|
+
function composeLiteral(value) {
|
|
1296
|
+
return value.replace(/\$/g, "$$$$");
|
|
1297
|
+
}
|
|
1298
|
+
function composeBuild(outputDir) {
|
|
1299
|
+
const subdir = outputSubdir(outputDir);
|
|
1300
|
+
if (subdir === void 0) {
|
|
1301
|
+
return ".";
|
|
1302
|
+
}
|
|
1303
|
+
return { context: pathToRoot(subdir), dockerfile: `${subdir}/Dockerfile` };
|
|
1304
|
+
}
|
|
1305
|
+
function generateCompose(config, _context) {
|
|
1306
|
+
const { app, services: services2 } = config;
|
|
1307
|
+
const appService = {
|
|
1308
|
+
build: composeBuild(config.output.dir),
|
|
1309
|
+
ports: [`${app.port}:${app.port}`],
|
|
1310
|
+
environment: generateAppEnvironment(config),
|
|
1311
|
+
depends_on: Object.keys(services2),
|
|
1312
|
+
volumes: app.volumes.length > 0 ? app.volumes : void 0
|
|
1011
1313
|
};
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
test: [`CMD`, `curl`, `-f`, `http://localhost:${app.port}${app.healthcheck}`],
|
|
1314
|
+
if (app.healthcheck) {
|
|
1315
|
+
appService.healthcheck = {
|
|
1316
|
+
test: ["CMD", "node", "-e", nodeHealthcheckScript(app.port, app.healthcheck)],
|
|
1016
1317
|
interval: "30s",
|
|
1017
1318
|
timeout: "3s",
|
|
1018
1319
|
retries: 3
|
|
1019
1320
|
};
|
|
1020
1321
|
}
|
|
1322
|
+
const composeServices = { app: appService };
|
|
1021
1323
|
for (const [serviceName, serviceConfig] of Object.entries(services2)) {
|
|
1022
|
-
|
|
1324
|
+
composeServices[serviceName] = generateServiceConfig(serviceConfig);
|
|
1023
1325
|
}
|
|
1024
|
-
|
|
1326
|
+
const compose = {
|
|
1327
|
+
services: composeServices,
|
|
1328
|
+
volumes: generateVolumeDefinitions(config)
|
|
1329
|
+
};
|
|
1330
|
+
return `${toYaml(compose)}
|
|
1331
|
+
`;
|
|
1025
1332
|
}
|
|
1026
|
-
function
|
|
1333
|
+
function connectionStringVar(serviceName) {
|
|
1334
|
+
return `${serviceName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}_URL`;
|
|
1335
|
+
}
|
|
1336
|
+
function hasDatabaseUrlAlias(config) {
|
|
1337
|
+
return config.services.postgres !== void 0 && !Object.entries(config.services).some(
|
|
1338
|
+
([name, service]) => Boolean(service.connectionString) && connectionStringVar(name) === "DATABASE_URL"
|
|
1339
|
+
);
|
|
1340
|
+
}
|
|
1341
|
+
function generateAppEnvironment(config) {
|
|
1027
1342
|
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1343
|
+
const add = (key, value) => {
|
|
1344
|
+
env.push(`${key}=${composeLiteral(String(value))}`);
|
|
1345
|
+
};
|
|
1028
1346
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1029
1347
|
if (serviceConfig.connectionString) {
|
|
1030
|
-
|
|
1031
|
-
env.push(`${key}=${serviceConfig.connectionString}`);
|
|
1348
|
+
add(connectionStringVar(serviceName), serviceConfig.connectionString);
|
|
1032
1349
|
}
|
|
1033
1350
|
}
|
|
1034
1351
|
if (config.services.postgres) {
|
|
1035
1352
|
const pg = config.services.postgres;
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1353
|
+
if (pg.connectionString && hasDatabaseUrlAlias(config)) {
|
|
1354
|
+
add("DATABASE_URL", pg.connectionString);
|
|
1355
|
+
}
|
|
1356
|
+
add("POSTGRES_HOST", pg.name);
|
|
1357
|
+
add("POSTGRES_PORT", pg.port);
|
|
1358
|
+
add("POSTGRES_USER", pg.env.POSTGRES_USER || "postgres");
|
|
1359
|
+
add("POSTGRES_PASSWORD", pg.env.POSTGRES_PASSWORD || "postgres");
|
|
1360
|
+
add("POSTGRES_DB", pg.env.POSTGRES_DB || config.name);
|
|
1041
1361
|
}
|
|
1042
1362
|
if (config.services.mysql) {
|
|
1043
1363
|
const mysql = config.services.mysql;
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1364
|
+
add("MYSQL_HOST", mysql.name);
|
|
1365
|
+
add("MYSQL_PORT", mysql.port);
|
|
1366
|
+
add("MYSQL_USER", mysql.env.MYSQL_USER || "root");
|
|
1367
|
+
add("MYSQL_PASSWORD", mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root");
|
|
1368
|
+
add("MYSQL_DATABASE", mysql.env.MYSQL_DATABASE || config.name);
|
|
1049
1369
|
}
|
|
1050
1370
|
if (config.services.mongo) {
|
|
1051
1371
|
const mongo = config.services.mongo;
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1372
|
+
add("MONGO_HOST", mongo.name);
|
|
1373
|
+
add("MONGO_PORT", mongo.port);
|
|
1374
|
+
add("MONGO_USER", mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo");
|
|
1375
|
+
add("MONGO_PASSWORD", mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo");
|
|
1376
|
+
add("MONGO_DATABASE", mongo.env.MONGO_INITDB_DATABASE || config.name);
|
|
1057
1377
|
}
|
|
1058
1378
|
if (config.services.redis) {
|
|
1059
1379
|
const redis = config.services.redis;
|
|
1060
|
-
|
|
1061
|
-
|
|
1380
|
+
add("REDIS_HOST", redis.name);
|
|
1381
|
+
add("REDIS_PORT", redis.port);
|
|
1382
|
+
if (redis.env.REDIS_PASSWORD) {
|
|
1383
|
+
add("REDIS_PASSWORD", redis.env.REDIS_PASSWORD);
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
for (const name of config.app.env) {
|
|
1387
|
+
if (!env.some((entry) => entry.startsWith(`${name}=`))) {
|
|
1388
|
+
env.push(name);
|
|
1389
|
+
}
|
|
1062
1390
|
}
|
|
1063
1391
|
return env;
|
|
1064
1392
|
}
|
|
1065
|
-
function generateServiceConfig(
|
|
1393
|
+
function generateServiceConfig(config) {
|
|
1394
|
+
const environment = {};
|
|
1395
|
+
for (const [key, value] of Object.entries(config.env)) {
|
|
1396
|
+
environment[key] = composeLiteral(value);
|
|
1397
|
+
}
|
|
1398
|
+
const portMappings = [`${config.hostPort ?? config.port}:${config.port}`];
|
|
1399
|
+
for (const extraPort of config.ports ?? []) {
|
|
1400
|
+
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1401
|
+
}
|
|
1066
1402
|
const service = {
|
|
1067
1403
|
image: config.image,
|
|
1068
|
-
ports:
|
|
1069
|
-
environment
|
|
1404
|
+
ports: portMappings,
|
|
1405
|
+
environment
|
|
1070
1406
|
};
|
|
1071
|
-
const portMappings = [];
|
|
1072
|
-
portMappings.push(`${config.hostPort ?? config.port}:${config.port}`);
|
|
1073
|
-
if (config.ports) {
|
|
1074
|
-
for (const extraPort of config.ports) {
|
|
1075
|
-
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
service.ports = portMappings;
|
|
1079
1407
|
if (config.volumes.length > 0) {
|
|
1080
1408
|
service.volumes = config.volumes;
|
|
1081
1409
|
}
|
|
@@ -1092,23 +1420,35 @@ function generateServiceConfig(name, config) {
|
|
|
1092
1420
|
}
|
|
1093
1421
|
return service;
|
|
1094
1422
|
}
|
|
1095
|
-
function generateVolumeDefinitions(
|
|
1423
|
+
function generateVolumeDefinitions(config) {
|
|
1096
1424
|
const volumes = {};
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
}
|
|
1425
|
+
const specs = [
|
|
1426
|
+
...config.app.volumes,
|
|
1427
|
+
...Object.values(config.services).flatMap((service) => service.volumes)
|
|
1428
|
+
];
|
|
1429
|
+
for (const spec of specs) {
|
|
1430
|
+
const name = namedVolume(spec);
|
|
1431
|
+
if (name) {
|
|
1432
|
+
volumes[name] = {};
|
|
1105
1433
|
}
|
|
1106
1434
|
}
|
|
1107
1435
|
return volumes;
|
|
1108
1436
|
}
|
|
1109
1437
|
|
|
1110
1438
|
// src/generators/dockerignore.ts
|
|
1111
|
-
|
|
1439
|
+
var DOCKERFILE_IGNORE_FILE = "Dockerfile.dockerignore";
|
|
1440
|
+
function dockerignoreFileName(outputDir) {
|
|
1441
|
+
return outputSubdir(outputDir) === void 0 ? ".dockerignore" : DOCKERFILE_IGNORE_FILE;
|
|
1442
|
+
}
|
|
1443
|
+
function outputDirPatterns(outputDir) {
|
|
1444
|
+
const subdir = outputSubdir(outputDir);
|
|
1445
|
+
if (subdir === void 0) {
|
|
1446
|
+
return "";
|
|
1447
|
+
}
|
|
1448
|
+
return ["Dockerfile", "docker-compose.yml", DOCKERFILE_IGNORE_FILE, ".env", ".env.*"].map((file) => `${subdir}/${file}
|
|
1449
|
+
`).join("");
|
|
1450
|
+
}
|
|
1451
|
+
function generateDockerignore(config) {
|
|
1112
1452
|
return `# Dependencies
|
|
1113
1453
|
node_modules/
|
|
1114
1454
|
npm-debug.log*
|
|
@@ -1116,10 +1456,10 @@ yarn-debug.log*
|
|
|
1116
1456
|
yarn-error.log*
|
|
1117
1457
|
pnpm-debug.log*
|
|
1118
1458
|
|
|
1119
|
-
# Environment
|
|
1459
|
+
# Environment (secrets must never be copied into the image)
|
|
1120
1460
|
.env
|
|
1121
|
-
.env
|
|
1122
|
-
.
|
|
1461
|
+
.env.*
|
|
1462
|
+
!.env.example
|
|
1123
1463
|
|
|
1124
1464
|
# Git
|
|
1125
1465
|
.git/
|
|
@@ -1170,7 +1510,7 @@ temp/
|
|
|
1170
1510
|
Dockerfile
|
|
1171
1511
|
docker-compose.yml
|
|
1172
1512
|
.dockerignore
|
|
1173
|
-
|
|
1513
|
+
${outputDirPatterns(config.output.dir)}
|
|
1174
1514
|
# Package manager lockfiles (optional - uncomment if you want to exclude)
|
|
1175
1515
|
# package-lock.json
|
|
1176
1516
|
# yarn.lock
|
|
@@ -1179,6 +1519,17 @@ docker-compose.yml
|
|
|
1179
1519
|
}
|
|
1180
1520
|
|
|
1181
1521
|
// src/generators/env.ts
|
|
1522
|
+
var BUILT_IN_SERVICES = /* @__PURE__ */ new Set(["postgres", "mysql", "mongo", "redis"]);
|
|
1523
|
+
function envValue(value) {
|
|
1524
|
+
if (/^[\w@%+=:,./?&-]*$/.test(value)) {
|
|
1525
|
+
return value;
|
|
1526
|
+
}
|
|
1527
|
+
if (!/['\r\n]/.test(value)) {
|
|
1528
|
+
return `'${value}'`;
|
|
1529
|
+
}
|
|
1530
|
+
const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
|
1531
|
+
return `"${escaped}"`;
|
|
1532
|
+
}
|
|
1182
1533
|
function generateEnvExample(config) {
|
|
1183
1534
|
let content = `# Application Configuration
|
|
1184
1535
|
NODE_ENV=production
|
|
@@ -1198,17 +1549,21 @@ PORT=${config.app.port}
|
|
|
1198
1549
|
content += `
|
|
1199
1550
|
# PostgreSQL
|
|
1200
1551
|
`;
|
|
1201
|
-
|
|
1552
|
+
if (hasDatabaseUrlAlias(config)) {
|
|
1553
|
+
content += `DATABASE_URL=${envValue(String(pg.connectionString || ""))}
|
|
1202
1554
|
`;
|
|
1203
|
-
|
|
1555
|
+
}
|
|
1556
|
+
content += `POSTGRES_URL=${envValue(String(pg.connectionString || ""))}
|
|
1557
|
+
`;
|
|
1558
|
+
content += `POSTGRES_HOST=${envValue(String(pg.name))}
|
|
1204
1559
|
`;
|
|
1205
|
-
content += `POSTGRES_PORT=${pg.port}
|
|
1560
|
+
content += `POSTGRES_PORT=${envValue(String(pg.port))}
|
|
1206
1561
|
`;
|
|
1207
|
-
content += `POSTGRES_USER=${pg.env.POSTGRES_USER || "postgres"}
|
|
1562
|
+
content += `POSTGRES_USER=${envValue(String(pg.env.POSTGRES_USER || "postgres"))}
|
|
1208
1563
|
`;
|
|
1209
|
-
content += `POSTGRES_PASSWORD=${pg.env.POSTGRES_PASSWORD || "postgres"}
|
|
1564
|
+
content += `POSTGRES_PASSWORD=${envValue(String(pg.env.POSTGRES_PASSWORD || "postgres"))}
|
|
1210
1565
|
`;
|
|
1211
|
-
content += `POSTGRES_DB=${pg.env.POSTGRES_DB || config.name}
|
|
1566
|
+
content += `POSTGRES_DB=${envValue(String(pg.env.POSTGRES_DB || config.name))}
|
|
1212
1567
|
`;
|
|
1213
1568
|
}
|
|
1214
1569
|
if (config.services.mysql) {
|
|
@@ -1216,17 +1571,17 @@ PORT=${config.app.port}
|
|
|
1216
1571
|
content += `
|
|
1217
1572
|
# MySQL
|
|
1218
1573
|
`;
|
|
1219
|
-
content += `MYSQL_URL=${mysql.connectionString || ""}
|
|
1574
|
+
content += `MYSQL_URL=${envValue(String(mysql.connectionString || ""))}
|
|
1220
1575
|
`;
|
|
1221
|
-
content += `MYSQL_HOST=${mysql.name}
|
|
1576
|
+
content += `MYSQL_HOST=${envValue(String(mysql.name))}
|
|
1222
1577
|
`;
|
|
1223
|
-
content += `MYSQL_PORT=${mysql.port}
|
|
1578
|
+
content += `MYSQL_PORT=${envValue(String(mysql.port))}
|
|
1224
1579
|
`;
|
|
1225
|
-
content += `MYSQL_USER=${mysql.env.MYSQL_USER || "root"}
|
|
1580
|
+
content += `MYSQL_USER=${envValue(String(mysql.env.MYSQL_USER || "root"))}
|
|
1226
1581
|
`;
|
|
1227
|
-
content += `MYSQL_PASSWORD=${mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"}
|
|
1582
|
+
content += `MYSQL_PASSWORD=${envValue(String(mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"))}
|
|
1228
1583
|
`;
|
|
1229
|
-
content += `MYSQL_DATABASE=${mysql.env.MYSQL_DATABASE || config.name}
|
|
1584
|
+
content += `MYSQL_DATABASE=${envValue(String(mysql.env.MYSQL_DATABASE || config.name))}
|
|
1230
1585
|
`;
|
|
1231
1586
|
}
|
|
1232
1587
|
if (config.services.mongo) {
|
|
@@ -1234,17 +1589,17 @@ PORT=${config.app.port}
|
|
|
1234
1589
|
content += `
|
|
1235
1590
|
# MongoDB
|
|
1236
1591
|
`;
|
|
1237
|
-
content += `MONGO_URL=${mongo.connectionString || ""}
|
|
1592
|
+
content += `MONGO_URL=${envValue(String(mongo.connectionString || ""))}
|
|
1238
1593
|
`;
|
|
1239
|
-
content += `MONGO_HOST=${mongo.name}
|
|
1594
|
+
content += `MONGO_HOST=${envValue(String(mongo.name))}
|
|
1240
1595
|
`;
|
|
1241
|
-
content += `MONGO_PORT=${mongo.port}
|
|
1596
|
+
content += `MONGO_PORT=${envValue(String(mongo.port))}
|
|
1242
1597
|
`;
|
|
1243
|
-
content += `MONGO_USER=${mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"}
|
|
1598
|
+
content += `MONGO_USER=${envValue(String(mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"))}
|
|
1244
1599
|
`;
|
|
1245
|
-
content += `MONGO_PASSWORD=${mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"}
|
|
1600
|
+
content += `MONGO_PASSWORD=${envValue(String(mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"))}
|
|
1246
1601
|
`;
|
|
1247
|
-
content += `MONGO_DATABASE=${mongo.env.MONGO_INITDB_DATABASE || config.name}
|
|
1602
|
+
content += `MONGO_DATABASE=${envValue(String(mongo.env.MONGO_INITDB_DATABASE || config.name))}
|
|
1248
1603
|
`;
|
|
1249
1604
|
}
|
|
1250
1605
|
if (config.services.redis) {
|
|
@@ -1252,14 +1607,23 @@ PORT=${config.app.port}
|
|
|
1252
1607
|
content += `
|
|
1253
1608
|
# Redis
|
|
1254
1609
|
`;
|
|
1255
|
-
content += `REDIS_URL=${redis.connectionString || ""}
|
|
1610
|
+
content += `REDIS_URL=${envValue(String(redis.connectionString || ""))}
|
|
1256
1611
|
`;
|
|
1257
|
-
content += `REDIS_HOST=${redis.name}
|
|
1612
|
+
content += `REDIS_HOST=${envValue(String(redis.name))}
|
|
1258
1613
|
`;
|
|
1259
|
-
content += `REDIS_PORT=${redis.port}
|
|
1614
|
+
content += `REDIS_PORT=${envValue(String(redis.port))}
|
|
1260
1615
|
`;
|
|
1261
1616
|
if (redis.env.REDIS_PASSWORD) {
|
|
1262
|
-
content += `REDIS_PASSWORD=${redis.env.REDIS_PASSWORD}
|
|
1617
|
+
content += `REDIS_PASSWORD=${envValue(String(redis.env.REDIS_PASSWORD))}
|
|
1618
|
+
`;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
for (const [serviceName, service] of Object.entries(config.services)) {
|
|
1622
|
+
if (!BUILT_IN_SERVICES.has(serviceName) && service.connectionString) {
|
|
1623
|
+
content += `
|
|
1624
|
+
# ${serviceName}
|
|
1625
|
+
`;
|
|
1626
|
+
content += `${connectionStringVar(serviceName)}=${envValue(service.connectionString)}
|
|
1263
1627
|
`;
|
|
1264
1628
|
}
|
|
1265
1629
|
}
|
|
@@ -1274,7 +1638,7 @@ function generateBuildScript(_config) {
|
|
|
1274
1638
|
set -e
|
|
1275
1639
|
|
|
1276
1640
|
echo "Building Docker image..."
|
|
1277
|
-
docker
|
|
1641
|
+
docker compose build
|
|
1278
1642
|
|
|
1279
1643
|
echo "Build complete!"
|
|
1280
1644
|
`;
|
|
@@ -1287,7 +1651,7 @@ function generateUpScript(config) {
|
|
|
1287
1651
|
set -e
|
|
1288
1652
|
|
|
1289
1653
|
echo "Starting services..."
|
|
1290
|
-
docker
|
|
1654
|
+
docker compose up -d ${services2.join(" ")}
|
|
1291
1655
|
|
|
1292
1656
|
echo "Services started!"
|
|
1293
1657
|
echo ""
|
|
@@ -1301,7 +1665,7 @@ function generateDownScript(_config) {
|
|
|
1301
1665
|
set -e
|
|
1302
1666
|
|
|
1303
1667
|
echo "Stopping services..."
|
|
1304
|
-
docker
|
|
1668
|
+
docker compose down
|
|
1305
1669
|
|
|
1306
1670
|
echo "Services stopped!"
|
|
1307
1671
|
`;
|
|
@@ -1313,9 +1677,9 @@ function generateLogsScript(_config) {
|
|
|
1313
1677
|
set -e
|
|
1314
1678
|
|
|
1315
1679
|
if [ -n "$1" ]; then
|
|
1316
|
-
docker
|
|
1680
|
+
docker compose logs -f "$1"
|
|
1317
1681
|
else
|
|
1318
|
-
docker
|
|
1682
|
+
docker compose logs -f
|
|
1319
1683
|
fi
|
|
1320
1684
|
`;
|
|
1321
1685
|
}
|
|
@@ -1342,28 +1706,28 @@ help:
|
|
|
1342
1706
|
@echo " make restart - Restart all services"
|
|
1343
1707
|
@echo " make logs - Show logs from all services"
|
|
1344
1708
|
@echo " make shell - Open shell in app container"
|
|
1345
|
-
@echo " make clean - Remove containers and volumes"
|
|
1709
|
+
@echo " make clean - Remove this project's containers and volumes"
|
|
1346
1710
|
@echo " make install - Install dependencies"
|
|
1347
1711
|
|
|
1348
1712
|
# Build Docker images
|
|
1349
1713
|
build:
|
|
1350
|
-
docker
|
|
1714
|
+
docker compose build
|
|
1351
1715
|
|
|
1352
1716
|
# Start all services
|
|
1353
1717
|
up:
|
|
1354
|
-
docker
|
|
1718
|
+
docker compose up -d
|
|
1355
1719
|
@echo "App running at http://localhost:${config.app.port}"
|
|
1356
1720
|
|
|
1357
1721
|
# Stop all services
|
|
1358
1722
|
down:
|
|
1359
|
-
docker
|
|
1723
|
+
docker compose down
|
|
1360
1724
|
|
|
1361
1725
|
# Restart all services
|
|
1362
1726
|
restart: down up
|
|
1363
1727
|
|
|
1364
1728
|
# Show logs from all services
|
|
1365
1729
|
logs:
|
|
1366
|
-
docker
|
|
1730
|
+
docker compose logs -f
|
|
1367
1731
|
`;
|
|
1368
1732
|
if (services2.length > 0) {
|
|
1369
1733
|
content += "\n";
|
|
@@ -1371,23 +1735,22 @@ logs:
|
|
|
1371
1735
|
content += `
|
|
1372
1736
|
logs-${service}:
|
|
1373
1737
|
`;
|
|
1374
|
-
content += ` docker
|
|
1738
|
+
content += ` docker compose logs -f ${service}
|
|
1375
1739
|
`;
|
|
1376
1740
|
}
|
|
1377
1741
|
}
|
|
1378
1742
|
content += `
|
|
1379
|
-
#
|
|
1743
|
+
# Remove this project's containers, networks and volumes
|
|
1380
1744
|
clean:
|
|
1381
|
-
docker
|
|
1382
|
-
docker system prune -f
|
|
1745
|
+
docker compose down -v --remove-orphans
|
|
1383
1746
|
|
|
1384
1747
|
# Install dependencies (in running container)
|
|
1385
1748
|
shell:
|
|
1386
|
-
docker
|
|
1749
|
+
docker compose exec app sh
|
|
1387
1750
|
|
|
1388
1751
|
# Install dependencies
|
|
1389
1752
|
install:
|
|
1390
|
-
docker
|
|
1753
|
+
docker compose exec app npm install
|
|
1391
1754
|
`;
|
|
1392
1755
|
return content;
|
|
1393
1756
|
}
|
|
@@ -1414,7 +1777,8 @@ function generateAll(config, context) {
|
|
|
1414
1777
|
} else {
|
|
1415
1778
|
const generator = GENERATORS[fileType];
|
|
1416
1779
|
if (generator) {
|
|
1417
|
-
|
|
1780
|
+
const file = fileType === "dockerignore" ? dockerignoreFileName(config.output.dir) : generator.file;
|
|
1781
|
+
result[file] = generator.generate(config, context);
|
|
1418
1782
|
}
|
|
1419
1783
|
}
|
|
1420
1784
|
}
|
|
@@ -1423,7 +1787,7 @@ function generateAll(config, context) {
|
|
|
1423
1787
|
|
|
1424
1788
|
// src/utils/fs.ts
|
|
1425
1789
|
var import_fs = require("fs");
|
|
1426
|
-
var
|
|
1790
|
+
var import_path2 = require("path");
|
|
1427
1791
|
async function ensureDir(path) {
|
|
1428
1792
|
try {
|
|
1429
1793
|
await import_fs.promises.mkdir(path, { recursive: true });
|
|
@@ -1434,18 +1798,27 @@ async function ensureDir(path) {
|
|
|
1434
1798
|
}
|
|
1435
1799
|
}
|
|
1436
1800
|
}
|
|
1437
|
-
async function writeFile(filePath, content) {
|
|
1438
|
-
|
|
1801
|
+
async function writeFile(filePath, content, mode) {
|
|
1802
|
+
try {
|
|
1803
|
+
await writeFileAtomic(filePath, content, mode);
|
|
1804
|
+
} catch (error) {
|
|
1805
|
+
throw new FileWriteError(filePath, "FILE_WRITE_ERROR", error);
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
async function writeFileAtomic(filePath, content, mode) {
|
|
1809
|
+
const dir = (0, import_path2.dirname)(filePath);
|
|
1439
1810
|
await ensureDir(dir);
|
|
1440
|
-
const
|
|
1811
|
+
const suffix = `${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2)}`;
|
|
1812
|
+
const tempPath = (0, import_path2.join)(dir, `.${(0, import_path2.basename)(filePath)}.${suffix}.tmp`);
|
|
1441
1813
|
try {
|
|
1442
|
-
await import_fs.promises.writeFile(tempPath, content, "utf8");
|
|
1814
|
+
await import_fs.promises.writeFile(tempPath, content, { encoding: "utf8", mode });
|
|
1815
|
+
if (mode !== void 0) {
|
|
1816
|
+
await import_fs.promises.chmod(tempPath, mode);
|
|
1817
|
+
}
|
|
1443
1818
|
await import_fs.promises.rename(tempPath, filePath);
|
|
1444
1819
|
} catch (error) {
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
} catch {
|
|
1448
|
-
}
|
|
1820
|
+
await import_fs.promises.unlink(tempPath).catch(() => {
|
|
1821
|
+
});
|
|
1449
1822
|
throw error;
|
|
1450
1823
|
}
|
|
1451
1824
|
}
|
|
@@ -1459,24 +1832,27 @@ async function fileExists(filePath) {
|
|
|
1459
1832
|
}
|
|
1460
1833
|
|
|
1461
1834
|
// src/core/dockcraft.ts
|
|
1462
|
-
var
|
|
1835
|
+
var import_path3 = require("path");
|
|
1836
|
+
function validationError(prefix, errors) {
|
|
1837
|
+
const errorMessages = errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1838
|
+
return new ValidationError(`${prefix}:
|
|
1839
|
+
${errorMessages}`, errors);
|
|
1840
|
+
}
|
|
1841
|
+
function isSafeOutputPath(fileName) {
|
|
1842
|
+
if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
|
|
1843
|
+
if ((0, import_path3.isAbsolute)(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
|
|
1844
|
+
const normalized = (0, import_path3.normalize)(fileName.replace(/\\/g, "/"));
|
|
1845
|
+
return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
|
|
1846
|
+
}
|
|
1463
1847
|
async function dockcraft(config, options = {}) {
|
|
1464
1848
|
const validation = validateConfig(config);
|
|
1465
1849
|
if (!validation.valid) {
|
|
1466
|
-
|
|
1467
|
-
throw new Error(
|
|
1468
|
-
`Configuration validation failed:
|
|
1469
|
-
${errorMessages}`
|
|
1470
|
-
);
|
|
1850
|
+
throw validationError("Configuration validation failed", validation.errors);
|
|
1471
1851
|
}
|
|
1472
1852
|
const resolved = await resolveConfig(config);
|
|
1473
1853
|
const resolvedValidation = validateResolvedConfig(resolved);
|
|
1474
1854
|
if (!resolvedValidation.valid) {
|
|
1475
|
-
|
|
1476
|
-
throw new Error(
|
|
1477
|
-
`Resolved configuration validation failed:
|
|
1478
|
-
${errorMessages}`
|
|
1479
|
-
);
|
|
1855
|
+
throw validationError("Resolved configuration validation failed", resolvedValidation.errors);
|
|
1480
1856
|
}
|
|
1481
1857
|
const context = {
|
|
1482
1858
|
config: resolved,
|
|
@@ -1484,22 +1860,38 @@ ${errorMessages}`
|
|
|
1484
1860
|
Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
|
|
1485
1861
|
),
|
|
1486
1862
|
strings: new Map(
|
|
1487
|
-
Object.entries(resolved.services).filter((
|
|
1863
|
+
Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
|
|
1488
1864
|
)
|
|
1489
1865
|
};
|
|
1490
1866
|
context.ports.set("app", resolved.app.port);
|
|
1491
1867
|
const generated = generateAll(resolved, context);
|
|
1868
|
+
for (const plugin of config.plugins ?? []) {
|
|
1869
|
+
for (const output of plugin.outputs ?? []) {
|
|
1870
|
+
if (!isSafeOutputPath(output.file)) {
|
|
1871
|
+
throw new ConfigError(
|
|
1872
|
+
`Plugin '${plugin.name}' output '${output.name}' has an unsafe file name '${output.file}'. Use a relative path inside the output directory.`,
|
|
1873
|
+
"UNSAFE_OUTPUT_PATH"
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
if (output.file in generated) {
|
|
1877
|
+
throw new ConfigError(
|
|
1878
|
+
`Plugin '${plugin.name}' output '${output.name}' would overwrite generated file '${output.file}'.`,
|
|
1879
|
+
"DUPLICATE_OUTPUT"
|
|
1880
|
+
);
|
|
1881
|
+
}
|
|
1882
|
+
generated[output.file] = output.generate(resolved, context);
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1492
1885
|
const written = [];
|
|
1493
1886
|
const outputDir = resolved.output.dir ?? "./";
|
|
1494
1887
|
if (!options.dryRun) {
|
|
1495
1888
|
for (const [fileName, content] of Object.entries(generated)) {
|
|
1496
|
-
|
|
1497
|
-
const filePath = (0, import_path2.join)(outputDir, fileName);
|
|
1889
|
+
const filePath = (0, import_path3.join)(outputDir, fileName);
|
|
1498
1890
|
const exists = await fileExists(filePath);
|
|
1499
1891
|
if (exists && !resolved.output.overwrite) {
|
|
1500
1892
|
continue;
|
|
1501
1893
|
}
|
|
1502
|
-
await writeFile(filePath, content);
|
|
1894
|
+
await writeFile(filePath, content, fileName.endsWith(".sh") ? 493 : void 0);
|
|
1503
1895
|
written.push(filePath);
|
|
1504
1896
|
}
|
|
1505
1897
|
}
|
|
@@ -1546,79 +1938,37 @@ function listDefaultPorts() {
|
|
|
1546
1938
|
return { ...DEFAULT_PORTS };
|
|
1547
1939
|
}
|
|
1548
1940
|
|
|
1549
|
-
// src/types.ts
|
|
1550
|
-
var DockcraftError = class extends Error {
|
|
1551
|
-
constructor(message, code) {
|
|
1552
|
-
super(message);
|
|
1553
|
-
this.code = code;
|
|
1554
|
-
this.name = "DockcraftError";
|
|
1555
|
-
}
|
|
1556
|
-
};
|
|
1557
|
-
var ConfigError = class extends DockcraftError {
|
|
1558
|
-
constructor(message, code = "CONFIG_ERROR") {
|
|
1559
|
-
super(message, code);
|
|
1560
|
-
this.name = "ConfigError";
|
|
1561
|
-
}
|
|
1562
|
-
};
|
|
1563
|
-
var ValidationError = class extends DockcraftError {
|
|
1564
|
-
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
1565
|
-
super(message, code);
|
|
1566
|
-
this.errors = errors;
|
|
1567
|
-
this.name = "ValidationError";
|
|
1568
|
-
}
|
|
1569
|
-
};
|
|
1570
|
-
var PortConflictError = class extends DockcraftError {
|
|
1571
|
-
constructor(port, code = "PORT_CONFLICT") {
|
|
1572
|
-
super(`Port ${port} is already in use`, code);
|
|
1573
|
-
this.name = "PortConflictError";
|
|
1574
|
-
}
|
|
1575
|
-
};
|
|
1576
|
-
var NoPortAvailableError = class extends DockcraftError {
|
|
1577
|
-
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
1578
|
-
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
1579
|
-
this.name = "NoPortAvailableError";
|
|
1580
|
-
}
|
|
1581
|
-
};
|
|
1582
|
-
var FileWriteError = class extends DockcraftError {
|
|
1583
|
-
constructor(path, code = "FILE_WRITE_ERROR") {
|
|
1584
|
-
super(`Failed to write file: ${path}`, code);
|
|
1585
|
-
this.name = "FileWriteError";
|
|
1586
|
-
}
|
|
1587
|
-
};
|
|
1588
|
-
var PresetNotFoundError = class extends DockcraftError {
|
|
1589
|
-
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
1590
|
-
super(`Preset '${name}' not found`, code);
|
|
1591
|
-
this.name = "PresetNotFoundError";
|
|
1592
|
-
}
|
|
1593
|
-
};
|
|
1594
|
-
var ServiceNotFoundError = class extends DockcraftError {
|
|
1595
|
-
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
1596
|
-
super(`Service '${name}' not found`, code);
|
|
1597
|
-
this.name = "ServiceNotFoundError";
|
|
1598
|
-
}
|
|
1599
|
-
};
|
|
1600
|
-
|
|
1601
1941
|
// src/index.ts
|
|
1602
1942
|
function getConnectionString(service, config) {
|
|
1943
|
+
const enc = encodeURIComponent;
|
|
1944
|
+
const { host, port } = config;
|
|
1603
1945
|
switch (service) {
|
|
1604
|
-
case "postgres":
|
|
1605
|
-
const
|
|
1606
|
-
const
|
|
1607
|
-
const
|
|
1608
|
-
return `postgres://${
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
const
|
|
1612
|
-
const
|
|
1613
|
-
|
|
1946
|
+
case "postgres": {
|
|
1947
|
+
const user = config.user ?? "postgres";
|
|
1948
|
+
const password = config.password ?? "postgres";
|
|
1949
|
+
const database = config.database ?? "myapp";
|
|
1950
|
+
return `postgres://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}`;
|
|
1951
|
+
}
|
|
1952
|
+
case "mysql": {
|
|
1953
|
+
const user = config.user ?? "root";
|
|
1954
|
+
const password = config.password ?? "root";
|
|
1955
|
+
const database = config.database ?? "myapp";
|
|
1956
|
+
return `mysql://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}`;
|
|
1957
|
+
}
|
|
1614
1958
|
case "mongo":
|
|
1615
|
-
case "mongodb":
|
|
1616
|
-
const
|
|
1617
|
-
const
|
|
1618
|
-
const
|
|
1619
|
-
return `mongodb://${
|
|
1620
|
-
|
|
1621
|
-
|
|
1959
|
+
case "mongodb": {
|
|
1960
|
+
const user = config.user ?? "mongo";
|
|
1961
|
+
const password = config.password ?? "mongo";
|
|
1962
|
+
const database = config.database ?? "myapp";
|
|
1963
|
+
return `mongodb://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}?authSource=admin`;
|
|
1964
|
+
}
|
|
1965
|
+
case "redis": {
|
|
1966
|
+
if (config.password === void 0 || config.password === "") {
|
|
1967
|
+
return `redis://${host}:${port}`;
|
|
1968
|
+
}
|
|
1969
|
+
const user = config.user ? enc(config.user) : "";
|
|
1970
|
+
return `redis://${user}:${enc(config.password)}@${host}:${port}`;
|
|
1971
|
+
}
|
|
1622
1972
|
default:
|
|
1623
1973
|
throw new Error(`Unknown service: ${service}`);
|
|
1624
1974
|
}
|