@oxog/dockcraft 1.1.0 → 1.2.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 +51 -0
- package/README.md +20 -1
- package/dist/cli/index.js +162 -57
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +98 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +96 -20
- package/dist/index.js.map +1 -1
- package/package.json +11 -7
package/dist/index.d.cts
CHANGED
|
@@ -367,7 +367,9 @@ declare class NoPortAvailableError extends DockcraftError {
|
|
|
367
367
|
* File write error
|
|
368
368
|
*/
|
|
369
369
|
declare class FileWriteError extends DockcraftError {
|
|
370
|
-
|
|
370
|
+
/** The underlying file system error, when there is one */
|
|
371
|
+
cause?: unknown;
|
|
372
|
+
constructor(path: string, code?: string, cause?: unknown);
|
|
371
373
|
}
|
|
372
374
|
/**
|
|
373
375
|
* Preset not found error
|
package/dist/index.d.ts
CHANGED
|
@@ -367,7 +367,9 @@ declare class NoPortAvailableError extends DockcraftError {
|
|
|
367
367
|
* File write error
|
|
368
368
|
*/
|
|
369
369
|
declare class FileWriteError extends DockcraftError {
|
|
370
|
-
|
|
370
|
+
/** The underlying file system error, when there is one */
|
|
371
|
+
cause?: unknown;
|
|
372
|
+
constructor(path: string, code?: string, cause?: unknown);
|
|
371
373
|
}
|
|
372
374
|
/**
|
|
373
375
|
* Preset not found error
|
package/dist/index.js
CHANGED
|
@@ -38,9 +38,12 @@ var NoPortAvailableError = class extends DockcraftError {
|
|
|
38
38
|
}
|
|
39
39
|
};
|
|
40
40
|
var FileWriteError = class extends DockcraftError {
|
|
41
|
-
constructor(path, code = "FILE_WRITE_ERROR") {
|
|
42
|
-
super(`Failed to write file: ${path}`, code);
|
|
41
|
+
constructor(path, code = "FILE_WRITE_ERROR", cause) {
|
|
42
|
+
super(`Failed to write file: ${path}${cause instanceof Error ? ` (${cause.message})` : ""}`, code);
|
|
43
43
|
this.name = "FileWriteError";
|
|
44
|
+
if (cause !== void 0) {
|
|
45
|
+
this.cause = cause;
|
|
46
|
+
}
|
|
44
47
|
}
|
|
45
48
|
};
|
|
46
49
|
var PresetNotFoundError = class extends DockcraftError {
|
|
@@ -499,7 +502,8 @@ async function resolvePort(options = {}) {
|
|
|
499
502
|
if (await isPortAvailable(preferred)) {
|
|
500
503
|
return preferred;
|
|
501
504
|
}
|
|
502
|
-
throw new
|
|
505
|
+
throw new PortConflictError(preferred);
|
|
506
|
+
// @ts-expect-error -- TS7029: the fall-through to 'auto' (no range given) is intended
|
|
503
507
|
case "range":
|
|
504
508
|
if (range) {
|
|
505
509
|
const [start, end = start + 100] = range;
|
|
@@ -507,7 +511,7 @@ async function resolvePort(options = {}) {
|
|
|
507
511
|
if (port !== void 0) {
|
|
508
512
|
return port;
|
|
509
513
|
}
|
|
510
|
-
throw new
|
|
514
|
+
throw new NoPortAvailableError([start, end]);
|
|
511
515
|
}
|
|
512
516
|
// Fall through to auto if no range specified
|
|
513
517
|
case "auto":
|
|
@@ -516,7 +520,7 @@ async function resolvePort(options = {}) {
|
|
|
516
520
|
if (port !== void 0) {
|
|
517
521
|
return port;
|
|
518
522
|
}
|
|
519
|
-
throw new
|
|
523
|
+
throw new NoPortAvailableError([preferred, 65535]);
|
|
520
524
|
}
|
|
521
525
|
}
|
|
522
526
|
}
|
|
@@ -884,8 +888,8 @@ function validateServiceConfig(name, config) {
|
|
|
884
888
|
if (!isComposeServiceName(name)) {
|
|
885
889
|
errors.push({
|
|
886
890
|
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}`,
|
|
891
|
+
message: `Invalid service name '${String(name)}'. Must start with a letter or digit and contain only letters, digits, '.', '_' and '-'.`,
|
|
892
|
+
path: `services.${String(name)}`,
|
|
889
893
|
value: name
|
|
890
894
|
});
|
|
891
895
|
}
|
|
@@ -902,6 +906,7 @@ function validateServiceConfig(name, config) {
|
|
|
902
906
|
if (svc.version !== void 0 && !isVersion(svc.version)) {
|
|
903
907
|
errors.push({
|
|
904
908
|
code: "INVALID_VERSION",
|
|
909
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- echoes whatever invalid value was given
|
|
905
910
|
message: `Invalid version '${String(svc.version)}' for service '${name}'. Must be a valid Docker image tag.`,
|
|
906
911
|
path: `services.${name}.version`,
|
|
907
912
|
value: svc.version
|
|
@@ -910,6 +915,7 @@ function validateServiceConfig(name, config) {
|
|
|
910
915
|
if (svc.image !== void 0 && !isDockerImage(svc.image)) {
|
|
911
916
|
errors.push({
|
|
912
917
|
code: "INVALID_IMAGE",
|
|
918
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- echoes whatever invalid value was given
|
|
913
919
|
message: `Invalid image '${String(svc.image)}' for service '${name}'. Must be a Docker image reference such as 'elasticsearch:8.11.0'.`,
|
|
914
920
|
path: `services.${name}.image`,
|
|
915
921
|
value: svc.image
|
|
@@ -1222,14 +1228,41 @@ function yamlScalar(str) {
|
|
|
1222
1228
|
return JSON.stringify(str);
|
|
1223
1229
|
}
|
|
1224
1230
|
|
|
1231
|
+
// src/generators/paths.ts
|
|
1232
|
+
import { isAbsolute, join, relative } from "path";
|
|
1233
|
+
function currentDir() {
|
|
1234
|
+
return typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : "/";
|
|
1235
|
+
}
|
|
1236
|
+
function outputSubdir(dir) {
|
|
1237
|
+
if (!dir) {
|
|
1238
|
+
return void 0;
|
|
1239
|
+
}
|
|
1240
|
+
const root = currentDir();
|
|
1241
|
+
const subdir = relative(root, isAbsolute(dir) ? dir : join(root, dir)).replace(/\\/g, "/");
|
|
1242
|
+
if (subdir === "" || subdir === ".." || subdir.startsWith("../") || isAbsolute(subdir)) {
|
|
1243
|
+
return void 0;
|
|
1244
|
+
}
|
|
1245
|
+
return subdir;
|
|
1246
|
+
}
|
|
1247
|
+
function pathToRoot(subdir) {
|
|
1248
|
+
return subdir.split("/").map(() => "..").join("/");
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1225
1251
|
// src/generators/compose.ts
|
|
1226
1252
|
function composeLiteral(value) {
|
|
1227
1253
|
return value.replace(/\$/g, "$$$$");
|
|
1228
1254
|
}
|
|
1255
|
+
function composeBuild(outputDir) {
|
|
1256
|
+
const subdir = outputSubdir(outputDir);
|
|
1257
|
+
if (subdir === void 0) {
|
|
1258
|
+
return ".";
|
|
1259
|
+
}
|
|
1260
|
+
return { context: pathToRoot(subdir), dockerfile: `${subdir}/Dockerfile` };
|
|
1261
|
+
}
|
|
1229
1262
|
function generateCompose(config, _context) {
|
|
1230
1263
|
const { app, services: services2 } = config;
|
|
1231
1264
|
const appService = {
|
|
1232
|
-
build:
|
|
1265
|
+
build: composeBuild(config.output.dir),
|
|
1233
1266
|
ports: [`${app.port}:${app.port}`],
|
|
1234
1267
|
environment: generateAppEnvironment(config),
|
|
1235
1268
|
depends_on: Object.keys(services2),
|
|
@@ -1248,13 +1281,20 @@ function generateCompose(config, _context) {
|
|
|
1248
1281
|
composeServices[serviceName] = generateServiceConfig(serviceConfig);
|
|
1249
1282
|
}
|
|
1250
1283
|
const compose = {
|
|
1251
|
-
version: "3.9",
|
|
1252
1284
|
services: composeServices,
|
|
1253
1285
|
volumes: generateVolumeDefinitions(config)
|
|
1254
1286
|
};
|
|
1255
1287
|
return `${toYaml(compose)}
|
|
1256
1288
|
`;
|
|
1257
1289
|
}
|
|
1290
|
+
function connectionStringVar(serviceName) {
|
|
1291
|
+
return `${serviceName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}_URL`;
|
|
1292
|
+
}
|
|
1293
|
+
function hasDatabaseUrlAlias(config) {
|
|
1294
|
+
return config.services.postgres !== void 0 && !Object.entries(config.services).some(
|
|
1295
|
+
([name, service]) => Boolean(service.connectionString) && connectionStringVar(name) === "DATABASE_URL"
|
|
1296
|
+
);
|
|
1297
|
+
}
|
|
1258
1298
|
function generateAppEnvironment(config) {
|
|
1259
1299
|
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1260
1300
|
const add = (key, value) => {
|
|
@@ -1262,12 +1302,14 @@ function generateAppEnvironment(config) {
|
|
|
1262
1302
|
};
|
|
1263
1303
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1264
1304
|
if (serviceConfig.connectionString) {
|
|
1265
|
-
|
|
1266
|
-
add(key, serviceConfig.connectionString);
|
|
1305
|
+
add(connectionStringVar(serviceName), serviceConfig.connectionString);
|
|
1267
1306
|
}
|
|
1268
1307
|
}
|
|
1269
1308
|
if (config.services.postgres) {
|
|
1270
1309
|
const pg = config.services.postgres;
|
|
1310
|
+
if (pg.connectionString && hasDatabaseUrlAlias(config)) {
|
|
1311
|
+
add("DATABASE_URL", pg.connectionString);
|
|
1312
|
+
}
|
|
1271
1313
|
add("POSTGRES_HOST", pg.name);
|
|
1272
1314
|
add("POSTGRES_PORT", pg.port);
|
|
1273
1315
|
add("POSTGRES_USER", pg.env.POSTGRES_USER || "postgres");
|
|
@@ -1351,7 +1393,19 @@ function generateVolumeDefinitions(config) {
|
|
|
1351
1393
|
}
|
|
1352
1394
|
|
|
1353
1395
|
// src/generators/dockerignore.ts
|
|
1354
|
-
|
|
1396
|
+
var DOCKERFILE_IGNORE_FILE = "Dockerfile.dockerignore";
|
|
1397
|
+
function dockerignoreFileName(outputDir) {
|
|
1398
|
+
return outputSubdir(outputDir) === void 0 ? ".dockerignore" : DOCKERFILE_IGNORE_FILE;
|
|
1399
|
+
}
|
|
1400
|
+
function outputDirPatterns(outputDir) {
|
|
1401
|
+
const subdir = outputSubdir(outputDir);
|
|
1402
|
+
if (subdir === void 0) {
|
|
1403
|
+
return "";
|
|
1404
|
+
}
|
|
1405
|
+
return ["Dockerfile", "docker-compose.yml", DOCKERFILE_IGNORE_FILE, ".env", ".env.*"].map((file) => `${subdir}/${file}
|
|
1406
|
+
`).join("");
|
|
1407
|
+
}
|
|
1408
|
+
function generateDockerignore(config) {
|
|
1355
1409
|
return `# Dependencies
|
|
1356
1410
|
node_modules/
|
|
1357
1411
|
npm-debug.log*
|
|
@@ -1413,7 +1467,7 @@ temp/
|
|
|
1413
1467
|
Dockerfile
|
|
1414
1468
|
docker-compose.yml
|
|
1415
1469
|
.dockerignore
|
|
1416
|
-
|
|
1470
|
+
${outputDirPatterns(config.output.dir)}
|
|
1417
1471
|
# Package manager lockfiles (optional - uncomment if you want to exclude)
|
|
1418
1472
|
# package-lock.json
|
|
1419
1473
|
# yarn.lock
|
|
@@ -1422,6 +1476,7 @@ docker-compose.yml
|
|
|
1422
1476
|
}
|
|
1423
1477
|
|
|
1424
1478
|
// src/generators/env.ts
|
|
1479
|
+
var BUILT_IN_SERVICES = /* @__PURE__ */ new Set(["postgres", "mysql", "mongo", "redis"]);
|
|
1425
1480
|
function envValue(value) {
|
|
1426
1481
|
if (/^[\w@%+=:,./?&-]*$/.test(value)) {
|
|
1427
1482
|
return value;
|
|
@@ -1451,7 +1506,11 @@ PORT=${config.app.port}
|
|
|
1451
1506
|
content += `
|
|
1452
1507
|
# PostgreSQL
|
|
1453
1508
|
`;
|
|
1454
|
-
|
|
1509
|
+
if (hasDatabaseUrlAlias(config)) {
|
|
1510
|
+
content += `DATABASE_URL=${envValue(String(pg.connectionString || ""))}
|
|
1511
|
+
`;
|
|
1512
|
+
}
|
|
1513
|
+
content += `POSTGRES_URL=${envValue(String(pg.connectionString || ""))}
|
|
1455
1514
|
`;
|
|
1456
1515
|
content += `POSTGRES_HOST=${envValue(String(pg.name))}
|
|
1457
1516
|
`;
|
|
@@ -1513,6 +1572,15 @@ PORT=${config.app.port}
|
|
|
1513
1572
|
`;
|
|
1514
1573
|
if (redis.env.REDIS_PASSWORD) {
|
|
1515
1574
|
content += `REDIS_PASSWORD=${envValue(String(redis.env.REDIS_PASSWORD))}
|
|
1575
|
+
`;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
for (const [serviceName, service] of Object.entries(config.services)) {
|
|
1579
|
+
if (!BUILT_IN_SERVICES.has(serviceName) && service.connectionString) {
|
|
1580
|
+
content += `
|
|
1581
|
+
# ${serviceName}
|
|
1582
|
+
`;
|
|
1583
|
+
content += `${connectionStringVar(serviceName)}=${envValue(service.connectionString)}
|
|
1516
1584
|
`;
|
|
1517
1585
|
}
|
|
1518
1586
|
}
|
|
@@ -1666,7 +1734,8 @@ function generateAll(config, context) {
|
|
|
1666
1734
|
} else {
|
|
1667
1735
|
const generator = GENERATORS[fileType];
|
|
1668
1736
|
if (generator) {
|
|
1669
|
-
|
|
1737
|
+
const file = fileType === "dockerignore" ? dockerignoreFileName(config.output.dir) : generator.file;
|
|
1738
|
+
result[file] = generator.generate(config, context);
|
|
1670
1739
|
}
|
|
1671
1740
|
}
|
|
1672
1741
|
}
|
|
@@ -1675,7 +1744,7 @@ function generateAll(config, context) {
|
|
|
1675
1744
|
|
|
1676
1745
|
// src/utils/fs.ts
|
|
1677
1746
|
import { promises as fs } from "fs";
|
|
1678
|
-
import { join, dirname, basename, extname, relative } from "path";
|
|
1747
|
+
import { join as join2, dirname, basename, extname, relative as relative2 } from "path";
|
|
1679
1748
|
async function ensureDir(path) {
|
|
1680
1749
|
try {
|
|
1681
1750
|
await fs.mkdir(path, { recursive: true });
|
|
@@ -1687,10 +1756,17 @@ async function ensureDir(path) {
|
|
|
1687
1756
|
}
|
|
1688
1757
|
}
|
|
1689
1758
|
async function writeFile(filePath, content, mode) {
|
|
1759
|
+
try {
|
|
1760
|
+
await writeFileAtomic(filePath, content, mode);
|
|
1761
|
+
} catch (error) {
|
|
1762
|
+
throw new FileWriteError(filePath, "FILE_WRITE_ERROR", error);
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
async function writeFileAtomic(filePath, content, mode) {
|
|
1690
1766
|
const dir = dirname(filePath);
|
|
1691
1767
|
await ensureDir(dir);
|
|
1692
1768
|
const suffix = `${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2)}`;
|
|
1693
|
-
const tempPath =
|
|
1769
|
+
const tempPath = join2(dir, `.${basename(filePath)}.${suffix}.tmp`);
|
|
1694
1770
|
try {
|
|
1695
1771
|
await fs.writeFile(tempPath, content, { encoding: "utf8", mode });
|
|
1696
1772
|
if (mode !== void 0) {
|
|
@@ -1713,7 +1789,7 @@ async function fileExists(filePath) {
|
|
|
1713
1789
|
}
|
|
1714
1790
|
|
|
1715
1791
|
// src/core/dockcraft.ts
|
|
1716
|
-
import { isAbsolute, join as
|
|
1792
|
+
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
1717
1793
|
function validationError(prefix, errors) {
|
|
1718
1794
|
const errorMessages = errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1719
1795
|
return new ValidationError(`${prefix}:
|
|
@@ -1721,7 +1797,7 @@ ${errorMessages}`, errors);
|
|
|
1721
1797
|
}
|
|
1722
1798
|
function isSafeOutputPath(fileName) {
|
|
1723
1799
|
if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
|
|
1724
|
-
if (
|
|
1800
|
+
if (isAbsolute2(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
|
|
1725
1801
|
const normalized = normalize(fileName.replace(/\\/g, "/"));
|
|
1726
1802
|
return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
|
|
1727
1803
|
}
|
|
@@ -1767,7 +1843,7 @@ async function dockcraft(config, options = {}) {
|
|
|
1767
1843
|
const outputDir = resolved.output.dir ?? "./";
|
|
1768
1844
|
if (!options.dryRun) {
|
|
1769
1845
|
for (const [fileName, content] of Object.entries(generated)) {
|
|
1770
|
-
const filePath =
|
|
1846
|
+
const filePath = join3(outputDir, fileName);
|
|
1771
1847
|
const exists = await fileExists(filePath);
|
|
1772
1848
|
if (exists && !resolved.output.overwrite) {
|
|
1773
1849
|
continue;
|