@oxog/dockcraft 1.1.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 +37 -0
- package/README.md +20 -1
- package/dist/cli/index.js +156 -54
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +93 -20
- 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 +91 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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,7 @@ async function resolvePort(options = {}) {
|
|
|
499
502
|
if (await isPortAvailable(preferred)) {
|
|
500
503
|
return preferred;
|
|
501
504
|
}
|
|
502
|
-
throw new
|
|
505
|
+
throw new PortConflictError(preferred);
|
|
503
506
|
case "range":
|
|
504
507
|
if (range) {
|
|
505
508
|
const [start, end = start + 100] = range;
|
|
@@ -507,7 +510,7 @@ async function resolvePort(options = {}) {
|
|
|
507
510
|
if (port !== void 0) {
|
|
508
511
|
return port;
|
|
509
512
|
}
|
|
510
|
-
throw new
|
|
513
|
+
throw new NoPortAvailableError([start, end]);
|
|
511
514
|
}
|
|
512
515
|
// Fall through to auto if no range specified
|
|
513
516
|
case "auto":
|
|
@@ -516,7 +519,7 @@ async function resolvePort(options = {}) {
|
|
|
516
519
|
if (port !== void 0) {
|
|
517
520
|
return port;
|
|
518
521
|
}
|
|
519
|
-
throw new
|
|
522
|
+
throw new NoPortAvailableError([preferred, 65535]);
|
|
520
523
|
}
|
|
521
524
|
}
|
|
522
525
|
}
|
|
@@ -1222,14 +1225,41 @@ function yamlScalar(str) {
|
|
|
1222
1225
|
return JSON.stringify(str);
|
|
1223
1226
|
}
|
|
1224
1227
|
|
|
1228
|
+
// src/generators/paths.ts
|
|
1229
|
+
import { isAbsolute, join, relative } from "path";
|
|
1230
|
+
function currentDir() {
|
|
1231
|
+
return typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : "/";
|
|
1232
|
+
}
|
|
1233
|
+
function outputSubdir(dir) {
|
|
1234
|
+
if (!dir) {
|
|
1235
|
+
return void 0;
|
|
1236
|
+
}
|
|
1237
|
+
const root = currentDir();
|
|
1238
|
+
const subdir = relative(root, isAbsolute(dir) ? dir : join(root, dir)).replace(/\\/g, "/");
|
|
1239
|
+
if (subdir === "" || subdir === ".." || subdir.startsWith("../") || isAbsolute(subdir)) {
|
|
1240
|
+
return void 0;
|
|
1241
|
+
}
|
|
1242
|
+
return subdir;
|
|
1243
|
+
}
|
|
1244
|
+
function pathToRoot(subdir) {
|
|
1245
|
+
return subdir.split("/").map(() => "..").join("/");
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1225
1248
|
// src/generators/compose.ts
|
|
1226
1249
|
function composeLiteral(value) {
|
|
1227
1250
|
return value.replace(/\$/g, "$$$$");
|
|
1228
1251
|
}
|
|
1252
|
+
function composeBuild(outputDir) {
|
|
1253
|
+
const subdir = outputSubdir(outputDir);
|
|
1254
|
+
if (subdir === void 0) {
|
|
1255
|
+
return ".";
|
|
1256
|
+
}
|
|
1257
|
+
return { context: pathToRoot(subdir), dockerfile: `${subdir}/Dockerfile` };
|
|
1258
|
+
}
|
|
1229
1259
|
function generateCompose(config, _context) {
|
|
1230
1260
|
const { app, services: services2 } = config;
|
|
1231
1261
|
const appService = {
|
|
1232
|
-
build:
|
|
1262
|
+
build: composeBuild(config.output.dir),
|
|
1233
1263
|
ports: [`${app.port}:${app.port}`],
|
|
1234
1264
|
environment: generateAppEnvironment(config),
|
|
1235
1265
|
depends_on: Object.keys(services2),
|
|
@@ -1248,13 +1278,20 @@ function generateCompose(config, _context) {
|
|
|
1248
1278
|
composeServices[serviceName] = generateServiceConfig(serviceConfig);
|
|
1249
1279
|
}
|
|
1250
1280
|
const compose = {
|
|
1251
|
-
version: "3.9",
|
|
1252
1281
|
services: composeServices,
|
|
1253
1282
|
volumes: generateVolumeDefinitions(config)
|
|
1254
1283
|
};
|
|
1255
1284
|
return `${toYaml(compose)}
|
|
1256
1285
|
`;
|
|
1257
1286
|
}
|
|
1287
|
+
function connectionStringVar(serviceName) {
|
|
1288
|
+
return `${serviceName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}_URL`;
|
|
1289
|
+
}
|
|
1290
|
+
function hasDatabaseUrlAlias(config) {
|
|
1291
|
+
return config.services.postgres !== void 0 && !Object.entries(config.services).some(
|
|
1292
|
+
([name, service]) => Boolean(service.connectionString) && connectionStringVar(name) === "DATABASE_URL"
|
|
1293
|
+
);
|
|
1294
|
+
}
|
|
1258
1295
|
function generateAppEnvironment(config) {
|
|
1259
1296
|
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1260
1297
|
const add = (key, value) => {
|
|
@@ -1262,12 +1299,14 @@ function generateAppEnvironment(config) {
|
|
|
1262
1299
|
};
|
|
1263
1300
|
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1264
1301
|
if (serviceConfig.connectionString) {
|
|
1265
|
-
|
|
1266
|
-
add(key, serviceConfig.connectionString);
|
|
1302
|
+
add(connectionStringVar(serviceName), serviceConfig.connectionString);
|
|
1267
1303
|
}
|
|
1268
1304
|
}
|
|
1269
1305
|
if (config.services.postgres) {
|
|
1270
1306
|
const pg = config.services.postgres;
|
|
1307
|
+
if (pg.connectionString && hasDatabaseUrlAlias(config)) {
|
|
1308
|
+
add("DATABASE_URL", pg.connectionString);
|
|
1309
|
+
}
|
|
1271
1310
|
add("POSTGRES_HOST", pg.name);
|
|
1272
1311
|
add("POSTGRES_PORT", pg.port);
|
|
1273
1312
|
add("POSTGRES_USER", pg.env.POSTGRES_USER || "postgres");
|
|
@@ -1351,7 +1390,19 @@ function generateVolumeDefinitions(config) {
|
|
|
1351
1390
|
}
|
|
1352
1391
|
|
|
1353
1392
|
// src/generators/dockerignore.ts
|
|
1354
|
-
|
|
1393
|
+
var DOCKERFILE_IGNORE_FILE = "Dockerfile.dockerignore";
|
|
1394
|
+
function dockerignoreFileName(outputDir) {
|
|
1395
|
+
return outputSubdir(outputDir) === void 0 ? ".dockerignore" : DOCKERFILE_IGNORE_FILE;
|
|
1396
|
+
}
|
|
1397
|
+
function outputDirPatterns(outputDir) {
|
|
1398
|
+
const subdir = outputSubdir(outputDir);
|
|
1399
|
+
if (subdir === void 0) {
|
|
1400
|
+
return "";
|
|
1401
|
+
}
|
|
1402
|
+
return ["Dockerfile", "docker-compose.yml", DOCKERFILE_IGNORE_FILE, ".env", ".env.*"].map((file) => `${subdir}/${file}
|
|
1403
|
+
`).join("");
|
|
1404
|
+
}
|
|
1405
|
+
function generateDockerignore(config) {
|
|
1355
1406
|
return `# Dependencies
|
|
1356
1407
|
node_modules/
|
|
1357
1408
|
npm-debug.log*
|
|
@@ -1413,7 +1464,7 @@ temp/
|
|
|
1413
1464
|
Dockerfile
|
|
1414
1465
|
docker-compose.yml
|
|
1415
1466
|
.dockerignore
|
|
1416
|
-
|
|
1467
|
+
${outputDirPatterns(config.output.dir)}
|
|
1417
1468
|
# Package manager lockfiles (optional - uncomment if you want to exclude)
|
|
1418
1469
|
# package-lock.json
|
|
1419
1470
|
# yarn.lock
|
|
@@ -1422,6 +1473,7 @@ docker-compose.yml
|
|
|
1422
1473
|
}
|
|
1423
1474
|
|
|
1424
1475
|
// src/generators/env.ts
|
|
1476
|
+
var BUILT_IN_SERVICES = /* @__PURE__ */ new Set(["postgres", "mysql", "mongo", "redis"]);
|
|
1425
1477
|
function envValue(value) {
|
|
1426
1478
|
if (/^[\w@%+=:,./?&-]*$/.test(value)) {
|
|
1427
1479
|
return value;
|
|
@@ -1451,7 +1503,11 @@ PORT=${config.app.port}
|
|
|
1451
1503
|
content += `
|
|
1452
1504
|
# PostgreSQL
|
|
1453
1505
|
`;
|
|
1454
|
-
|
|
1506
|
+
if (hasDatabaseUrlAlias(config)) {
|
|
1507
|
+
content += `DATABASE_URL=${envValue(String(pg.connectionString || ""))}
|
|
1508
|
+
`;
|
|
1509
|
+
}
|
|
1510
|
+
content += `POSTGRES_URL=${envValue(String(pg.connectionString || ""))}
|
|
1455
1511
|
`;
|
|
1456
1512
|
content += `POSTGRES_HOST=${envValue(String(pg.name))}
|
|
1457
1513
|
`;
|
|
@@ -1513,6 +1569,15 @@ PORT=${config.app.port}
|
|
|
1513
1569
|
`;
|
|
1514
1570
|
if (redis.env.REDIS_PASSWORD) {
|
|
1515
1571
|
content += `REDIS_PASSWORD=${envValue(String(redis.env.REDIS_PASSWORD))}
|
|
1572
|
+
`;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
for (const [serviceName, service] of Object.entries(config.services)) {
|
|
1576
|
+
if (!BUILT_IN_SERVICES.has(serviceName) && service.connectionString) {
|
|
1577
|
+
content += `
|
|
1578
|
+
# ${serviceName}
|
|
1579
|
+
`;
|
|
1580
|
+
content += `${connectionStringVar(serviceName)}=${envValue(service.connectionString)}
|
|
1516
1581
|
`;
|
|
1517
1582
|
}
|
|
1518
1583
|
}
|
|
@@ -1666,7 +1731,8 @@ function generateAll(config, context) {
|
|
|
1666
1731
|
} else {
|
|
1667
1732
|
const generator = GENERATORS[fileType];
|
|
1668
1733
|
if (generator) {
|
|
1669
|
-
|
|
1734
|
+
const file = fileType === "dockerignore" ? dockerignoreFileName(config.output.dir) : generator.file;
|
|
1735
|
+
result[file] = generator.generate(config, context);
|
|
1670
1736
|
}
|
|
1671
1737
|
}
|
|
1672
1738
|
}
|
|
@@ -1675,7 +1741,7 @@ function generateAll(config, context) {
|
|
|
1675
1741
|
|
|
1676
1742
|
// src/utils/fs.ts
|
|
1677
1743
|
import { promises as fs } from "fs";
|
|
1678
|
-
import { join, dirname, basename, extname, relative } from "path";
|
|
1744
|
+
import { join as join2, dirname, basename, extname, relative as relative2 } from "path";
|
|
1679
1745
|
async function ensureDir(path) {
|
|
1680
1746
|
try {
|
|
1681
1747
|
await fs.mkdir(path, { recursive: true });
|
|
@@ -1687,10 +1753,17 @@ async function ensureDir(path) {
|
|
|
1687
1753
|
}
|
|
1688
1754
|
}
|
|
1689
1755
|
async function writeFile(filePath, content, mode) {
|
|
1756
|
+
try {
|
|
1757
|
+
await writeFileAtomic(filePath, content, mode);
|
|
1758
|
+
} catch (error) {
|
|
1759
|
+
throw new FileWriteError(filePath, "FILE_WRITE_ERROR", error);
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
async function writeFileAtomic(filePath, content, mode) {
|
|
1690
1763
|
const dir = dirname(filePath);
|
|
1691
1764
|
await ensureDir(dir);
|
|
1692
1765
|
const suffix = `${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2)}`;
|
|
1693
|
-
const tempPath =
|
|
1766
|
+
const tempPath = join2(dir, `.${basename(filePath)}.${suffix}.tmp`);
|
|
1694
1767
|
try {
|
|
1695
1768
|
await fs.writeFile(tempPath, content, { encoding: "utf8", mode });
|
|
1696
1769
|
if (mode !== void 0) {
|
|
@@ -1713,7 +1786,7 @@ async function fileExists(filePath) {
|
|
|
1713
1786
|
}
|
|
1714
1787
|
|
|
1715
1788
|
// src/core/dockcraft.ts
|
|
1716
|
-
import { isAbsolute, join as
|
|
1789
|
+
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
1717
1790
|
function validationError(prefix, errors) {
|
|
1718
1791
|
const errorMessages = errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1719
1792
|
return new ValidationError(`${prefix}:
|
|
@@ -1721,7 +1794,7 @@ ${errorMessages}`, errors);
|
|
|
1721
1794
|
}
|
|
1722
1795
|
function isSafeOutputPath(fileName) {
|
|
1723
1796
|
if (typeof fileName !== "string" || fileName === "" || fileName.includes("\0")) return false;
|
|
1724
|
-
if (
|
|
1797
|
+
if (isAbsolute2(fileName) || /^[a-zA-Z]:/.test(fileName) || fileName.startsWith("\\")) return false;
|
|
1725
1798
|
const normalized = normalize(fileName.replace(/\\/g, "/"));
|
|
1726
1799
|
return !normalized.split(/[\\/]/).includes("..") && normalized !== ".";
|
|
1727
1800
|
}
|
|
@@ -1767,7 +1840,7 @@ async function dockcraft(config, options = {}) {
|
|
|
1767
1840
|
const outputDir = resolved.output.dir ?? "./";
|
|
1768
1841
|
if (!options.dryRun) {
|
|
1769
1842
|
for (const [fileName, content] of Object.entries(generated)) {
|
|
1770
|
-
const filePath =
|
|
1843
|
+
const filePath = join3(outputDir, fileName);
|
|
1771
1844
|
const exists = await fileExists(filePath);
|
|
1772
1845
|
if (exists && !resolved.output.overwrite) {
|
|
1773
1846
|
continue;
|