@oxog/dockcraft 1.0.0 → 1.1.0
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 +183 -0
- package/README.md +112 -20
- package/dist/cli/index.js +805 -434
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +656 -379
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -11
- package/dist/index.d.ts +26 -11
- package/dist/index.js +654 -377
- 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.js
CHANGED
|
@@ -1,8 +1,60 @@
|
|
|
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
|
+
|
|
7
|
+
// src/types.ts
|
|
8
|
+
var DockcraftError = class extends Error {
|
|
9
|
+
constructor(message, code) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.name = "DockcraftError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var ConfigError = class extends DockcraftError {
|
|
16
|
+
constructor(message, code = "CONFIG_ERROR") {
|
|
17
|
+
super(message, code);
|
|
18
|
+
this.name = "ConfigError";
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var ValidationError = class extends DockcraftError {
|
|
22
|
+
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
23
|
+
super(message, code);
|
|
24
|
+
this.errors = errors;
|
|
25
|
+
this.name = "ValidationError";
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var PortConflictError = class extends DockcraftError {
|
|
29
|
+
constructor(port, code = "PORT_CONFLICT") {
|
|
30
|
+
super(`Port ${port} is already in use`, code);
|
|
31
|
+
this.name = "PortConflictError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var NoPortAvailableError = class extends DockcraftError {
|
|
35
|
+
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
36
|
+
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
37
|
+
this.name = "NoPortAvailableError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var FileWriteError = class extends DockcraftError {
|
|
41
|
+
constructor(path, code = "FILE_WRITE_ERROR") {
|
|
42
|
+
super(`Failed to write file: ${path}`, code);
|
|
43
|
+
this.name = "FileWriteError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var PresetNotFoundError = class extends DockcraftError {
|
|
47
|
+
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
48
|
+
super(`Preset '${name}' not found`, code);
|
|
49
|
+
this.name = "PresetNotFoundError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var ServiceNotFoundError = class extends DockcraftError {
|
|
53
|
+
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
54
|
+
super(`Service '${name}' not found`, code);
|
|
55
|
+
this.name = "ServiceNotFoundError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
6
58
|
|
|
7
59
|
// src/presets/express-postgres.ts
|
|
8
60
|
var expressPostgresPreset = {
|
|
@@ -242,10 +294,24 @@ registerPreset("nest-postgres", nestPostgresPreset);
|
|
|
242
294
|
registerPreset("nest-mongo", nestMongoPreset);
|
|
243
295
|
registerPreset("node-minimal", nodeMinimalPreset);
|
|
244
296
|
|
|
297
|
+
// src/services/image.ts
|
|
298
|
+
function variantImage(repository, version, variant) {
|
|
299
|
+
if (version === "latest") {
|
|
300
|
+
return `${repository}:${variant}`;
|
|
301
|
+
}
|
|
302
|
+
if (version === variant || version.includes("-")) {
|
|
303
|
+
return `${repository}:${version}`;
|
|
304
|
+
}
|
|
305
|
+
return `${repository}:${version}-${variant}`;
|
|
306
|
+
}
|
|
307
|
+
function urlPart(value) {
|
|
308
|
+
return encodeURIComponent(value);
|
|
309
|
+
}
|
|
310
|
+
|
|
245
311
|
// src/services/postgres.ts
|
|
246
312
|
var postgresService = {
|
|
247
313
|
name: "postgres",
|
|
248
|
-
image: (version) =>
|
|
314
|
+
image: (version) => variantImage("postgres", version, "alpine"),
|
|
249
315
|
defaultPort: 5432,
|
|
250
316
|
defaultVersion: "16",
|
|
251
317
|
env: {
|
|
@@ -262,7 +328,7 @@ var postgresService = {
|
|
|
262
328
|
const database = config.env.POSTGRES_DB || "myapp";
|
|
263
329
|
const host = config.name;
|
|
264
330
|
const port = config.port;
|
|
265
|
-
return `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
331
|
+
return `postgres://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
266
332
|
}
|
|
267
333
|
};
|
|
268
334
|
|
|
@@ -287,7 +353,7 @@ var mysqlService = {
|
|
|
287
353
|
const database = config.env.MYSQL_DATABASE || "myapp";
|
|
288
354
|
const host = config.name;
|
|
289
355
|
const port = config.port;
|
|
290
|
-
return `mysql://${user}:${password}@${host}:${port}/${database}`;
|
|
356
|
+
return `mysql://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}`;
|
|
291
357
|
}
|
|
292
358
|
};
|
|
293
359
|
|
|
@@ -311,14 +377,14 @@ var mongoService = {
|
|
|
311
377
|
const database = config.env.MONGO_INITDB_DATABASE || "myapp";
|
|
312
378
|
const host = config.name;
|
|
313
379
|
const port = config.port;
|
|
314
|
-
return `mongodb://${user}:${password}@${host}:${port}/${database}`;
|
|
380
|
+
return `mongodb://${urlPart(user)}:${urlPart(password)}@${host}:${port}/${urlPart(database)}?authSource=admin`;
|
|
315
381
|
}
|
|
316
382
|
};
|
|
317
383
|
|
|
318
384
|
// src/services/redis.ts
|
|
319
385
|
var redisService = {
|
|
320
386
|
name: "redis",
|
|
321
|
-
image: (version) =>
|
|
387
|
+
image: (version) => variantImage("redis", version, "alpine"),
|
|
322
388
|
defaultPort: 6379,
|
|
323
389
|
defaultVersion: "7",
|
|
324
390
|
env: {},
|
|
@@ -328,7 +394,9 @@ var redisService = {
|
|
|
328
394
|
connectionString: (config) => {
|
|
329
395
|
const host = config.name;
|
|
330
396
|
const port = config.port;
|
|
331
|
-
|
|
397
|
+
const password = config.env.REDIS_PASSWORD;
|
|
398
|
+
const auth = password ? `:${urlPart(password)}@` : "";
|
|
399
|
+
return `redis://${auth}${host}:${port}`;
|
|
332
400
|
}
|
|
333
401
|
};
|
|
334
402
|
|
|
@@ -349,12 +417,13 @@ registerService(mongoService);
|
|
|
349
417
|
registerService(redisService);
|
|
350
418
|
|
|
351
419
|
// src/utils/merge.ts
|
|
420
|
+
var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
352
421
|
function deepMerge(...objects) {
|
|
353
422
|
const result = {};
|
|
354
423
|
for (const obj of objects) {
|
|
355
424
|
if (obj === null || typeof obj !== "object") continue;
|
|
356
425
|
for (const [key, value] of Object.entries(obj)) {
|
|
357
|
-
if (value === void 0) continue;
|
|
426
|
+
if (value === void 0 || UNSAFE_KEYS.has(key)) continue;
|
|
358
427
|
if (value && typeof value === "object" && !Array.isArray(value) && result[key] && typeof result[key] === "object" && !Array.isArray(result[key])) {
|
|
359
428
|
result[key] = deepMerge(result[key], value);
|
|
360
429
|
} else {
|
|
@@ -368,21 +437,30 @@ function deepMerge(...objects) {
|
|
|
368
437
|
// src/ports/scanner.ts
|
|
369
438
|
import { createServer } from "net";
|
|
370
439
|
async function isPortAvailable(port) {
|
|
440
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
441
|
+
throw new RangeError(`Invalid port: ${String(port)}. Must be an integer between 1 and 65535.`);
|
|
442
|
+
}
|
|
371
443
|
return new Promise((resolve) => {
|
|
372
444
|
const server = createServer();
|
|
445
|
+
let settled = false;
|
|
446
|
+
const finish = (available) => {
|
|
447
|
+
if (settled) return;
|
|
448
|
+
settled = true;
|
|
449
|
+
clearTimeout(timer);
|
|
450
|
+
resolve(available);
|
|
451
|
+
};
|
|
452
|
+
const timer = setTimeout(() => {
|
|
453
|
+
server.close();
|
|
454
|
+
finish(false);
|
|
455
|
+
}, 1e3);
|
|
456
|
+
timer.unref();
|
|
373
457
|
server.once("error", () => {
|
|
374
|
-
|
|
458
|
+
finish(false);
|
|
375
459
|
});
|
|
376
460
|
server.once("listening", () => {
|
|
377
|
-
server.close(() =>
|
|
378
|
-
resolve(true);
|
|
379
|
-
});
|
|
461
|
+
server.close(() => finish(true));
|
|
380
462
|
});
|
|
381
463
|
server.listen(port, "127.0.0.1");
|
|
382
|
-
setTimeout(() => {
|
|
383
|
-
server.close();
|
|
384
|
-
resolve(false);
|
|
385
|
-
}, 1e3);
|
|
386
464
|
});
|
|
387
465
|
}
|
|
388
466
|
async function checkPorts(ports) {
|
|
@@ -425,20 +503,21 @@ async function resolvePort(options = {}) {
|
|
|
425
503
|
case "range":
|
|
426
504
|
if (range) {
|
|
427
505
|
const [start, end = start + 100] = range;
|
|
428
|
-
const
|
|
429
|
-
if (
|
|
430
|
-
return
|
|
506
|
+
const port = await findAvailablePortInRange(start, end);
|
|
507
|
+
if (port !== void 0) {
|
|
508
|
+
return port;
|
|
431
509
|
}
|
|
432
510
|
throw new Error(`No available port in range ${start}-${end}`);
|
|
433
511
|
}
|
|
434
512
|
// Fall through to auto if no range specified
|
|
435
513
|
case "auto":
|
|
436
|
-
default:
|
|
514
|
+
default: {
|
|
437
515
|
const port = await findNextAvailablePort(preferred);
|
|
438
516
|
if (port !== void 0) {
|
|
439
517
|
return port;
|
|
440
518
|
}
|
|
441
519
|
throw new Error(`No available port found starting from ${preferred}`);
|
|
520
|
+
}
|
|
442
521
|
}
|
|
443
522
|
}
|
|
444
523
|
|
|
@@ -453,9 +532,9 @@ var DEFAULT_APP_CONFIG = {
|
|
|
453
532
|
volumes: [],
|
|
454
533
|
multiStage: true
|
|
455
534
|
};
|
|
456
|
-
function normalizeAppConfig(config) {
|
|
535
|
+
function normalizeAppConfig(config = {}) {
|
|
457
536
|
return {
|
|
458
|
-
port: config.port === "
|
|
537
|
+
port: typeof config.port === "number" ? config.port : DEFAULT_APP_CONFIG.port,
|
|
459
538
|
node: config.node ?? DEFAULT_APP_CONFIG.node,
|
|
460
539
|
workdir: config.workdir ?? DEFAULT_APP_CONFIG.workdir,
|
|
461
540
|
command: config.command ?? DEFAULT_APP_CONFIG.command,
|
|
@@ -477,7 +556,7 @@ function normalizeConfig(config) {
|
|
|
477
556
|
return {
|
|
478
557
|
name: config.name,
|
|
479
558
|
preset: config.preset,
|
|
480
|
-
app: config.app,
|
|
559
|
+
app: config.app ?? {},
|
|
481
560
|
services: config.services ?? {},
|
|
482
561
|
output: normalizeOutputConfig(config.output),
|
|
483
562
|
plugins: config.plugins ?? []
|
|
@@ -485,10 +564,11 @@ function normalizeConfig(config) {
|
|
|
485
564
|
}
|
|
486
565
|
|
|
487
566
|
// src/core/resolver.ts
|
|
567
|
+
var REDIS_AUTH_COMMAND = `sh -c 'exec redis-server --requirepass "$$REDIS_PASSWORD"'`;
|
|
488
568
|
function mergePreset(config, presetName) {
|
|
489
569
|
const preset = getPreset(presetName);
|
|
490
570
|
if (!preset) {
|
|
491
|
-
throw new
|
|
571
|
+
throw new PresetNotFoundError(presetName);
|
|
492
572
|
}
|
|
493
573
|
const merged = deepMerge(
|
|
494
574
|
{
|
|
@@ -501,55 +581,65 @@ function mergePreset(config, presetName) {
|
|
|
501
581
|
);
|
|
502
582
|
return merged;
|
|
503
583
|
}
|
|
504
|
-
async function
|
|
584
|
+
async function resolveServicePorts(portConfig, containerPort) {
|
|
585
|
+
if (portConfig === "auto") {
|
|
586
|
+
return {
|
|
587
|
+
port: containerPort,
|
|
588
|
+
hostPort: await resolvePort({ preferred: containerPort, strategy: "auto" })
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
if (typeof portConfig === "number") {
|
|
592
|
+
return { port: containerPort, hostPort: portConfig };
|
|
593
|
+
}
|
|
594
|
+
if (portConfig && typeof portConfig === "object") {
|
|
595
|
+
const startPort = portConfig.start ?? containerPort;
|
|
596
|
+
const endPort = portConfig.end ?? startPort + 100;
|
|
597
|
+
return {
|
|
598
|
+
port: containerPort,
|
|
599
|
+
hostPort: await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] })
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
return { port: containerPort };
|
|
603
|
+
}
|
|
604
|
+
async function resolveServiceConfig(name, config, projectName, definitions) {
|
|
505
605
|
if ("image" in config && config.image) {
|
|
506
606
|
return resolveCustomService(name, config);
|
|
507
607
|
}
|
|
508
|
-
const definition = getService(name);
|
|
608
|
+
const definition = definitions?.get(name) ?? getService(name);
|
|
509
609
|
if (!definition) {
|
|
510
|
-
throw new
|
|
511
|
-
}
|
|
512
|
-
const defaultPort = definition.defaultPort;
|
|
513
|
-
let port = defaultPort;
|
|
514
|
-
let hostPort;
|
|
515
|
-
if (config.port === "auto") {
|
|
516
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
517
|
-
hostPort = port;
|
|
518
|
-
} else if (typeof config.port === "number") {
|
|
519
|
-
port = config.port;
|
|
520
|
-
hostPort = config.port;
|
|
521
|
-
} else if (config.port && typeof config.port === "object") {
|
|
522
|
-
const { start, end } = config.port;
|
|
523
|
-
const startPort = start ?? defaultPort;
|
|
524
|
-
const endPort = end ?? startPort + 100;
|
|
525
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
526
|
-
hostPort = port;
|
|
610
|
+
throw new ServiceNotFoundError(name);
|
|
527
611
|
}
|
|
612
|
+
const { port, hostPort } = await resolveServicePorts(config.port, definition.defaultPort);
|
|
528
613
|
const version = config.version ?? definition.defaultVersion;
|
|
529
614
|
const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
|
|
530
615
|
const env = { ...definition.env, ...config.env };
|
|
616
|
+
let command = config.command;
|
|
531
617
|
if (name === "postgres") {
|
|
532
618
|
const pgConfig = config;
|
|
533
619
|
if (pgConfig.database) env.POSTGRES_DB = pgConfig.database;
|
|
534
620
|
if (pgConfig.user) env.POSTGRES_USER = pgConfig.user;
|
|
535
621
|
if (pgConfig.password) env.POSTGRES_PASSWORD = pgConfig.password;
|
|
536
|
-
if (!env
|
|
622
|
+
if (!pgConfig.database && !config.env?.POSTGRES_DB) env.POSTGRES_DB = projectName;
|
|
537
623
|
} else if (name === "mysql") {
|
|
538
624
|
const mysqlConfig = config;
|
|
539
625
|
if (mysqlConfig.database) env.MYSQL_DATABASE = mysqlConfig.database;
|
|
540
626
|
if (mysqlConfig.user) env.MYSQL_USER = mysqlConfig.user;
|
|
541
627
|
if (mysqlConfig.password) env.MYSQL_PASSWORD = mysqlConfig.password;
|
|
542
628
|
if (mysqlConfig.rootPassword) env.MYSQL_ROOT_PASSWORD = mysqlConfig.rootPassword;
|
|
543
|
-
if (!env
|
|
629
|
+
if (!mysqlConfig.database && !config.env?.MYSQL_DATABASE) env.MYSQL_DATABASE = projectName;
|
|
544
630
|
} else if (name === "mongo") {
|
|
545
631
|
const mongoConfig = config;
|
|
546
632
|
if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
|
|
547
633
|
if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
|
|
548
634
|
if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
|
|
549
|
-
if (!env
|
|
635
|
+
if (!mongoConfig.database && !config.env?.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
|
|
550
636
|
} else if (name === "redis") {
|
|
551
637
|
const redisConfig = config;
|
|
552
638
|
if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
|
|
639
|
+
if (env.REDIS_PASSWORD) {
|
|
640
|
+
env.REDISCLI_AUTH = env.REDIS_PASSWORD;
|
|
641
|
+
command ??= REDIS_AUTH_COMMAND;
|
|
642
|
+
}
|
|
553
643
|
}
|
|
554
644
|
const volumes = [...definition.volumes ?? [], ...config.volumes ?? []];
|
|
555
645
|
const resolvedConfig = {
|
|
@@ -559,7 +649,7 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
559
649
|
hostPort,
|
|
560
650
|
env,
|
|
561
651
|
volumes,
|
|
562
|
-
command
|
|
652
|
+
command,
|
|
563
653
|
healthcheck: definition.healthcheck,
|
|
564
654
|
ports: definition.ports
|
|
565
655
|
};
|
|
@@ -569,53 +659,53 @@ async function resolveServiceConfig(name, config, projectName) {
|
|
|
569
659
|
return resolvedConfig;
|
|
570
660
|
}
|
|
571
661
|
async function resolveCustomService(name, config) {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
578
|
-
hostPort = port;
|
|
579
|
-
} else if (typeof config.port === "number") {
|
|
580
|
-
port = config.port;
|
|
581
|
-
hostPort = config.port;
|
|
582
|
-
} else if (config.port && typeof config.port === "object") {
|
|
583
|
-
const { start, end } = config.port;
|
|
584
|
-
const startPort = start ?? defaultPort;
|
|
585
|
-
const endPort = end ?? startPort + 100;
|
|
586
|
-
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
587
|
-
hostPort = port;
|
|
662
|
+
let containerPort = 80;
|
|
663
|
+
if (typeof config.port === "number") {
|
|
664
|
+
containerPort = config.port;
|
|
665
|
+
} else if (config.port && typeof config.port === "object" && config.port.start) {
|
|
666
|
+
containerPort = config.port.start;
|
|
588
667
|
}
|
|
668
|
+
const { port, hostPort } = await resolveServicePorts(config.port, containerPort);
|
|
589
669
|
return {
|
|
590
670
|
name,
|
|
591
671
|
image: config.image,
|
|
592
672
|
port,
|
|
593
673
|
hostPort,
|
|
594
|
-
env: config.env
|
|
595
|
-
volumes: config.volumes ?? [],
|
|
674
|
+
env: { ...config.env },
|
|
675
|
+
volumes: [...config.volumes ?? []],
|
|
596
676
|
command: config.command,
|
|
597
|
-
healthcheck: config.
|
|
677
|
+
healthcheck: config.healthcheck,
|
|
598
678
|
ports: []
|
|
599
679
|
};
|
|
600
680
|
}
|
|
681
|
+
function pluginServiceDefinitions(plugins) {
|
|
682
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
683
|
+
for (const plugin of plugins ?? []) {
|
|
684
|
+
for (const [name, def] of Object.entries(plugin.services ?? {})) {
|
|
685
|
+
definitions.set(name, { ...def, name });
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return definitions;
|
|
689
|
+
}
|
|
601
690
|
async function resolveConfig(config) {
|
|
602
691
|
let finalConfig = config;
|
|
603
692
|
if (config.preset) {
|
|
604
693
|
finalConfig = mergePreset(config, config.preset);
|
|
605
694
|
}
|
|
606
695
|
finalConfig = normalizeConfig(finalConfig);
|
|
607
|
-
const
|
|
608
|
-
const
|
|
696
|
+
const appConfig = finalConfig.app ?? {};
|
|
697
|
+
const appPort = appConfig.port === "auto" ? await resolvePort({ preferred: 3e3, strategy: "auto" }) : appConfig.port;
|
|
698
|
+
const app = normalizeAppConfig({ ...appConfig, port: appPort });
|
|
699
|
+
const definitions = pluginServiceDefinitions(finalConfig.plugins);
|
|
609
700
|
const services2 = {};
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}
|
|
701
|
+
for (const [name, serviceConfig] of Object.entries(finalConfig.services ?? {})) {
|
|
702
|
+
if (serviceConfig) {
|
|
703
|
+
services2[name] = await resolveServiceConfig(
|
|
704
|
+
name,
|
|
705
|
+
serviceConfig,
|
|
706
|
+
finalConfig.name,
|
|
707
|
+
definitions
|
|
708
|
+
);
|
|
619
709
|
}
|
|
620
710
|
}
|
|
621
711
|
return {
|
|
@@ -638,31 +728,73 @@ function isEnvVarName(value) {
|
|
|
638
728
|
}
|
|
639
729
|
function isVersion(value) {
|
|
640
730
|
if (typeof value !== "string") return false;
|
|
641
|
-
|
|
642
|
-
return /^[a-zA-Z0-9.\-]+$/.test(value);
|
|
731
|
+
return /^\w[\w.-]{0,127}$/.test(value);
|
|
643
732
|
}
|
|
644
733
|
function isProjectName(value) {
|
|
645
734
|
if (typeof value !== "string") return false;
|
|
646
735
|
if (value.length === 0 || value.length > 100) return false;
|
|
647
736
|
return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(value);
|
|
648
737
|
}
|
|
738
|
+
function isComposeServiceName(value) {
|
|
739
|
+
if (typeof value !== "string") return false;
|
|
740
|
+
return /^[a-zA-Z0-9][\w.-]{0,62}$/.test(value);
|
|
741
|
+
}
|
|
742
|
+
var DOMAIN_COMPONENT = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])";
|
|
743
|
+
var DOMAIN = `${DOMAIN_COMPONENT}(?:\\.${DOMAIN_COMPONENT})*(?::[0-9]+)?`;
|
|
744
|
+
var PATH_COMPONENT = "[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*";
|
|
745
|
+
var TAG = "\\w[\\w.-]{0,127}";
|
|
746
|
+
var DIGEST = "[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*:[0-9a-fA-F]{32,}";
|
|
747
|
+
var IMAGE_REFERENCE = new RegExp(
|
|
748
|
+
`^(?:${DOMAIN}/)?${PATH_COMPONENT}(?:/${PATH_COMPONENT})*(?::${TAG})?(?:@${DIGEST})?$`
|
|
749
|
+
);
|
|
750
|
+
function isDockerImage(value) {
|
|
751
|
+
if (typeof value !== "string") return false;
|
|
752
|
+
if (value.length === 0 || value.length > 255) return false;
|
|
753
|
+
return IMAGE_REFERENCE.test(value);
|
|
754
|
+
}
|
|
755
|
+
function isHealthcheckPath(value) {
|
|
756
|
+
if (typeof value !== "string") return false;
|
|
757
|
+
return value.length <= 2048 && /^\/[\w\-.~%/?=&+,;:@]*$/.test(value);
|
|
758
|
+
}
|
|
759
|
+
function isSingleLine(value) {
|
|
760
|
+
return typeof value === "string" && !/[\r\n\0]/.test(value);
|
|
761
|
+
}
|
|
762
|
+
function isContainerPath(value) {
|
|
763
|
+
return typeof value === "string" && /^\/[\w./-]*$/.test(value);
|
|
764
|
+
}
|
|
765
|
+
function namedVolume(spec) {
|
|
766
|
+
const index = spec.indexOf(":");
|
|
767
|
+
if (index <= 0) return void 0;
|
|
768
|
+
const source = spec.slice(0, index);
|
|
769
|
+
return /^[a-zA-Z0-9][\w.-]*$/.test(source) ? source : void 0;
|
|
770
|
+
}
|
|
649
771
|
|
|
650
772
|
// src/core/validator.ts
|
|
773
|
+
var VALID_OUTPUT_FILES = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
|
|
651
774
|
function validateProjectName(name) {
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
return
|
|
775
|
+
if (isProjectName(name)) return [];
|
|
776
|
+
return [{
|
|
777
|
+
code: "INVALID_NAME",
|
|
778
|
+
message: `Invalid project name '${String(name)}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
|
|
779
|
+
path: "name",
|
|
780
|
+
value: name
|
|
781
|
+
}];
|
|
782
|
+
}
|
|
783
|
+
function validateStringList(value, path) {
|
|
784
|
+
if (value === void 0) return [];
|
|
785
|
+
if (!Array.isArray(value)) {
|
|
786
|
+
return [{ code: "INVALID_TYPE", message: `'${path}' must be an array of strings.`, path, value }];
|
|
787
|
+
}
|
|
788
|
+
return value.filter((item) => !isSingleLine(item) || item === "").map((item) => ({
|
|
789
|
+
code: "INVALID_VALUE",
|
|
790
|
+
message: `Invalid entry ${JSON.stringify(item)} in '${path}'. Entries must be non-empty single-line strings.`,
|
|
791
|
+
path,
|
|
792
|
+
value: item
|
|
793
|
+
}));
|
|
662
794
|
}
|
|
663
795
|
function validateAppConfig(config) {
|
|
664
796
|
const errors = [];
|
|
665
|
-
if (config.port !== "auto" && !isPort(config.port)) {
|
|
797
|
+
if (config.port !== void 0 && config.port !== "auto" && !isPort(config.port)) {
|
|
666
798
|
errors.push({
|
|
667
799
|
code: "INVALID_PORT",
|
|
668
800
|
message: `Invalid app port '${String(config.port)}'. Must be a number between 1-65535 or 'auto'.`,
|
|
@@ -673,54 +805,163 @@ function validateAppConfig(config) {
|
|
|
673
805
|
if (config.node !== void 0 && !isVersion(config.node)) {
|
|
674
806
|
errors.push({
|
|
675
807
|
code: "INVALID_VERSION",
|
|
676
|
-
message: `Invalid Node.js version '${config.node}'.`,
|
|
808
|
+
message: `Invalid Node.js version '${String(config.node)}'. Must be a valid Docker image tag.`,
|
|
677
809
|
path: "app.node",
|
|
678
810
|
value: config.node
|
|
679
811
|
});
|
|
680
812
|
}
|
|
681
|
-
if (config.
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
813
|
+
if (config.workdir !== void 0 && !isContainerPath(config.workdir)) {
|
|
814
|
+
errors.push({
|
|
815
|
+
code: "INVALID_WORKDIR",
|
|
816
|
+
message: `Invalid workdir '${String(config.workdir)}'. Must be an absolute path made of letters, numbers, '.', '_', '-' and '/'.`,
|
|
817
|
+
path: "app.workdir",
|
|
818
|
+
value: config.workdir
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
for (const key of ["command", "buildCommand"]) {
|
|
822
|
+
const value = config[key];
|
|
823
|
+
if (value !== void 0 && !isSingleLine(value)) {
|
|
824
|
+
errors.push({
|
|
825
|
+
code: "INVALID_COMMAND",
|
|
826
|
+
message: `Invalid app.${key}: must be a single-line string.`,
|
|
827
|
+
path: `app.${key}`,
|
|
828
|
+
value
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
if (config.command !== void 0 && config.command.trim() === "") {
|
|
833
|
+
errors.push({
|
|
834
|
+
code: "INVALID_COMMAND",
|
|
835
|
+
message: "Invalid app.command: must not be empty.",
|
|
836
|
+
path: "app.command",
|
|
837
|
+
value: config.command
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
if (config.healthcheck !== void 0 && !isHealthcheckPath(config.healthcheck)) {
|
|
841
|
+
errors.push({
|
|
842
|
+
code: "INVALID_HEALTHCHECK",
|
|
843
|
+
message: `Invalid health check path '${String(config.healthcheck)}'. Must start with '/' and contain only URL path characters (no quotes, spaces, '$' or backslashes).`,
|
|
844
|
+
path: "app.healthcheck",
|
|
845
|
+
value: config.healthcheck
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
if (config.env !== void 0) {
|
|
849
|
+
if (!Array.isArray(config.env)) {
|
|
850
|
+
errors.push({ code: "INVALID_TYPE", message: `'app.env' must be an array of variable names.`, path: "app.env", value: config.env });
|
|
851
|
+
} else {
|
|
852
|
+
for (const envVar of config.env) {
|
|
853
|
+
if (!isEnvVarName(envVar)) {
|
|
854
|
+
errors.push({
|
|
855
|
+
code: "INVALID_ENV_VAR",
|
|
856
|
+
message: `Invalid environment variable name '${String(envVar)}'. Must be uppercase letters, numbers, and underscores.`,
|
|
857
|
+
path: "app.env",
|
|
858
|
+
value: envVar
|
|
859
|
+
});
|
|
860
|
+
}
|
|
690
861
|
}
|
|
691
862
|
}
|
|
692
863
|
}
|
|
864
|
+
errors.push(...validateStringList(config.volumes, "app.volumes"));
|
|
693
865
|
return errors;
|
|
694
866
|
}
|
|
867
|
+
function validateServicePort(name, port) {
|
|
868
|
+
if (port === void 0 || port === "auto" || isPort(port)) return [];
|
|
869
|
+
if (port && typeof port === "object" && !Array.isArray(port)) {
|
|
870
|
+
const { start, end } = port;
|
|
871
|
+
if (isPort(start) && (end === void 0 || isPort(end) && end >= start)) {
|
|
872
|
+
return [];
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return [{
|
|
876
|
+
code: "INVALID_PORT",
|
|
877
|
+
message: `Invalid port configuration for service '${name}'. Must be a port number (1-65535), 'auto', or a range { start, end } with start <= end.`,
|
|
878
|
+
path: `services.${name}.port`,
|
|
879
|
+
value: port
|
|
880
|
+
}];
|
|
881
|
+
}
|
|
695
882
|
function validateServiceConfig(name, config) {
|
|
696
883
|
const errors = [];
|
|
697
|
-
if (
|
|
884
|
+
if (!isComposeServiceName(name)) {
|
|
885
|
+
errors.push({
|
|
886
|
+
code: "INVALID_SERVICE_NAME",
|
|
887
|
+
message: `Invalid service name '${name}'. Must start with a letter or digit and contain only letters, digits, '.', '_' and '-'.`,
|
|
888
|
+
path: `services.${name}`,
|
|
889
|
+
value: name
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
893
|
+
errors.push({
|
|
894
|
+
code: "INVALID_TYPE",
|
|
895
|
+
message: `Service '${name}' must be configured with an object.`,
|
|
896
|
+
path: `services.${name}`,
|
|
897
|
+
value: config
|
|
898
|
+
});
|
|
899
|
+
return errors;
|
|
900
|
+
}
|
|
901
|
+
const svc = config;
|
|
902
|
+
if (svc.version !== void 0 && !isVersion(svc.version)) {
|
|
698
903
|
errors.push({
|
|
699
904
|
code: "INVALID_VERSION",
|
|
700
|
-
message: `Invalid version '${
|
|
905
|
+
message: `Invalid version '${String(svc.version)}' for service '${name}'. Must be a valid Docker image tag.`,
|
|
701
906
|
path: `services.${name}.version`,
|
|
702
|
-
value:
|
|
907
|
+
value: svc.version
|
|
703
908
|
});
|
|
704
909
|
}
|
|
705
|
-
if (
|
|
910
|
+
if (svc.image !== void 0 && !isDockerImage(svc.image)) {
|
|
706
911
|
errors.push({
|
|
707
|
-
code: "
|
|
708
|
-
message: `Invalid
|
|
709
|
-
path: `services.${name}.
|
|
710
|
-
value:
|
|
912
|
+
code: "INVALID_IMAGE",
|
|
913
|
+
message: `Invalid image '${String(svc.image)}' for service '${name}'. Must be a Docker image reference such as 'elasticsearch:8.11.0'.`,
|
|
914
|
+
path: `services.${name}.image`,
|
|
915
|
+
value: svc.image
|
|
711
916
|
});
|
|
712
917
|
}
|
|
918
|
+
errors.push(...validateServicePort(name, svc.port));
|
|
919
|
+
if (svc.env !== void 0) {
|
|
920
|
+
if (typeof svc.env !== "object" || svc.env === null || Array.isArray(svc.env)) {
|
|
921
|
+
errors.push({ code: "INVALID_TYPE", message: `'services.${name}.env' must be an object of strings.`, path: `services.${name}.env`, value: svc.env });
|
|
922
|
+
} else {
|
|
923
|
+
for (const [key, value] of Object.entries(svc.env)) {
|
|
924
|
+
if (key === "" || /[=\s]/.test(key) || typeof value !== "string") {
|
|
925
|
+
errors.push({
|
|
926
|
+
code: "INVALID_ENV_VAR",
|
|
927
|
+
message: `Invalid environment variable '${key}' for service '${name}'. Names must not be empty or contain '=' or whitespace, and values must be strings.`,
|
|
928
|
+
path: `services.${name}.env.${key}`,
|
|
929
|
+
value
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
for (const key of ["user", "password", "database", "rootPassword", "command", "healthcheck"]) {
|
|
936
|
+
const value = svc[key];
|
|
937
|
+
if (value !== void 0 && typeof value !== "string") {
|
|
938
|
+
errors.push({
|
|
939
|
+
code: "INVALID_TYPE",
|
|
940
|
+
message: `'services.${name}.${key}' must be a string.`,
|
|
941
|
+
path: `services.${name}.${key}`,
|
|
942
|
+
value
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
errors.push(...validateStringList(svc.volumes, `services.${name}.volumes`));
|
|
713
947
|
return errors;
|
|
714
948
|
}
|
|
715
949
|
function validateOutputConfig(config) {
|
|
716
950
|
const errors = [];
|
|
717
|
-
if (config
|
|
718
|
-
|
|
951
|
+
if (config.dir !== void 0 && (typeof config.dir !== "string" || config.dir === "" || config.dir.includes("\0"))) {
|
|
952
|
+
errors.push({
|
|
953
|
+
code: "INVALID_OUTPUT_DIR",
|
|
954
|
+
message: "output.dir must be a non-empty path.",
|
|
955
|
+
path: "output.dir",
|
|
956
|
+
value: config.dir
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
if (config.files) {
|
|
719
960
|
for (const file of config.files) {
|
|
720
|
-
if (!
|
|
961
|
+
if (!VALID_OUTPUT_FILES.includes(file)) {
|
|
721
962
|
errors.push({
|
|
722
963
|
code: "INVALID_OUTPUT_FILE",
|
|
723
|
-
message: `Invalid output file '${file}'. Must be one of: ${
|
|
964
|
+
message: `Invalid output file '${String(file)}'. Must be one of: ${VALID_OUTPUT_FILES.join(", ")}.`,
|
|
724
965
|
path: "output.files",
|
|
725
966
|
value: file
|
|
726
967
|
});
|
|
@@ -731,13 +972,19 @@ function validateOutputConfig(config) {
|
|
|
731
972
|
}
|
|
732
973
|
function validateConfig(config) {
|
|
733
974
|
const errors = [];
|
|
975
|
+
if (typeof config !== "object" || config === null) {
|
|
976
|
+
return {
|
|
977
|
+
valid: false,
|
|
978
|
+
errors: [{ code: "INVALID_CONFIG", message: "Configuration must be an object.", value: config }]
|
|
979
|
+
};
|
|
980
|
+
}
|
|
734
981
|
errors.push(...validateProjectName(config.name));
|
|
735
982
|
if (config.app) {
|
|
736
983
|
errors.push(...validateAppConfig(config.app));
|
|
737
984
|
}
|
|
738
985
|
if (config.services) {
|
|
739
986
|
for (const [name, serviceConfig] of Object.entries(config.services)) {
|
|
740
|
-
if (serviceConfig) {
|
|
987
|
+
if (serviceConfig !== void 0) {
|
|
741
988
|
errors.push(...validateServiceConfig(name, serviceConfig));
|
|
742
989
|
}
|
|
743
990
|
}
|
|
@@ -752,18 +999,23 @@ function validateConfig(config) {
|
|
|
752
999
|
}
|
|
753
1000
|
function validateResolvedConfig(config) {
|
|
754
1001
|
const errors = [];
|
|
755
|
-
const usedPorts = /* @__PURE__ */ new
|
|
756
|
-
usedPorts.
|
|
1002
|
+
const usedPorts = /* @__PURE__ */ new Map();
|
|
1003
|
+
usedPorts.set(config.app.port, "app");
|
|
757
1004
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1005
|
+
const published = [serviceConfig.hostPort ?? serviceConfig.port, ...serviceConfig.ports ?? []];
|
|
1006
|
+
for (const port of published) {
|
|
1007
|
+
const owner = usedPorts.get(port);
|
|
1008
|
+
if (owner !== void 0) {
|
|
1009
|
+
errors.push({
|
|
1010
|
+
code: "PORT_CONFLICT",
|
|
1011
|
+
message: `Host port ${port} of service '${serviceName}' is already published by '${owner}'.`,
|
|
1012
|
+
path: `services.${serviceName}.port`,
|
|
1013
|
+
value: port
|
|
1014
|
+
});
|
|
1015
|
+
} else {
|
|
1016
|
+
usedPorts.set(port, serviceName);
|
|
1017
|
+
}
|
|
765
1018
|
}
|
|
766
|
-
usedPorts.add(serviceConfig.port);
|
|
767
1019
|
}
|
|
768
1020
|
return {
|
|
769
1021
|
valid: errors.length === 0,
|
|
@@ -771,11 +1023,24 @@ function validateResolvedConfig(config) {
|
|
|
771
1023
|
};
|
|
772
1024
|
}
|
|
773
1025
|
|
|
1026
|
+
// src/generators/healthcheck.ts
|
|
1027
|
+
function nodeHealthcheckScript(port, path) {
|
|
1028
|
+
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))`;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
774
1031
|
// src/generators/dockerfile.ts
|
|
1032
|
+
function dockerCmd(command) {
|
|
1033
|
+
const words = command.trim().split(/\s+/);
|
|
1034
|
+
if (words.every((word) => /^[\w@%+=:,./-]+$/.test(word))) {
|
|
1035
|
+
return `CMD ${JSON.stringify(words).replace(/","/g, '", "')}`;
|
|
1036
|
+
}
|
|
1037
|
+
return `CMD ${command.trim()}`;
|
|
1038
|
+
}
|
|
775
1039
|
function generateDockerfile(config, _context) {
|
|
776
|
-
const { app
|
|
1040
|
+
const { app } = config;
|
|
777
1041
|
const { node, workdir, command, buildCommand, multiStage, healthcheck } = app;
|
|
778
1042
|
const port = app.port;
|
|
1043
|
+
const hasBuild = buildCommand.trim() !== "";
|
|
779
1044
|
let dockerfile = "";
|
|
780
1045
|
if (multiStage) {
|
|
781
1046
|
dockerfile += `# Builder stage
|
|
@@ -801,11 +1066,13 @@ function generateDockerfile(config, _context) {
|
|
|
801
1066
|
dockerfile += `COPY . .
|
|
802
1067
|
|
|
803
1068
|
`;
|
|
804
|
-
|
|
1069
|
+
if (hasBuild) {
|
|
1070
|
+
dockerfile += `# Build application
|
|
805
1071
|
`;
|
|
806
|
-
|
|
1072
|
+
dockerfile += `RUN ${buildCommand.trim()}
|
|
807
1073
|
|
|
808
1074
|
`;
|
|
1075
|
+
}
|
|
809
1076
|
dockerfile += `# Production stage
|
|
810
1077
|
`;
|
|
811
1078
|
dockerfile += `FROM node:${node}
|
|
@@ -819,13 +1086,19 @@ function generateDockerfile(config, _context) {
|
|
|
819
1086
|
`;
|
|
820
1087
|
dockerfile += `# Copy from builder
|
|
821
1088
|
`;
|
|
822
|
-
|
|
1089
|
+
if (hasBuild) {
|
|
1090
|
+
dockerfile += `COPY --from=builder ${workdir}/node_modules ./node_modules
|
|
823
1091
|
`;
|
|
824
|
-
|
|
1092
|
+
dockerfile += `COPY --from=builder ${workdir}/dist ./dist
|
|
1093
|
+
`;
|
|
1094
|
+
dockerfile += `COPY --from=builder ${workdir}/package*.json ./
|
|
1095
|
+
|
|
825
1096
|
`;
|
|
826
|
-
|
|
1097
|
+
} else {
|
|
1098
|
+
dockerfile += `COPY --from=builder ${workdir} ./
|
|
827
1099
|
|
|
828
1100
|
`;
|
|
1101
|
+
}
|
|
829
1102
|
} else {
|
|
830
1103
|
dockerfile += `FROM node:${node}
|
|
831
1104
|
|
|
@@ -856,180 +1129,196 @@ function generateDockerfile(config, _context) {
|
|
|
856
1129
|
|
|
857
1130
|
`;
|
|
858
1131
|
if (healthcheck) {
|
|
1132
|
+
const script = JSON.stringify(nodeHealthcheckScript(port, healthcheck));
|
|
859
1133
|
dockerfile += `HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \\
|
|
860
|
-
|
|
1134
|
+
`;
|
|
1135
|
+
dockerfile += ` CMD ["node", "-e", ${script}]
|
|
861
1136
|
|
|
862
1137
|
`;
|
|
863
1138
|
}
|
|
864
|
-
dockerfile +=
|
|
1139
|
+
dockerfile += `${dockerCmd(command)}
|
|
865
1140
|
`;
|
|
866
1141
|
return dockerfile;
|
|
867
1142
|
}
|
|
868
1143
|
|
|
869
1144
|
// src/utils/yaml.ts
|
|
1145
|
+
var PLAIN_SCALAR = /^[A-Za-z_/.][\w./=@+~-]*$/;
|
|
1146
|
+
var NUMBER_LIKE = /^\.(?:\d|inf$|nan$)/i;
|
|
1147
|
+
var DURATION = /^\d+(?:\.\d+)?(?:ns|us|ms|s|m|h)$/;
|
|
1148
|
+
var RESERVED_WORD = /^(?:y|n|yes|no|on|off|true|false|null)$/i;
|
|
870
1149
|
function toYaml(obj, indent = 0) {
|
|
871
|
-
|
|
872
|
-
|
|
1150
|
+
if (isInline(obj)) {
|
|
1151
|
+
return inlineValue(obj);
|
|
1152
|
+
}
|
|
1153
|
+
if (Array.isArray(obj)) {
|
|
1154
|
+
return arrayToYaml(obj, indent);
|
|
1155
|
+
}
|
|
1156
|
+
return objectToYaml(obj, indent);
|
|
1157
|
+
}
|
|
1158
|
+
function isInline(value) {
|
|
1159
|
+
if (value === null || typeof value !== "object") {
|
|
1160
|
+
return true;
|
|
1161
|
+
}
|
|
1162
|
+
if (Array.isArray(value)) {
|
|
1163
|
+
return value.every((item) => item === null || item === void 0 || typeof item !== "object");
|
|
1164
|
+
}
|
|
1165
|
+
return Object.values(value).every((item) => item === void 0);
|
|
1166
|
+
}
|
|
1167
|
+
function inlineValue(value) {
|
|
1168
|
+
if (value === null || value === void 0) {
|
|
873
1169
|
return "null";
|
|
874
1170
|
}
|
|
875
|
-
if (typeof
|
|
876
|
-
return
|
|
1171
|
+
if (typeof value === "string") {
|
|
1172
|
+
return yamlScalar(value);
|
|
877
1173
|
}
|
|
878
|
-
if (typeof
|
|
879
|
-
return String(
|
|
1174
|
+
if (typeof value === "number") {
|
|
1175
|
+
if (Number.isFinite(value)) return String(value);
|
|
1176
|
+
if (Number.isNaN(value)) return ".nan";
|
|
1177
|
+
return value > 0 ? ".inf" : "-.inf";
|
|
880
1178
|
}
|
|
881
|
-
if (
|
|
882
|
-
return
|
|
1179
|
+
if (typeof value === "boolean") {
|
|
1180
|
+
return String(value);
|
|
1181
|
+
}
|
|
1182
|
+
if (Array.isArray(value)) {
|
|
1183
|
+
return `[${value.map((item) => inlineValue(item)).join(", ")}]`;
|
|
883
1184
|
}
|
|
884
|
-
if (typeof
|
|
885
|
-
return
|
|
1185
|
+
if (typeof value === "object") {
|
|
1186
|
+
return "{}";
|
|
886
1187
|
}
|
|
887
|
-
return String(
|
|
1188
|
+
return JSON.stringify(String(value));
|
|
888
1189
|
}
|
|
889
1190
|
function arrayToYaml(arr, indent) {
|
|
890
1191
|
const spaces = " ".repeat(indent);
|
|
891
|
-
const innerSpaces = " ".repeat(indent + 2);
|
|
892
|
-
if (arr.length === 0) {
|
|
893
|
-
return "[]";
|
|
894
|
-
}
|
|
895
|
-
if (arr.every(
|
|
896
|
-
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null
|
|
897
|
-
)) {
|
|
898
|
-
const items = arr.map((item) => toYaml(item, 0)).join(", ");
|
|
899
|
-
return `[${items}]`;
|
|
900
|
-
}
|
|
901
1192
|
const lines = [];
|
|
902
1193
|
for (const item of arr) {
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
lines.push(`${spaces}- ${value.slice(indent + 2)}`);
|
|
1194
|
+
if (isInline(item)) {
|
|
1195
|
+
lines.push(`${spaces}- ${inlineValue(item)}`);
|
|
906
1196
|
} else {
|
|
907
|
-
|
|
1197
|
+
const block = toYaml(item, indent + 2);
|
|
1198
|
+
lines.push(`${spaces}- ${block.slice(indent + 2)}`);
|
|
908
1199
|
}
|
|
909
1200
|
}
|
|
910
1201
|
return lines.join("\n");
|
|
911
1202
|
}
|
|
912
1203
|
function objectToYaml(obj, indent) {
|
|
913
|
-
const keys = Object.keys(obj);
|
|
914
|
-
if (keys.length === 0) {
|
|
915
|
-
return "{}";
|
|
916
|
-
}
|
|
917
1204
|
const spaces = " ".repeat(indent);
|
|
918
1205
|
const lines = [];
|
|
919
|
-
for (const key of
|
|
920
|
-
|
|
921
|
-
const
|
|
922
|
-
if (
|
|
923
|
-
lines.push(`${spaces}${
|
|
924
|
-
lines.push(valueStr);
|
|
1206
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1207
|
+
if (value === void 0) continue;
|
|
1208
|
+
const yamlKey = yamlScalar(key);
|
|
1209
|
+
if (isInline(value)) {
|
|
1210
|
+
lines.push(`${spaces}${yamlKey}: ${inlineValue(value)}`);
|
|
925
1211
|
} else {
|
|
926
|
-
lines.push(`${spaces}${
|
|
1212
|
+
lines.push(`${spaces}${yamlKey}:`);
|
|
1213
|
+
lines.push(toYaml(value, indent + 2));
|
|
927
1214
|
}
|
|
928
1215
|
}
|
|
929
1216
|
return lines.join("\n");
|
|
930
1217
|
}
|
|
931
|
-
function
|
|
932
|
-
if (str
|
|
933
|
-
return
|
|
934
|
-
}
|
|
935
|
-
if (/^[\-\?:\[\]{},&*#|!<>@%'"`]/.test(str)) {
|
|
936
|
-
return `"${escapeString(str)}"`;
|
|
937
|
-
}
|
|
938
|
-
if (/[:\s#]/.test(str)) {
|
|
939
|
-
return `"${escapeString(str)}"`;
|
|
1218
|
+
function yamlScalar(str) {
|
|
1219
|
+
if ((PLAIN_SCALAR.test(str) || DURATION.test(str)) && !RESERVED_WORD.test(str) && !NUMBER_LIKE.test(str)) {
|
|
1220
|
+
return str;
|
|
940
1221
|
}
|
|
941
|
-
|
|
942
|
-
return `"${escapeString(str)}"`;
|
|
943
|
-
}
|
|
944
|
-
return escapeString(str);
|
|
945
|
-
}
|
|
946
|
-
function escapeString(str) {
|
|
947
|
-
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
|
|
1222
|
+
return JSON.stringify(str);
|
|
948
1223
|
}
|
|
949
1224
|
|
|
950
1225
|
// src/generators/compose.ts
|
|
951
|
-
function
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
}
|
|
963
|
-
},
|
|
964
|
-
volumes: generateVolumeDefinitions(services2)
|
|
1226
|
+
function composeLiteral(value) {
|
|
1227
|
+
return value.replace(/\$/g, "$$$$");
|
|
1228
|
+
}
|
|
1229
|
+
function generateCompose(config, _context) {
|
|
1230
|
+
const { app, services: services2 } = config;
|
|
1231
|
+
const appService = {
|
|
1232
|
+
build: ".",
|
|
1233
|
+
ports: [`${app.port}:${app.port}`],
|
|
1234
|
+
environment: generateAppEnvironment(config),
|
|
1235
|
+
depends_on: Object.keys(services2),
|
|
1236
|
+
volumes: app.volumes.length > 0 ? app.volumes : void 0
|
|
965
1237
|
};
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
test: [`CMD`, `curl`, `-f`, `http://localhost:${app.port}${app.healthcheck}`],
|
|
1238
|
+
if (app.healthcheck) {
|
|
1239
|
+
appService.healthcheck = {
|
|
1240
|
+
test: ["CMD", "node", "-e", nodeHealthcheckScript(app.port, app.healthcheck)],
|
|
970
1241
|
interval: "30s",
|
|
971
1242
|
timeout: "3s",
|
|
972
1243
|
retries: 3
|
|
973
1244
|
};
|
|
974
1245
|
}
|
|
1246
|
+
const composeServices = { app: appService };
|
|
975
1247
|
for (const [serviceName, serviceConfig] of Object.entries(services2)) {
|
|
976
|
-
|
|
1248
|
+
composeServices[serviceName] = generateServiceConfig(serviceConfig);
|
|
977
1249
|
}
|
|
978
|
-
|
|
1250
|
+
const compose = {
|
|
1251
|
+
version: "3.9",
|
|
1252
|
+
services: composeServices,
|
|
1253
|
+
volumes: generateVolumeDefinitions(config)
|
|
1254
|
+
};
|
|
1255
|
+
return `${toYaml(compose)}
|
|
1256
|
+
`;
|
|
979
1257
|
}
|
|
980
|
-
function generateAppEnvironment(config
|
|
1258
|
+
function generateAppEnvironment(config) {
|
|
981
1259
|
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1260
|
+
const add = (key, value) => {
|
|
1261
|
+
env.push(`${key}=${composeLiteral(String(value))}`);
|
|
1262
|
+
};
|
|
982
1263
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
983
1264
|
if (serviceConfig.connectionString) {
|
|
984
|
-
const key = `${serviceName.toUpperCase()}_URL`;
|
|
985
|
-
|
|
1265
|
+
const key = `${serviceName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}_URL`;
|
|
1266
|
+
add(key, serviceConfig.connectionString);
|
|
986
1267
|
}
|
|
987
1268
|
}
|
|
988
1269
|
if (config.services.postgres) {
|
|
989
1270
|
const pg = config.services.postgres;
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1271
|
+
add("POSTGRES_HOST", pg.name);
|
|
1272
|
+
add("POSTGRES_PORT", pg.port);
|
|
1273
|
+
add("POSTGRES_USER", pg.env.POSTGRES_USER || "postgres");
|
|
1274
|
+
add("POSTGRES_PASSWORD", pg.env.POSTGRES_PASSWORD || "postgres");
|
|
1275
|
+
add("POSTGRES_DB", pg.env.POSTGRES_DB || config.name);
|
|
995
1276
|
}
|
|
996
1277
|
if (config.services.mysql) {
|
|
997
1278
|
const mysql = config.services.mysql;
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1279
|
+
add("MYSQL_HOST", mysql.name);
|
|
1280
|
+
add("MYSQL_PORT", mysql.port);
|
|
1281
|
+
add("MYSQL_USER", mysql.env.MYSQL_USER || "root");
|
|
1282
|
+
add("MYSQL_PASSWORD", mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root");
|
|
1283
|
+
add("MYSQL_DATABASE", mysql.env.MYSQL_DATABASE || config.name);
|
|
1003
1284
|
}
|
|
1004
1285
|
if (config.services.mongo) {
|
|
1005
1286
|
const mongo = config.services.mongo;
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1287
|
+
add("MONGO_HOST", mongo.name);
|
|
1288
|
+
add("MONGO_PORT", mongo.port);
|
|
1289
|
+
add("MONGO_USER", mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo");
|
|
1290
|
+
add("MONGO_PASSWORD", mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo");
|
|
1291
|
+
add("MONGO_DATABASE", mongo.env.MONGO_INITDB_DATABASE || config.name);
|
|
1011
1292
|
}
|
|
1012
1293
|
if (config.services.redis) {
|
|
1013
1294
|
const redis = config.services.redis;
|
|
1014
|
-
|
|
1015
|
-
|
|
1295
|
+
add("REDIS_HOST", redis.name);
|
|
1296
|
+
add("REDIS_PORT", redis.port);
|
|
1297
|
+
if (redis.env.REDIS_PASSWORD) {
|
|
1298
|
+
add("REDIS_PASSWORD", redis.env.REDIS_PASSWORD);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
for (const name of config.app.env) {
|
|
1302
|
+
if (!env.some((entry) => entry.startsWith(`${name}=`))) {
|
|
1303
|
+
env.push(name);
|
|
1304
|
+
}
|
|
1016
1305
|
}
|
|
1017
1306
|
return env;
|
|
1018
1307
|
}
|
|
1019
|
-
function generateServiceConfig(
|
|
1308
|
+
function generateServiceConfig(config) {
|
|
1309
|
+
const environment = {};
|
|
1310
|
+
for (const [key, value] of Object.entries(config.env)) {
|
|
1311
|
+
environment[key] = composeLiteral(value);
|
|
1312
|
+
}
|
|
1313
|
+
const portMappings = [`${config.hostPort ?? config.port}:${config.port}`];
|
|
1314
|
+
for (const extraPort of config.ports ?? []) {
|
|
1315
|
+
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1316
|
+
}
|
|
1020
1317
|
const service = {
|
|
1021
1318
|
image: config.image,
|
|
1022
|
-
ports:
|
|
1023
|
-
environment
|
|
1319
|
+
ports: portMappings,
|
|
1320
|
+
environment
|
|
1024
1321
|
};
|
|
1025
|
-
const portMappings = [];
|
|
1026
|
-
portMappings.push(`${config.hostPort ?? config.port}:${config.port}`);
|
|
1027
|
-
if (config.ports) {
|
|
1028
|
-
for (const extraPort of config.ports) {
|
|
1029
|
-
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
service.ports = portMappings;
|
|
1033
1322
|
if (config.volumes.length > 0) {
|
|
1034
1323
|
service.volumes = config.volumes;
|
|
1035
1324
|
}
|
|
@@ -1046,16 +1335,16 @@ function generateServiceConfig(name, config) {
|
|
|
1046
1335
|
}
|
|
1047
1336
|
return service;
|
|
1048
1337
|
}
|
|
1049
|
-
function generateVolumeDefinitions(
|
|
1338
|
+
function generateVolumeDefinitions(config) {
|
|
1050
1339
|
const volumes = {};
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
}
|
|
1340
|
+
const specs = [
|
|
1341
|
+
...config.app.volumes,
|
|
1342
|
+
...Object.values(config.services).flatMap((service) => service.volumes)
|
|
1343
|
+
];
|
|
1344
|
+
for (const spec of specs) {
|
|
1345
|
+
const name = namedVolume(spec);
|
|
1346
|
+
if (name) {
|
|
1347
|
+
volumes[name] = {};
|
|
1059
1348
|
}
|
|
1060
1349
|
}
|
|
1061
1350
|
return volumes;
|
|
@@ -1070,10 +1359,10 @@ yarn-debug.log*
|
|
|
1070
1359
|
yarn-error.log*
|
|
1071
1360
|
pnpm-debug.log*
|
|
1072
1361
|
|
|
1073
|
-
# Environment
|
|
1362
|
+
# Environment (secrets must never be copied into the image)
|
|
1074
1363
|
.env
|
|
1075
|
-
.env
|
|
1076
|
-
.
|
|
1364
|
+
.env.*
|
|
1365
|
+
!.env.example
|
|
1077
1366
|
|
|
1078
1367
|
# Git
|
|
1079
1368
|
.git/
|
|
@@ -1133,6 +1422,16 @@ docker-compose.yml
|
|
|
1133
1422
|
}
|
|
1134
1423
|
|
|
1135
1424
|
// src/generators/env.ts
|
|
1425
|
+
function envValue(value) {
|
|
1426
|
+
if (/^[\w@%+=:,./?&-]*$/.test(value)) {
|
|
1427
|
+
return value;
|
|
1428
|
+
}
|
|
1429
|
+
if (!/['\r\n]/.test(value)) {
|
|
1430
|
+
return `'${value}'`;
|
|
1431
|
+
}
|
|
1432
|
+
const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
|
1433
|
+
return `"${escaped}"`;
|
|
1434
|
+
}
|
|
1136
1435
|
function generateEnvExample(config) {
|
|
1137
1436
|
let content = `# Application Configuration
|
|
1138
1437
|
NODE_ENV=production
|
|
@@ -1152,17 +1451,17 @@ PORT=${config.app.port}
|
|
|
1152
1451
|
content += `
|
|
1153
1452
|
# PostgreSQL
|
|
1154
1453
|
`;
|
|
1155
|
-
content += `DATABASE_URL=${pg.connectionString || ""}
|
|
1454
|
+
content += `DATABASE_URL=${envValue(String(pg.connectionString || ""))}
|
|
1156
1455
|
`;
|
|
1157
|
-
content += `POSTGRES_HOST=${pg.name}
|
|
1456
|
+
content += `POSTGRES_HOST=${envValue(String(pg.name))}
|
|
1158
1457
|
`;
|
|
1159
|
-
content += `POSTGRES_PORT=${pg.port}
|
|
1458
|
+
content += `POSTGRES_PORT=${envValue(String(pg.port))}
|
|
1160
1459
|
`;
|
|
1161
|
-
content += `POSTGRES_USER=${pg.env.POSTGRES_USER || "postgres"}
|
|
1460
|
+
content += `POSTGRES_USER=${envValue(String(pg.env.POSTGRES_USER || "postgres"))}
|
|
1162
1461
|
`;
|
|
1163
|
-
content += `POSTGRES_PASSWORD=${pg.env.POSTGRES_PASSWORD || "postgres"}
|
|
1462
|
+
content += `POSTGRES_PASSWORD=${envValue(String(pg.env.POSTGRES_PASSWORD || "postgres"))}
|
|
1164
1463
|
`;
|
|
1165
|
-
content += `POSTGRES_DB=${pg.env.POSTGRES_DB || config.name}
|
|
1464
|
+
content += `POSTGRES_DB=${envValue(String(pg.env.POSTGRES_DB || config.name))}
|
|
1166
1465
|
`;
|
|
1167
1466
|
}
|
|
1168
1467
|
if (config.services.mysql) {
|
|
@@ -1170,17 +1469,17 @@ PORT=${config.app.port}
|
|
|
1170
1469
|
content += `
|
|
1171
1470
|
# MySQL
|
|
1172
1471
|
`;
|
|
1173
|
-
content += `MYSQL_URL=${mysql.connectionString || ""}
|
|
1472
|
+
content += `MYSQL_URL=${envValue(String(mysql.connectionString || ""))}
|
|
1174
1473
|
`;
|
|
1175
|
-
content += `MYSQL_HOST=${mysql.name}
|
|
1474
|
+
content += `MYSQL_HOST=${envValue(String(mysql.name))}
|
|
1176
1475
|
`;
|
|
1177
|
-
content += `MYSQL_PORT=${mysql.port}
|
|
1476
|
+
content += `MYSQL_PORT=${envValue(String(mysql.port))}
|
|
1178
1477
|
`;
|
|
1179
|
-
content += `MYSQL_USER=${mysql.env.MYSQL_USER || "root"}
|
|
1478
|
+
content += `MYSQL_USER=${envValue(String(mysql.env.MYSQL_USER || "root"))}
|
|
1180
1479
|
`;
|
|
1181
|
-
content += `MYSQL_PASSWORD=${mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"}
|
|
1480
|
+
content += `MYSQL_PASSWORD=${envValue(String(mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"))}
|
|
1182
1481
|
`;
|
|
1183
|
-
content += `MYSQL_DATABASE=${mysql.env.MYSQL_DATABASE || config.name}
|
|
1482
|
+
content += `MYSQL_DATABASE=${envValue(String(mysql.env.MYSQL_DATABASE || config.name))}
|
|
1184
1483
|
`;
|
|
1185
1484
|
}
|
|
1186
1485
|
if (config.services.mongo) {
|
|
@@ -1188,17 +1487,17 @@ PORT=${config.app.port}
|
|
|
1188
1487
|
content += `
|
|
1189
1488
|
# MongoDB
|
|
1190
1489
|
`;
|
|
1191
|
-
content += `MONGO_URL=${mongo.connectionString || ""}
|
|
1490
|
+
content += `MONGO_URL=${envValue(String(mongo.connectionString || ""))}
|
|
1192
1491
|
`;
|
|
1193
|
-
content += `MONGO_HOST=${mongo.name}
|
|
1492
|
+
content += `MONGO_HOST=${envValue(String(mongo.name))}
|
|
1194
1493
|
`;
|
|
1195
|
-
content += `MONGO_PORT=${mongo.port}
|
|
1494
|
+
content += `MONGO_PORT=${envValue(String(mongo.port))}
|
|
1196
1495
|
`;
|
|
1197
|
-
content += `MONGO_USER=${mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"}
|
|
1496
|
+
content += `MONGO_USER=${envValue(String(mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"))}
|
|
1198
1497
|
`;
|
|
1199
|
-
content += `MONGO_PASSWORD=${mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"}
|
|
1498
|
+
content += `MONGO_PASSWORD=${envValue(String(mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"))}
|
|
1200
1499
|
`;
|
|
1201
|
-
content += `MONGO_DATABASE=${mongo.env.MONGO_INITDB_DATABASE || config.name}
|
|
1500
|
+
content += `MONGO_DATABASE=${envValue(String(mongo.env.MONGO_INITDB_DATABASE || config.name))}
|
|
1202
1501
|
`;
|
|
1203
1502
|
}
|
|
1204
1503
|
if (config.services.redis) {
|
|
@@ -1206,14 +1505,14 @@ PORT=${config.app.port}
|
|
|
1206
1505
|
content += `
|
|
1207
1506
|
# Redis
|
|
1208
1507
|
`;
|
|
1209
|
-
content += `REDIS_URL=${redis.connectionString || ""}
|
|
1508
|
+
content += `REDIS_URL=${envValue(String(redis.connectionString || ""))}
|
|
1210
1509
|
`;
|
|
1211
|
-
content += `REDIS_HOST=${redis.name}
|
|
1510
|
+
content += `REDIS_HOST=${envValue(String(redis.name))}
|
|
1212
1511
|
`;
|
|
1213
|
-
content += `REDIS_PORT=${redis.port}
|
|
1512
|
+
content += `REDIS_PORT=${envValue(String(redis.port))}
|
|
1214
1513
|
`;
|
|
1215
1514
|
if (redis.env.REDIS_PASSWORD) {
|
|
1216
|
-
content += `REDIS_PASSWORD=${redis.env.REDIS_PASSWORD}
|
|
1515
|
+
content += `REDIS_PASSWORD=${envValue(String(redis.env.REDIS_PASSWORD))}
|
|
1217
1516
|
`;
|
|
1218
1517
|
}
|
|
1219
1518
|
}
|
|
@@ -1228,7 +1527,7 @@ function generateBuildScript(_config) {
|
|
|
1228
1527
|
set -e
|
|
1229
1528
|
|
|
1230
1529
|
echo "Building Docker image..."
|
|
1231
|
-
docker
|
|
1530
|
+
docker compose build
|
|
1232
1531
|
|
|
1233
1532
|
echo "Build complete!"
|
|
1234
1533
|
`;
|
|
@@ -1241,7 +1540,7 @@ function generateUpScript(config) {
|
|
|
1241
1540
|
set -e
|
|
1242
1541
|
|
|
1243
1542
|
echo "Starting services..."
|
|
1244
|
-
docker
|
|
1543
|
+
docker compose up -d ${services2.join(" ")}
|
|
1245
1544
|
|
|
1246
1545
|
echo "Services started!"
|
|
1247
1546
|
echo ""
|
|
@@ -1255,7 +1554,7 @@ function generateDownScript(_config) {
|
|
|
1255
1554
|
set -e
|
|
1256
1555
|
|
|
1257
1556
|
echo "Stopping services..."
|
|
1258
|
-
docker
|
|
1557
|
+
docker compose down
|
|
1259
1558
|
|
|
1260
1559
|
echo "Services stopped!"
|
|
1261
1560
|
`;
|
|
@@ -1267,9 +1566,9 @@ function generateLogsScript(_config) {
|
|
|
1267
1566
|
set -e
|
|
1268
1567
|
|
|
1269
1568
|
if [ -n "$1" ]; then
|
|
1270
|
-
docker
|
|
1569
|
+
docker compose logs -f "$1"
|
|
1271
1570
|
else
|
|
1272
|
-
docker
|
|
1571
|
+
docker compose logs -f
|
|
1273
1572
|
fi
|
|
1274
1573
|
`;
|
|
1275
1574
|
}
|
|
@@ -1296,28 +1595,28 @@ help:
|
|
|
1296
1595
|
@echo " make restart - Restart all services"
|
|
1297
1596
|
@echo " make logs - Show logs from all services"
|
|
1298
1597
|
@echo " make shell - Open shell in app container"
|
|
1299
|
-
@echo " make clean - Remove containers and volumes"
|
|
1598
|
+
@echo " make clean - Remove this project's containers and volumes"
|
|
1300
1599
|
@echo " make install - Install dependencies"
|
|
1301
1600
|
|
|
1302
1601
|
# Build Docker images
|
|
1303
1602
|
build:
|
|
1304
|
-
docker
|
|
1603
|
+
docker compose build
|
|
1305
1604
|
|
|
1306
1605
|
# Start all services
|
|
1307
1606
|
up:
|
|
1308
|
-
docker
|
|
1607
|
+
docker compose up -d
|
|
1309
1608
|
@echo "App running at http://localhost:${config.app.port}"
|
|
1310
1609
|
|
|
1311
1610
|
# Stop all services
|
|
1312
1611
|
down:
|
|
1313
|
-
docker
|
|
1612
|
+
docker compose down
|
|
1314
1613
|
|
|
1315
1614
|
# Restart all services
|
|
1316
1615
|
restart: down up
|
|
1317
1616
|
|
|
1318
1617
|
# Show logs from all services
|
|
1319
1618
|
logs:
|
|
1320
|
-
docker
|
|
1619
|
+
docker compose logs -f
|
|
1321
1620
|
`;
|
|
1322
1621
|
if (services2.length > 0) {
|
|
1323
1622
|
content += "\n";
|
|
@@ -1325,23 +1624,22 @@ logs:
|
|
|
1325
1624
|
content += `
|
|
1326
1625
|
logs-${service}:
|
|
1327
1626
|
`;
|
|
1328
|
-
content += ` docker
|
|
1627
|
+
content += ` docker compose logs -f ${service}
|
|
1329
1628
|
`;
|
|
1330
1629
|
}
|
|
1331
1630
|
}
|
|
1332
1631
|
content += `
|
|
1333
|
-
#
|
|
1632
|
+
# Remove this project's containers, networks and volumes
|
|
1334
1633
|
clean:
|
|
1335
|
-
docker
|
|
1336
|
-
docker system prune -f
|
|
1634
|
+
docker compose down -v --remove-orphans
|
|
1337
1635
|
|
|
1338
1636
|
# Install dependencies (in running container)
|
|
1339
1637
|
shell:
|
|
1340
|
-
docker
|
|
1638
|
+
docker compose exec app sh
|
|
1341
1639
|
|
|
1342
1640
|
# Install dependencies
|
|
1343
1641
|
install:
|
|
1344
|
-
docker
|
|
1642
|
+
docker compose exec app npm install
|
|
1345
1643
|
`;
|
|
1346
1644
|
return content;
|
|
1347
1645
|
}
|
|
@@ -1388,18 +1686,20 @@ async function ensureDir(path) {
|
|
|
1388
1686
|
}
|
|
1389
1687
|
}
|
|
1390
1688
|
}
|
|
1391
|
-
async function writeFile(filePath, content) {
|
|
1689
|
+
async function writeFile(filePath, content, mode) {
|
|
1392
1690
|
const dir = dirname(filePath);
|
|
1393
1691
|
await ensureDir(dir);
|
|
1394
|
-
const
|
|
1692
|
+
const suffix = `${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2)}`;
|
|
1693
|
+
const tempPath = join(dir, `.${basename(filePath)}.${suffix}.tmp`);
|
|
1395
1694
|
try {
|
|
1396
|
-
await fs.writeFile(tempPath, content, "utf8");
|
|
1695
|
+
await fs.writeFile(tempPath, content, { encoding: "utf8", mode });
|
|
1696
|
+
if (mode !== void 0) {
|
|
1697
|
+
await fs.chmod(tempPath, mode);
|
|
1698
|
+
}
|
|
1397
1699
|
await fs.rename(tempPath, filePath);
|
|
1398
1700
|
} catch (error) {
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
} catch {
|
|
1402
|
-
}
|
|
1701
|
+
await fs.unlink(tempPath).catch(() => {
|
|
1702
|
+
});
|
|
1403
1703
|
throw error;
|
|
1404
1704
|
}
|
|
1405
1705
|
}
|
|
@@ -1413,24 +1713,27 @@ async function fileExists(filePath) {
|
|
|
1413
1713
|
}
|
|
1414
1714
|
|
|
1415
1715
|
// src/core/dockcraft.ts
|
|
1416
|
-
import { join as join2 } from "path";
|
|
1716
|
+
import { isAbsolute, join as join2, normalize } from "path";
|
|
1717
|
+
function validationError(prefix, errors) {
|
|
1718
|
+
const errorMessages = errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1719
|
+
return new ValidationError(`${prefix}:
|
|
1720
|
+
${errorMessages}`, errors);
|
|
1721
|
+
}
|
|
1722
|
+
function isSafeOutputPath(fileName) {
|
|
1723
|
+
if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
|
|
1724
|
+
if (isAbsolute(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
|
|
1725
|
+
const normalized = normalize(fileName.replace(/\\/g, "/"));
|
|
1726
|
+
return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
|
|
1727
|
+
}
|
|
1417
1728
|
async function dockcraft(config, options = {}) {
|
|
1418
1729
|
const validation = validateConfig(config);
|
|
1419
1730
|
if (!validation.valid) {
|
|
1420
|
-
|
|
1421
|
-
throw new Error(
|
|
1422
|
-
`Configuration validation failed:
|
|
1423
|
-
${errorMessages}`
|
|
1424
|
-
);
|
|
1731
|
+
throw validationError("Configuration validation failed", validation.errors);
|
|
1425
1732
|
}
|
|
1426
1733
|
const resolved = await resolveConfig(config);
|
|
1427
1734
|
const resolvedValidation = validateResolvedConfig(resolved);
|
|
1428
1735
|
if (!resolvedValidation.valid) {
|
|
1429
|
-
|
|
1430
|
-
throw new Error(
|
|
1431
|
-
`Resolved configuration validation failed:
|
|
1432
|
-
${errorMessages}`
|
|
1433
|
-
);
|
|
1736
|
+
throw validationError("Resolved configuration validation failed", resolvedValidation.errors);
|
|
1434
1737
|
}
|
|
1435
1738
|
const context = {
|
|
1436
1739
|
config: resolved,
|
|
@@ -1438,22 +1741,38 @@ ${errorMessages}`
|
|
|
1438
1741
|
Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
|
|
1439
1742
|
),
|
|
1440
1743
|
strings: new Map(
|
|
1441
|
-
Object.entries(resolved.services).filter((
|
|
1744
|
+
Object.entries(resolved.services).filter((entry) => Boolean(entry[1].connectionString)).map(([name, svc]) => [name, svc.connectionString])
|
|
1442
1745
|
)
|
|
1443
1746
|
};
|
|
1444
1747
|
context.ports.set("app", resolved.app.port);
|
|
1445
1748
|
const generated = generateAll(resolved, context);
|
|
1749
|
+
for (const plugin of config.plugins ?? []) {
|
|
1750
|
+
for (const output of plugin.outputs ?? []) {
|
|
1751
|
+
if (!isSafeOutputPath(output.file)) {
|
|
1752
|
+
throw new ConfigError(
|
|
1753
|
+
`Plugin '${plugin.name}' output '${output.name}' has an unsafe file name '${output.file}'. Use a relative path inside the output directory.`,
|
|
1754
|
+
"UNSAFE_OUTPUT_PATH"
|
|
1755
|
+
);
|
|
1756
|
+
}
|
|
1757
|
+
if (output.file in generated) {
|
|
1758
|
+
throw new ConfigError(
|
|
1759
|
+
`Plugin '${plugin.name}' output '${output.name}' would overwrite generated file '${output.file}'.`,
|
|
1760
|
+
"DUPLICATE_OUTPUT"
|
|
1761
|
+
);
|
|
1762
|
+
}
|
|
1763
|
+
generated[output.file] = output.generate(resolved, context);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1446
1766
|
const written = [];
|
|
1447
1767
|
const outputDir = resolved.output.dir ?? "./";
|
|
1448
1768
|
if (!options.dryRun) {
|
|
1449
1769
|
for (const [fileName, content] of Object.entries(generated)) {
|
|
1450
|
-
if (!fileName) continue;
|
|
1451
1770
|
const filePath = join2(outputDir, fileName);
|
|
1452
1771
|
const exists = await fileExists(filePath);
|
|
1453
1772
|
if (exists && !resolved.output.overwrite) {
|
|
1454
1773
|
continue;
|
|
1455
1774
|
}
|
|
1456
|
-
await writeFile(filePath, content);
|
|
1775
|
+
await writeFile(filePath, content, fileName.endsWith(".sh") ? 493 : void 0);
|
|
1457
1776
|
written.push(filePath);
|
|
1458
1777
|
}
|
|
1459
1778
|
}
|
|
@@ -1500,79 +1819,37 @@ function listDefaultPorts() {
|
|
|
1500
1819
|
return { ...DEFAULT_PORTS };
|
|
1501
1820
|
}
|
|
1502
1821
|
|
|
1503
|
-
// src/types.ts
|
|
1504
|
-
var DockcraftError = class extends Error {
|
|
1505
|
-
constructor(message, code) {
|
|
1506
|
-
super(message);
|
|
1507
|
-
this.code = code;
|
|
1508
|
-
this.name = "DockcraftError";
|
|
1509
|
-
}
|
|
1510
|
-
};
|
|
1511
|
-
var ConfigError = class extends DockcraftError {
|
|
1512
|
-
constructor(message, code = "CONFIG_ERROR") {
|
|
1513
|
-
super(message, code);
|
|
1514
|
-
this.name = "ConfigError";
|
|
1515
|
-
}
|
|
1516
|
-
};
|
|
1517
|
-
var ValidationError = class extends DockcraftError {
|
|
1518
|
-
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
1519
|
-
super(message, code);
|
|
1520
|
-
this.errors = errors;
|
|
1521
|
-
this.name = "ValidationError";
|
|
1522
|
-
}
|
|
1523
|
-
};
|
|
1524
|
-
var PortConflictError = class extends DockcraftError {
|
|
1525
|
-
constructor(port, code = "PORT_CONFLICT") {
|
|
1526
|
-
super(`Port ${port} is already in use`, code);
|
|
1527
|
-
this.name = "PortConflictError";
|
|
1528
|
-
}
|
|
1529
|
-
};
|
|
1530
|
-
var NoPortAvailableError = class extends DockcraftError {
|
|
1531
|
-
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
1532
|
-
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
1533
|
-
this.name = "NoPortAvailableError";
|
|
1534
|
-
}
|
|
1535
|
-
};
|
|
1536
|
-
var FileWriteError = class extends DockcraftError {
|
|
1537
|
-
constructor(path, code = "FILE_WRITE_ERROR") {
|
|
1538
|
-
super(`Failed to write file: ${path}`, code);
|
|
1539
|
-
this.name = "FileWriteError";
|
|
1540
|
-
}
|
|
1541
|
-
};
|
|
1542
|
-
var PresetNotFoundError = class extends DockcraftError {
|
|
1543
|
-
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
1544
|
-
super(`Preset '${name}' not found`, code);
|
|
1545
|
-
this.name = "PresetNotFoundError";
|
|
1546
|
-
}
|
|
1547
|
-
};
|
|
1548
|
-
var ServiceNotFoundError = class extends DockcraftError {
|
|
1549
|
-
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
1550
|
-
super(`Service '${name}' not found`, code);
|
|
1551
|
-
this.name = "ServiceNotFoundError";
|
|
1552
|
-
}
|
|
1553
|
-
};
|
|
1554
|
-
|
|
1555
1822
|
// src/index.ts
|
|
1556
1823
|
function getConnectionString(service, config) {
|
|
1824
|
+
const enc = encodeURIComponent;
|
|
1825
|
+
const { host, port } = config;
|
|
1557
1826
|
switch (service) {
|
|
1558
|
-
case "postgres":
|
|
1559
|
-
const
|
|
1560
|
-
const
|
|
1561
|
-
const
|
|
1562
|
-
return `postgres://${
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
const
|
|
1566
|
-
const
|
|
1567
|
-
|
|
1827
|
+
case "postgres": {
|
|
1828
|
+
const user = config.user ?? "postgres";
|
|
1829
|
+
const password = config.password ?? "postgres";
|
|
1830
|
+
const database = config.database ?? "myapp";
|
|
1831
|
+
return `postgres://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}`;
|
|
1832
|
+
}
|
|
1833
|
+
case "mysql": {
|
|
1834
|
+
const user = config.user ?? "root";
|
|
1835
|
+
const password = config.password ?? "root";
|
|
1836
|
+
const database = config.database ?? "myapp";
|
|
1837
|
+
return `mysql://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}`;
|
|
1838
|
+
}
|
|
1568
1839
|
case "mongo":
|
|
1569
|
-
case "mongodb":
|
|
1570
|
-
const
|
|
1571
|
-
const
|
|
1572
|
-
const
|
|
1573
|
-
return `mongodb://${
|
|
1574
|
-
|
|
1575
|
-
|
|
1840
|
+
case "mongodb": {
|
|
1841
|
+
const user = config.user ?? "mongo";
|
|
1842
|
+
const password = config.password ?? "mongo";
|
|
1843
|
+
const database = config.database ?? "myapp";
|
|
1844
|
+
return `mongodb://${enc(user)}:${enc(password)}@${host}:${port}/${enc(database)}?authSource=admin`;
|
|
1845
|
+
}
|
|
1846
|
+
case "redis": {
|
|
1847
|
+
if (config.password === void 0 || config.password === "") {
|
|
1848
|
+
return `redis://${host}:${port}`;
|
|
1849
|
+
}
|
|
1850
|
+
const user = config.user ? enc(config.user) : "";
|
|
1851
|
+
return `redis://${user}:${enc(config.password)}@${host}:${port}`;
|
|
1852
|
+
}
|
|
1576
1853
|
default:
|
|
1577
1854
|
throw new Error(`Unknown service: ${service}`);
|
|
1578
1855
|
}
|