@oxog/dockcraft 1.0.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/LICENSE +21 -0
- package/README.md +336 -0
- package/dist/cli/index.cjs +2095 -0
- package/dist/cli/index.cjs.map +1 -0
- package/dist/cli/index.d.cts +6 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +2065 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.cjs +1651 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +446 -0
- package/dist/index.js +1604 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1651 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oxog/dockcraft - Zero-Dependency NPM Package
|
|
3
|
+
* GitHub: https://github.com/ersinkoc/dockcraft
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var index_exports = {};
|
|
27
|
+
__export(index_exports, {
|
|
28
|
+
ConfigError: () => ConfigError,
|
|
29
|
+
DockcraftError: () => DockcraftError,
|
|
30
|
+
FileWriteError: () => FileWriteError,
|
|
31
|
+
NoPortAvailableError: () => NoPortAvailableError,
|
|
32
|
+
PortConflictError: () => PortConflictError,
|
|
33
|
+
PresetNotFoundError: () => PresetNotFoundError,
|
|
34
|
+
ServiceNotFoundError: () => ServiceNotFoundError,
|
|
35
|
+
ValidationErrorClass: () => ValidationError,
|
|
36
|
+
checkPorts: () => checkPorts,
|
|
37
|
+
defineConfig: () => defineConfig,
|
|
38
|
+
defineService: () => defineService,
|
|
39
|
+
dockcraft: () => dockcraft,
|
|
40
|
+
getConnectionString: () => getConnectionString,
|
|
41
|
+
getDefaultPort: () => getDefaultPort,
|
|
42
|
+
getPreset: () => getPreset,
|
|
43
|
+
getService: () => getService,
|
|
44
|
+
isPortAvailable: () => isPortAvailable,
|
|
45
|
+
listDefaultPorts: () => listDefaultPorts,
|
|
46
|
+
listPresets: () => listPresets,
|
|
47
|
+
listServices: () => listServices,
|
|
48
|
+
registerService: () => registerService,
|
|
49
|
+
resolvePort: () => resolvePort
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(index_exports);
|
|
52
|
+
|
|
53
|
+
// src/presets/express-postgres.ts
|
|
54
|
+
var expressPostgresPreset = {
|
|
55
|
+
description: "Express application with PostgreSQL database",
|
|
56
|
+
app: {
|
|
57
|
+
port: 3e3,
|
|
58
|
+
node: "20-alpine",
|
|
59
|
+
workdir: "/app",
|
|
60
|
+
command: "npm start",
|
|
61
|
+
buildCommand: "npm run build",
|
|
62
|
+
multiStage: true,
|
|
63
|
+
healthcheck: "/health"
|
|
64
|
+
},
|
|
65
|
+
services: {
|
|
66
|
+
postgres: {
|
|
67
|
+
version: "16"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/presets/express-postgres-redis.ts
|
|
73
|
+
var expressPostgresRedisPreset = {
|
|
74
|
+
description: "Express application with PostgreSQL database and Redis cache",
|
|
75
|
+
app: {
|
|
76
|
+
port: 3e3,
|
|
77
|
+
node: "20-alpine",
|
|
78
|
+
workdir: "/app",
|
|
79
|
+
command: "npm start",
|
|
80
|
+
buildCommand: "npm run build",
|
|
81
|
+
multiStage: true,
|
|
82
|
+
healthcheck: "/health"
|
|
83
|
+
},
|
|
84
|
+
services: {
|
|
85
|
+
postgres: {
|
|
86
|
+
version: "16"
|
|
87
|
+
},
|
|
88
|
+
redis: {
|
|
89
|
+
version: "7-alpine"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/presets/express-mongo.ts
|
|
95
|
+
var expressMongoPreset = {
|
|
96
|
+
description: "Express application with MongoDB database",
|
|
97
|
+
app: {
|
|
98
|
+
port: 3e3,
|
|
99
|
+
node: "20-alpine",
|
|
100
|
+
workdir: "/app",
|
|
101
|
+
command: "npm start",
|
|
102
|
+
buildCommand: "npm run build",
|
|
103
|
+
multiStage: true,
|
|
104
|
+
healthcheck: "/health"
|
|
105
|
+
},
|
|
106
|
+
services: {
|
|
107
|
+
mongo: {
|
|
108
|
+
version: "7"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// src/presets/express-mongo-redis.ts
|
|
114
|
+
var expressMongoRedisPreset = {
|
|
115
|
+
description: "Express application with MongoDB database and Redis cache",
|
|
116
|
+
app: {
|
|
117
|
+
port: 3e3,
|
|
118
|
+
node: "20-alpine",
|
|
119
|
+
workdir: "/app",
|
|
120
|
+
command: "npm start",
|
|
121
|
+
buildCommand: "npm run build",
|
|
122
|
+
multiStage: true,
|
|
123
|
+
healthcheck: "/health"
|
|
124
|
+
},
|
|
125
|
+
services: {
|
|
126
|
+
mongo: {
|
|
127
|
+
version: "7"
|
|
128
|
+
},
|
|
129
|
+
redis: {
|
|
130
|
+
version: "7-alpine"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/presets/express-mysql.ts
|
|
136
|
+
var expressMysqlPreset = {
|
|
137
|
+
description: "Express application with MySQL database",
|
|
138
|
+
app: {
|
|
139
|
+
port: 3e3,
|
|
140
|
+
node: "20-alpine",
|
|
141
|
+
workdir: "/app",
|
|
142
|
+
command: "npm start",
|
|
143
|
+
buildCommand: "npm run build",
|
|
144
|
+
multiStage: true,
|
|
145
|
+
healthcheck: "/health"
|
|
146
|
+
},
|
|
147
|
+
services: {
|
|
148
|
+
mysql: {
|
|
149
|
+
version: "8"
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// src/presets/express-mysql-redis.ts
|
|
155
|
+
var expressMysqlRedisPreset = {
|
|
156
|
+
description: "Express application with MySQL database and Redis cache",
|
|
157
|
+
app: {
|
|
158
|
+
port: 3e3,
|
|
159
|
+
node: "20-alpine",
|
|
160
|
+
workdir: "/app",
|
|
161
|
+
command: "npm start",
|
|
162
|
+
buildCommand: "npm run build",
|
|
163
|
+
multiStage: true,
|
|
164
|
+
healthcheck: "/health"
|
|
165
|
+
},
|
|
166
|
+
services: {
|
|
167
|
+
mysql: {
|
|
168
|
+
version: "8"
|
|
169
|
+
},
|
|
170
|
+
redis: {
|
|
171
|
+
version: "7-alpine"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/presets/fastify-postgres.ts
|
|
177
|
+
var fastifyPostgresPreset = {
|
|
178
|
+
description: "Fastify application with PostgreSQL database",
|
|
179
|
+
app: {
|
|
180
|
+
port: 3e3,
|
|
181
|
+
node: "20-alpine",
|
|
182
|
+
workdir: "/app",
|
|
183
|
+
command: "npm start",
|
|
184
|
+
buildCommand: "npm run build",
|
|
185
|
+
multiStage: true,
|
|
186
|
+
healthcheck: "/health"
|
|
187
|
+
},
|
|
188
|
+
services: {
|
|
189
|
+
postgres: {
|
|
190
|
+
version: "16"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/presets/fastify-postgres-redis.ts
|
|
196
|
+
var fastifyPostgresRedisPreset = {
|
|
197
|
+
description: "Fastify application with PostgreSQL database and Redis cache",
|
|
198
|
+
app: {
|
|
199
|
+
port: 3e3,
|
|
200
|
+
node: "20-alpine",
|
|
201
|
+
workdir: "/app",
|
|
202
|
+
command: "npm start",
|
|
203
|
+
buildCommand: "npm run build",
|
|
204
|
+
multiStage: true,
|
|
205
|
+
healthcheck: "/health"
|
|
206
|
+
},
|
|
207
|
+
services: {
|
|
208
|
+
postgres: {
|
|
209
|
+
version: "16"
|
|
210
|
+
},
|
|
211
|
+
redis: {
|
|
212
|
+
version: "7-alpine"
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// src/presets/nest-postgres.ts
|
|
218
|
+
var nestPostgresPreset = {
|
|
219
|
+
description: "NestJS application with PostgreSQL database",
|
|
220
|
+
app: {
|
|
221
|
+
port: 3e3,
|
|
222
|
+
node: "20-alpine",
|
|
223
|
+
workdir: "/app",
|
|
224
|
+
command: "npm run start:prod",
|
|
225
|
+
buildCommand: "npm run build",
|
|
226
|
+
multiStage: true,
|
|
227
|
+
healthcheck: "/health"
|
|
228
|
+
},
|
|
229
|
+
services: {
|
|
230
|
+
postgres: {
|
|
231
|
+
version: "16"
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/presets/nest-mongo.ts
|
|
237
|
+
var nestMongoPreset = {
|
|
238
|
+
description: "NestJS application with MongoDB database",
|
|
239
|
+
app: {
|
|
240
|
+
port: 3e3,
|
|
241
|
+
node: "20-alpine",
|
|
242
|
+
workdir: "/app",
|
|
243
|
+
command: "npm run start:prod",
|
|
244
|
+
buildCommand: "npm run build",
|
|
245
|
+
multiStage: true,
|
|
246
|
+
healthcheck: "/health"
|
|
247
|
+
},
|
|
248
|
+
services: {
|
|
249
|
+
mongo: {
|
|
250
|
+
version: "7"
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/presets/node-minimal.ts
|
|
256
|
+
var nodeMinimalPreset = {
|
|
257
|
+
description: "Minimal Node.js application setup",
|
|
258
|
+
app: {
|
|
259
|
+
port: 3e3,
|
|
260
|
+
node: "20-alpine",
|
|
261
|
+
workdir: "/app",
|
|
262
|
+
command: "npm start",
|
|
263
|
+
multiStage: true
|
|
264
|
+
},
|
|
265
|
+
services: {}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// src/presets/index.ts
|
|
269
|
+
var presets = /* @__PURE__ */ new Map();
|
|
270
|
+
function registerPreset(name, config) {
|
|
271
|
+
presets.set(name, config);
|
|
272
|
+
}
|
|
273
|
+
function getPreset(name) {
|
|
274
|
+
return presets.get(name);
|
|
275
|
+
}
|
|
276
|
+
function listPresets() {
|
|
277
|
+
return Array.from(presets.keys());
|
|
278
|
+
}
|
|
279
|
+
registerPreset("express-postgres", expressPostgresPreset);
|
|
280
|
+
registerPreset("express-postgres-redis", expressPostgresRedisPreset);
|
|
281
|
+
registerPreset("express-mongo", expressMongoPreset);
|
|
282
|
+
registerPreset("express-mongo-redis", expressMongoRedisPreset);
|
|
283
|
+
registerPreset("express-mysql", expressMysqlPreset);
|
|
284
|
+
registerPreset("express-mysql-redis", expressMysqlRedisPreset);
|
|
285
|
+
registerPreset("fastify-postgres", fastifyPostgresPreset);
|
|
286
|
+
registerPreset("fastify-postgres-redis", fastifyPostgresRedisPreset);
|
|
287
|
+
registerPreset("nest-postgres", nestPostgresPreset);
|
|
288
|
+
registerPreset("nest-mongo", nestMongoPreset);
|
|
289
|
+
registerPreset("node-minimal", nodeMinimalPreset);
|
|
290
|
+
|
|
291
|
+
// src/services/postgres.ts
|
|
292
|
+
var postgresService = {
|
|
293
|
+
name: "postgres",
|
|
294
|
+
image: (version) => `postgres:${version}-alpine`,
|
|
295
|
+
defaultPort: 5432,
|
|
296
|
+
defaultVersion: "16",
|
|
297
|
+
env: {
|
|
298
|
+
POSTGRES_USER: "postgres",
|
|
299
|
+
POSTGRES_PASSWORD: "postgres",
|
|
300
|
+
POSTGRES_DB: "myapp"
|
|
301
|
+
},
|
|
302
|
+
volumes: ["postgres_data:/var/lib/postgresql/data"],
|
|
303
|
+
healthcheck: "pg_isready -U postgres",
|
|
304
|
+
ports: [],
|
|
305
|
+
connectionString: (config) => {
|
|
306
|
+
const user = config.env.POSTGRES_USER || "postgres";
|
|
307
|
+
const password = config.env.POSTGRES_PASSWORD || "postgres";
|
|
308
|
+
const database = config.env.POSTGRES_DB || "myapp";
|
|
309
|
+
const host = config.name;
|
|
310
|
+
const port = config.port;
|
|
311
|
+
return `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/services/mysql.ts
|
|
316
|
+
var mysqlService = {
|
|
317
|
+
name: "mysql",
|
|
318
|
+
image: (version) => `mysql:${version}`,
|
|
319
|
+
defaultPort: 3306,
|
|
320
|
+
defaultVersion: "8",
|
|
321
|
+
env: {
|
|
322
|
+
MYSQL_ROOT_PASSWORD: "root",
|
|
323
|
+
MYSQL_DATABASE: "myapp",
|
|
324
|
+
MYSQL_USER: "mysql",
|
|
325
|
+
MYSQL_PASSWORD: "mysql"
|
|
326
|
+
},
|
|
327
|
+
volumes: ["mysql_data:/var/lib/mysql"],
|
|
328
|
+
healthcheck: "mysqladmin ping -h localhost",
|
|
329
|
+
ports: [],
|
|
330
|
+
connectionString: (config) => {
|
|
331
|
+
const user = config.env.MYSQL_USER || "root";
|
|
332
|
+
const password = config.env.MYSQL_PASSWORD || config.env.MYSQL_ROOT_PASSWORD || "root";
|
|
333
|
+
const database = config.env.MYSQL_DATABASE || "myapp";
|
|
334
|
+
const host = config.name;
|
|
335
|
+
const port = config.port;
|
|
336
|
+
return `mysql://${user}:${password}@${host}:${port}/${database}`;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// src/services/mongo.ts
|
|
341
|
+
var mongoService = {
|
|
342
|
+
name: "mongo",
|
|
343
|
+
image: (version) => `mongo:${version}`,
|
|
344
|
+
defaultPort: 27017,
|
|
345
|
+
defaultVersion: "7",
|
|
346
|
+
env: {
|
|
347
|
+
MONGO_INITDB_ROOT_USERNAME: "mongo",
|
|
348
|
+
MONGO_INITDB_ROOT_PASSWORD: "mongo",
|
|
349
|
+
MONGO_INITDB_DATABASE: "myapp"
|
|
350
|
+
},
|
|
351
|
+
volumes: ["mongo_data:/data/db"],
|
|
352
|
+
healthcheck: 'mongosh --eval "db.adminCommand({ ping: 1 })"',
|
|
353
|
+
ports: [],
|
|
354
|
+
connectionString: (config) => {
|
|
355
|
+
const user = config.env.MONGO_INITDB_ROOT_USERNAME || "mongo";
|
|
356
|
+
const password = config.env.MONGO_INITDB_ROOT_PASSWORD || "mongo";
|
|
357
|
+
const database = config.env.MONGO_INITDB_DATABASE || "myapp";
|
|
358
|
+
const host = config.name;
|
|
359
|
+
const port = config.port;
|
|
360
|
+
return `mongodb://${user}:${password}@${host}:${port}/${database}`;
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// src/services/redis.ts
|
|
365
|
+
var redisService = {
|
|
366
|
+
name: "redis",
|
|
367
|
+
image: (version) => `redis:${version}-alpine`,
|
|
368
|
+
defaultPort: 6379,
|
|
369
|
+
defaultVersion: "7",
|
|
370
|
+
env: {},
|
|
371
|
+
volumes: ["redis_data:/data"],
|
|
372
|
+
healthcheck: "redis-cli ping",
|
|
373
|
+
ports: [],
|
|
374
|
+
connectionString: (config) => {
|
|
375
|
+
const host = config.name;
|
|
376
|
+
const port = config.port;
|
|
377
|
+
return `redis://${host}:${port}`;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
// src/services/index.ts
|
|
382
|
+
var services = /* @__PURE__ */ new Map();
|
|
383
|
+
function registerService(def) {
|
|
384
|
+
services.set(def.name, def);
|
|
385
|
+
}
|
|
386
|
+
function getService(name) {
|
|
387
|
+
return services.get(name);
|
|
388
|
+
}
|
|
389
|
+
function listServices() {
|
|
390
|
+
return Array.from(services.keys());
|
|
391
|
+
}
|
|
392
|
+
registerService(postgresService);
|
|
393
|
+
registerService(mysqlService);
|
|
394
|
+
registerService(mongoService);
|
|
395
|
+
registerService(redisService);
|
|
396
|
+
|
|
397
|
+
// src/utils/merge.ts
|
|
398
|
+
function deepMerge(...objects) {
|
|
399
|
+
const result = {};
|
|
400
|
+
for (const obj of objects) {
|
|
401
|
+
if (obj === null || typeof obj !== "object") continue;
|
|
402
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
403
|
+
if (value === void 0) continue;
|
|
404
|
+
if (value && typeof value === "object" && !Array.isArray(value) && result[key] && typeof result[key] === "object" && !Array.isArray(result[key])) {
|
|
405
|
+
result[key] = deepMerge(result[key], value);
|
|
406
|
+
} else {
|
|
407
|
+
result[key] = value;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return result;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/ports/scanner.ts
|
|
415
|
+
var import_net = require("net");
|
|
416
|
+
async function isPortAvailable(port) {
|
|
417
|
+
return new Promise((resolve) => {
|
|
418
|
+
const server = (0, import_net.createServer)();
|
|
419
|
+
server.once("error", () => {
|
|
420
|
+
resolve(false);
|
|
421
|
+
});
|
|
422
|
+
server.once("listening", () => {
|
|
423
|
+
server.close(() => {
|
|
424
|
+
resolve(true);
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
server.listen(port, "127.0.0.1");
|
|
428
|
+
setTimeout(() => {
|
|
429
|
+
server.close();
|
|
430
|
+
resolve(false);
|
|
431
|
+
}, 1e3);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
async function checkPorts(ports) {
|
|
435
|
+
const results = /* @__PURE__ */ new Map();
|
|
436
|
+
await Promise.all(
|
|
437
|
+
ports.map(async (port) => {
|
|
438
|
+
results.set(port, await isPortAvailable(port));
|
|
439
|
+
})
|
|
440
|
+
);
|
|
441
|
+
return results;
|
|
442
|
+
}
|
|
443
|
+
async function findNextAvailablePort(startPort, maxPort = 65535) {
|
|
444
|
+
for (let port = startPort; port <= maxPort; port++) {
|
|
445
|
+
if (await isPortAvailable(port)) {
|
|
446
|
+
return port;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
async function findAvailablePortInRange(start, end) {
|
|
452
|
+
for (let port = start; port <= end; port++) {
|
|
453
|
+
if (await isPortAvailable(port)) {
|
|
454
|
+
return port;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return void 0;
|
|
458
|
+
}
|
|
459
|
+
async function resolvePort(options = {}) {
|
|
460
|
+
const {
|
|
461
|
+
preferred = 3e3,
|
|
462
|
+
strategy = "auto",
|
|
463
|
+
range
|
|
464
|
+
} = options;
|
|
465
|
+
switch (strategy) {
|
|
466
|
+
case "fixed":
|
|
467
|
+
if (await isPortAvailable(preferred)) {
|
|
468
|
+
return preferred;
|
|
469
|
+
}
|
|
470
|
+
throw new Error(`Port ${preferred} is already in use`);
|
|
471
|
+
case "range":
|
|
472
|
+
if (range) {
|
|
473
|
+
const [start, end = start + 100] = range;
|
|
474
|
+
const port2 = await findAvailablePortInRange(start, end);
|
|
475
|
+
if (port2 !== void 0) {
|
|
476
|
+
return port2;
|
|
477
|
+
}
|
|
478
|
+
throw new Error(`No available port in range ${start}-${end}`);
|
|
479
|
+
}
|
|
480
|
+
// Fall through to auto if no range specified
|
|
481
|
+
case "auto":
|
|
482
|
+
default:
|
|
483
|
+
const port = await findNextAvailablePort(preferred);
|
|
484
|
+
if (port !== void 0) {
|
|
485
|
+
return port;
|
|
486
|
+
}
|
|
487
|
+
throw new Error(`No available port found starting from ${preferred}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// src/core/config.ts
|
|
492
|
+
var DEFAULT_APP_CONFIG = {
|
|
493
|
+
port: 3e3,
|
|
494
|
+
node: "20-alpine",
|
|
495
|
+
workdir: "/app",
|
|
496
|
+
command: "npm start",
|
|
497
|
+
buildCommand: "npm run build",
|
|
498
|
+
env: [],
|
|
499
|
+
volumes: [],
|
|
500
|
+
multiStage: true
|
|
501
|
+
};
|
|
502
|
+
function normalizeAppConfig(config) {
|
|
503
|
+
return {
|
|
504
|
+
port: config.port === "auto" ? 3e3 : config.port,
|
|
505
|
+
node: config.node ?? DEFAULT_APP_CONFIG.node,
|
|
506
|
+
workdir: config.workdir ?? DEFAULT_APP_CONFIG.workdir,
|
|
507
|
+
command: config.command ?? DEFAULT_APP_CONFIG.command,
|
|
508
|
+
buildCommand: config.buildCommand ?? DEFAULT_APP_CONFIG.buildCommand,
|
|
509
|
+
env: config.env ?? DEFAULT_APP_CONFIG.env,
|
|
510
|
+
volumes: config.volumes ?? DEFAULT_APP_CONFIG.volumes,
|
|
511
|
+
multiStage: config.multiStage ?? DEFAULT_APP_CONFIG.multiStage,
|
|
512
|
+
healthcheck: config.healthcheck
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function normalizeOutputConfig(config) {
|
|
516
|
+
return {
|
|
517
|
+
dir: config?.dir ?? "./",
|
|
518
|
+
files: config?.files ?? ["dockerfile", "compose", "dockerignore", "env"],
|
|
519
|
+
overwrite: config?.overwrite ?? false
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function normalizeConfig(config) {
|
|
523
|
+
return {
|
|
524
|
+
name: config.name,
|
|
525
|
+
preset: config.preset,
|
|
526
|
+
app: config.app,
|
|
527
|
+
services: config.services ?? {},
|
|
528
|
+
output: normalizeOutputConfig(config.output),
|
|
529
|
+
plugins: config.plugins ?? []
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/core/resolver.ts
|
|
534
|
+
function mergePreset(config, presetName) {
|
|
535
|
+
const preset = getPreset(presetName);
|
|
536
|
+
if (!preset) {
|
|
537
|
+
throw new Error(`Preset '${presetName}' not found`);
|
|
538
|
+
}
|
|
539
|
+
const merged = deepMerge(
|
|
540
|
+
{
|
|
541
|
+
name: config.name,
|
|
542
|
+
app: preset.app ?? {},
|
|
543
|
+
services: preset.services ?? {},
|
|
544
|
+
output: preset.output ?? {}
|
|
545
|
+
},
|
|
546
|
+
config
|
|
547
|
+
);
|
|
548
|
+
return merged;
|
|
549
|
+
}
|
|
550
|
+
async function resolveServiceConfig(name, config, projectName) {
|
|
551
|
+
if ("image" in config && config.image) {
|
|
552
|
+
return resolveCustomService(name, config);
|
|
553
|
+
}
|
|
554
|
+
const definition = getService(name);
|
|
555
|
+
if (!definition) {
|
|
556
|
+
throw new Error(`Unknown service: ${name}`);
|
|
557
|
+
}
|
|
558
|
+
const defaultPort = definition.defaultPort;
|
|
559
|
+
let port = defaultPort;
|
|
560
|
+
let hostPort;
|
|
561
|
+
if (config.port === "auto") {
|
|
562
|
+
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
563
|
+
hostPort = port;
|
|
564
|
+
} else if (typeof config.port === "number") {
|
|
565
|
+
port = config.port;
|
|
566
|
+
hostPort = config.port;
|
|
567
|
+
} else if (config.port && typeof config.port === "object") {
|
|
568
|
+
const { start, end } = config.port;
|
|
569
|
+
const startPort = start ?? defaultPort;
|
|
570
|
+
const endPort = end ?? startPort + 100;
|
|
571
|
+
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
572
|
+
hostPort = port;
|
|
573
|
+
}
|
|
574
|
+
const version = config.version ?? definition.defaultVersion;
|
|
575
|
+
const image = typeof definition.image === "function" ? definition.image(version) : definition.image;
|
|
576
|
+
const env = { ...definition.env, ...config.env };
|
|
577
|
+
if (name === "postgres") {
|
|
578
|
+
const pgConfig = config;
|
|
579
|
+
if (pgConfig.database) env.POSTGRES_DB = pgConfig.database;
|
|
580
|
+
if (pgConfig.user) env.POSTGRES_USER = pgConfig.user;
|
|
581
|
+
if (pgConfig.password) env.POSTGRES_PASSWORD = pgConfig.password;
|
|
582
|
+
if (!env.POSTGRES_DB) env.POSTGRES_DB = projectName;
|
|
583
|
+
} else if (name === "mysql") {
|
|
584
|
+
const mysqlConfig = config;
|
|
585
|
+
if (mysqlConfig.database) env.MYSQL_DATABASE = mysqlConfig.database;
|
|
586
|
+
if (mysqlConfig.user) env.MYSQL_USER = mysqlConfig.user;
|
|
587
|
+
if (mysqlConfig.password) env.MYSQL_PASSWORD = mysqlConfig.password;
|
|
588
|
+
if (mysqlConfig.rootPassword) env.MYSQL_ROOT_PASSWORD = mysqlConfig.rootPassword;
|
|
589
|
+
if (!env.MYSQL_DATABASE) env.MYSQL_DATABASE = projectName;
|
|
590
|
+
} else if (name === "mongo") {
|
|
591
|
+
const mongoConfig = config;
|
|
592
|
+
if (mongoConfig.database) env.MONGO_INITDB_DATABASE = mongoConfig.database;
|
|
593
|
+
if (mongoConfig.user) env.MONGO_INITDB_ROOT_USERNAME = mongoConfig.user;
|
|
594
|
+
if (mongoConfig.password) env.MONGO_INITDB_ROOT_PASSWORD = mongoConfig.password;
|
|
595
|
+
if (!env.MONGO_INITDB_DATABASE) env.MONGO_INITDB_DATABASE = projectName;
|
|
596
|
+
} else if (name === "redis") {
|
|
597
|
+
const redisConfig = config;
|
|
598
|
+
if (redisConfig.password) env.REDIS_PASSWORD = redisConfig.password;
|
|
599
|
+
}
|
|
600
|
+
const volumes = [...definition.volumes ?? [], ...config.volumes ?? []];
|
|
601
|
+
const resolvedConfig = {
|
|
602
|
+
name,
|
|
603
|
+
image,
|
|
604
|
+
port,
|
|
605
|
+
hostPort,
|
|
606
|
+
env,
|
|
607
|
+
volumes,
|
|
608
|
+
command: config.command,
|
|
609
|
+
healthcheck: definition.healthcheck,
|
|
610
|
+
ports: definition.ports
|
|
611
|
+
};
|
|
612
|
+
if (definition.connectionString) {
|
|
613
|
+
resolvedConfig.connectionString = definition.connectionString(resolvedConfig);
|
|
614
|
+
}
|
|
615
|
+
return resolvedConfig;
|
|
616
|
+
}
|
|
617
|
+
async function resolveCustomService(name, config) {
|
|
618
|
+
const imageMatch = config.image.match(/:(\d+)$/);
|
|
619
|
+
const defaultPort = imageMatch && imageMatch[1] ? parseInt(imageMatch[1], 10) : 80;
|
|
620
|
+
let port = defaultPort;
|
|
621
|
+
let hostPort;
|
|
622
|
+
if (config.port === "auto") {
|
|
623
|
+
port = await resolvePort({ preferred: defaultPort, strategy: "auto" });
|
|
624
|
+
hostPort = port;
|
|
625
|
+
} else if (typeof config.port === "number") {
|
|
626
|
+
port = config.port;
|
|
627
|
+
hostPort = config.port;
|
|
628
|
+
} else if (config.port && typeof config.port === "object") {
|
|
629
|
+
const { start, end } = config.port;
|
|
630
|
+
const startPort = start ?? defaultPort;
|
|
631
|
+
const endPort = end ?? startPort + 100;
|
|
632
|
+
port = await resolvePort({ preferred: startPort, strategy: "range", range: [startPort, endPort] });
|
|
633
|
+
hostPort = port;
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
name,
|
|
637
|
+
image: config.image,
|
|
638
|
+
port,
|
|
639
|
+
hostPort,
|
|
640
|
+
env: config.env ?? {},
|
|
641
|
+
volumes: config.volumes ?? [],
|
|
642
|
+
command: config.command,
|
|
643
|
+
healthcheck: config.command,
|
|
644
|
+
ports: []
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
async function resolveConfig(config) {
|
|
648
|
+
let finalConfig = config;
|
|
649
|
+
if (config.preset) {
|
|
650
|
+
finalConfig = mergePreset(config, config.preset);
|
|
651
|
+
}
|
|
652
|
+
finalConfig = normalizeConfig(finalConfig);
|
|
653
|
+
const appPort = finalConfig.app.port === "auto" ? await resolvePort({ preferred: 3e3, strategy: "auto" }) : finalConfig.app.port;
|
|
654
|
+
const app = normalizeAppConfig({ ...finalConfig.app, port: appPort });
|
|
655
|
+
const services2 = {};
|
|
656
|
+
if (finalConfig.services) {
|
|
657
|
+
for (const [name, serviceConfig] of Object.entries(finalConfig.services)) {
|
|
658
|
+
if (serviceConfig) {
|
|
659
|
+
services2[name] = await resolveServiceConfig(
|
|
660
|
+
name,
|
|
661
|
+
serviceConfig,
|
|
662
|
+
finalConfig.name
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return {
|
|
668
|
+
name: finalConfig.name,
|
|
669
|
+
app,
|
|
670
|
+
services: services2,
|
|
671
|
+
output: normalizeOutputConfig(finalConfig.output)
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// src/utils/validation.ts
|
|
676
|
+
var MIN_PORT = 1;
|
|
677
|
+
var MAX_PORT = 65535;
|
|
678
|
+
function isPort(value) {
|
|
679
|
+
return typeof value === "number" && Number.isInteger(value) && value >= MIN_PORT && value <= MAX_PORT;
|
|
680
|
+
}
|
|
681
|
+
function isEnvVarName(value) {
|
|
682
|
+
if (typeof value !== "string") return false;
|
|
683
|
+
return /^[A-Z_][A-Z0-9_]*$/.test(value);
|
|
684
|
+
}
|
|
685
|
+
function isVersion(value) {
|
|
686
|
+
if (typeof value !== "string") return false;
|
|
687
|
+
if (value.length === 0) return false;
|
|
688
|
+
return /^[a-zA-Z0-9.\-]+$/.test(value);
|
|
689
|
+
}
|
|
690
|
+
function isProjectName(value) {
|
|
691
|
+
if (typeof value !== "string") return false;
|
|
692
|
+
if (value.length === 0 || value.length > 100) return false;
|
|
693
|
+
return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(value);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// src/core/validator.ts
|
|
697
|
+
function validateProjectName(name) {
|
|
698
|
+
const errors = [];
|
|
699
|
+
if (!isProjectName(name)) {
|
|
700
|
+
errors.push({
|
|
701
|
+
code: "INVALID_NAME",
|
|
702
|
+
message: `Invalid project name '${name}'. Must start with a letter and contain only letters, numbers, hyphens, and underscores.`,
|
|
703
|
+
path: "name",
|
|
704
|
+
value: name
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
return errors;
|
|
708
|
+
}
|
|
709
|
+
function validateAppConfig(config) {
|
|
710
|
+
const errors = [];
|
|
711
|
+
if (config.port !== "auto" && !isPort(config.port)) {
|
|
712
|
+
errors.push({
|
|
713
|
+
code: "INVALID_PORT",
|
|
714
|
+
message: `Invalid app port '${String(config.port)}'. Must be a number between 1-65535 or 'auto'.`,
|
|
715
|
+
path: "app.port",
|
|
716
|
+
value: config.port
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
if (config.node !== void 0 && !isVersion(config.node)) {
|
|
720
|
+
errors.push({
|
|
721
|
+
code: "INVALID_VERSION",
|
|
722
|
+
message: `Invalid Node.js version '${config.node}'.`,
|
|
723
|
+
path: "app.node",
|
|
724
|
+
value: config.node
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
if (config.env) {
|
|
728
|
+
for (const envVar of config.env) {
|
|
729
|
+
if (!isEnvVarName(envVar)) {
|
|
730
|
+
errors.push({
|
|
731
|
+
code: "INVALID_ENV_VAR",
|
|
732
|
+
message: `Invalid environment variable name '${envVar}'. Must be uppercase letters, numbers, and underscores.`,
|
|
733
|
+
path: "app.env",
|
|
734
|
+
value: envVar
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
return errors;
|
|
740
|
+
}
|
|
741
|
+
function validateServiceConfig(name, config) {
|
|
742
|
+
const errors = [];
|
|
743
|
+
if (config.version !== void 0 && !isVersion(config.version)) {
|
|
744
|
+
errors.push({
|
|
745
|
+
code: "INVALID_VERSION",
|
|
746
|
+
message: `Invalid version '${config.version}' for service '${name}'.`,
|
|
747
|
+
path: `services.${name}.version`,
|
|
748
|
+
value: config.version
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
if (config.port !== void 0 && config.port !== "auto" && typeof config.port !== "number" && typeof config.port !== "object") {
|
|
752
|
+
errors.push({
|
|
753
|
+
code: "INVALID_PORT",
|
|
754
|
+
message: `Invalid port configuration for service '${name}'. Must be a number, 'auto', or a range object.`,
|
|
755
|
+
path: `services.${name}.port`,
|
|
756
|
+
value: config.port
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
return errors;
|
|
760
|
+
}
|
|
761
|
+
function validateOutputConfig(config) {
|
|
762
|
+
const errors = [];
|
|
763
|
+
if (config?.files) {
|
|
764
|
+
const validFiles = ["dockerfile", "compose", "dockerignore", "env", "scripts", "makefile"];
|
|
765
|
+
for (const file of config.files) {
|
|
766
|
+
if (!validFiles.includes(file)) {
|
|
767
|
+
errors.push({
|
|
768
|
+
code: "INVALID_OUTPUT_FILE",
|
|
769
|
+
message: `Invalid output file '${file}'. Must be one of: ${validFiles.join(", ")}.`,
|
|
770
|
+
path: "output.files",
|
|
771
|
+
value: file
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return errors;
|
|
777
|
+
}
|
|
778
|
+
function validateConfig(config) {
|
|
779
|
+
const errors = [];
|
|
780
|
+
errors.push(...validateProjectName(config.name));
|
|
781
|
+
if (config.app) {
|
|
782
|
+
errors.push(...validateAppConfig(config.app));
|
|
783
|
+
}
|
|
784
|
+
if (config.services) {
|
|
785
|
+
for (const [name, serviceConfig] of Object.entries(config.services)) {
|
|
786
|
+
if (serviceConfig) {
|
|
787
|
+
errors.push(...validateServiceConfig(name, serviceConfig));
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if (config.output) {
|
|
792
|
+
errors.push(...validateOutputConfig(config.output));
|
|
793
|
+
}
|
|
794
|
+
return {
|
|
795
|
+
valid: errors.length === 0,
|
|
796
|
+
errors
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
function validateResolvedConfig(config) {
|
|
800
|
+
const errors = [];
|
|
801
|
+
const usedPorts = /* @__PURE__ */ new Set();
|
|
802
|
+
usedPorts.add(config.app.port);
|
|
803
|
+
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
804
|
+
if (usedPorts.has(serviceConfig.port)) {
|
|
805
|
+
errors.push({
|
|
806
|
+
code: "PORT_CONFLICT",
|
|
807
|
+
message: `Port ${serviceConfig.port} is already in use by ${serviceName}.`,
|
|
808
|
+
path: `services.${serviceName}.port`,
|
|
809
|
+
value: serviceConfig.port
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
usedPorts.add(serviceConfig.port);
|
|
813
|
+
}
|
|
814
|
+
return {
|
|
815
|
+
valid: errors.length === 0,
|
|
816
|
+
errors
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// src/generators/dockerfile.ts
|
|
821
|
+
function generateDockerfile(config, _context) {
|
|
822
|
+
const { app, name } = config;
|
|
823
|
+
const { node, workdir, command, buildCommand, multiStage, healthcheck } = app;
|
|
824
|
+
const port = app.port;
|
|
825
|
+
let dockerfile = "";
|
|
826
|
+
if (multiStage) {
|
|
827
|
+
dockerfile += `# Builder stage
|
|
828
|
+
`;
|
|
829
|
+
dockerfile += `FROM node:${node} AS builder
|
|
830
|
+
|
|
831
|
+
`;
|
|
832
|
+
dockerfile += `WORKDIR ${workdir}
|
|
833
|
+
|
|
834
|
+
`;
|
|
835
|
+
dockerfile += `# Copy package files
|
|
836
|
+
`;
|
|
837
|
+
dockerfile += `COPY package*.json ./
|
|
838
|
+
|
|
839
|
+
`;
|
|
840
|
+
dockerfile += `# Install dependencies
|
|
841
|
+
`;
|
|
842
|
+
dockerfile += `RUN npm ci && npm cache clean --force
|
|
843
|
+
|
|
844
|
+
`;
|
|
845
|
+
dockerfile += `# Copy source files
|
|
846
|
+
`;
|
|
847
|
+
dockerfile += `COPY . .
|
|
848
|
+
|
|
849
|
+
`;
|
|
850
|
+
dockerfile += `# Build application
|
|
851
|
+
`;
|
|
852
|
+
dockerfile += `RUN npm run build 2>/dev/null || true
|
|
853
|
+
|
|
854
|
+
`;
|
|
855
|
+
dockerfile += `# Production stage
|
|
856
|
+
`;
|
|
857
|
+
dockerfile += `FROM node:${node}
|
|
858
|
+
|
|
859
|
+
`;
|
|
860
|
+
dockerfile += `WORKDIR ${workdir}
|
|
861
|
+
|
|
862
|
+
`;
|
|
863
|
+
dockerfile += `ENV NODE_ENV=production
|
|
864
|
+
|
|
865
|
+
`;
|
|
866
|
+
dockerfile += `# Copy from builder
|
|
867
|
+
`;
|
|
868
|
+
dockerfile += `COPY --from=builder ${workdir}/node_modules ./node_modules
|
|
869
|
+
`;
|
|
870
|
+
dockerfile += `COPY --from=builder ${workdir}/dist ./dist
|
|
871
|
+
`;
|
|
872
|
+
dockerfile += `COPY --from=builder ${workdir}/package*.json ./
|
|
873
|
+
|
|
874
|
+
`;
|
|
875
|
+
} else {
|
|
876
|
+
dockerfile += `FROM node:${node}
|
|
877
|
+
|
|
878
|
+
`;
|
|
879
|
+
dockerfile += `WORKDIR ${workdir}
|
|
880
|
+
|
|
881
|
+
`;
|
|
882
|
+
dockerfile += `ENV NODE_ENV=production
|
|
883
|
+
|
|
884
|
+
`;
|
|
885
|
+
dockerfile += `# Copy package files
|
|
886
|
+
`;
|
|
887
|
+
dockerfile += `COPY package*.json ./
|
|
888
|
+
|
|
889
|
+
`;
|
|
890
|
+
dockerfile += `# Install dependencies
|
|
891
|
+
`;
|
|
892
|
+
dockerfile += `RUN npm ci --only=production && npm cache clean --force
|
|
893
|
+
|
|
894
|
+
`;
|
|
895
|
+
dockerfile += `# Copy source files
|
|
896
|
+
`;
|
|
897
|
+
dockerfile += `COPY . .
|
|
898
|
+
|
|
899
|
+
`;
|
|
900
|
+
}
|
|
901
|
+
dockerfile += `EXPOSE ${port}
|
|
902
|
+
|
|
903
|
+
`;
|
|
904
|
+
if (healthcheck) {
|
|
905
|
+
dockerfile += `HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \\
|
|
906
|
+
CMD node -e "require('http').get('http://localhost:${port}${healthcheck}', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})" || exit 1
|
|
907
|
+
|
|
908
|
+
`;
|
|
909
|
+
}
|
|
910
|
+
dockerfile += `CMD ["npm", "start"]
|
|
911
|
+
`;
|
|
912
|
+
return dockerfile;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// src/utils/yaml.ts
|
|
916
|
+
function toYaml(obj, indent = 0) {
|
|
917
|
+
const spaces = " ".repeat(indent);
|
|
918
|
+
if (obj === null) {
|
|
919
|
+
return "null";
|
|
920
|
+
}
|
|
921
|
+
if (typeof obj === "string") {
|
|
922
|
+
return quoteStringIfNeeded(obj);
|
|
923
|
+
}
|
|
924
|
+
if (typeof obj === "number" || typeof obj === "boolean") {
|
|
925
|
+
return String(obj);
|
|
926
|
+
}
|
|
927
|
+
if (Array.isArray(obj)) {
|
|
928
|
+
return arrayToYaml(obj, indent);
|
|
929
|
+
}
|
|
930
|
+
if (typeof obj === "object") {
|
|
931
|
+
return objectToYaml(obj, indent);
|
|
932
|
+
}
|
|
933
|
+
return String(obj);
|
|
934
|
+
}
|
|
935
|
+
function arrayToYaml(arr, indent) {
|
|
936
|
+
const spaces = " ".repeat(indent);
|
|
937
|
+
const innerSpaces = " ".repeat(indent + 2);
|
|
938
|
+
if (arr.length === 0) {
|
|
939
|
+
return "[]";
|
|
940
|
+
}
|
|
941
|
+
if (arr.every(
|
|
942
|
+
(item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null
|
|
943
|
+
)) {
|
|
944
|
+
const items = arr.map((item) => toYaml(item, 0)).join(", ");
|
|
945
|
+
return `[${items}]`;
|
|
946
|
+
}
|
|
947
|
+
const lines = [];
|
|
948
|
+
for (const item of arr) {
|
|
949
|
+
const value = toYaml(item, indent + 2);
|
|
950
|
+
if (value.includes("\n")) {
|
|
951
|
+
lines.push(`${spaces}- ${value.slice(indent + 2)}`);
|
|
952
|
+
} else {
|
|
953
|
+
lines.push(`${spaces}- ${value}`);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return lines.join("\n");
|
|
957
|
+
}
|
|
958
|
+
function objectToYaml(obj, indent) {
|
|
959
|
+
const keys = Object.keys(obj);
|
|
960
|
+
if (keys.length === 0) {
|
|
961
|
+
return "{}";
|
|
962
|
+
}
|
|
963
|
+
const spaces = " ".repeat(indent);
|
|
964
|
+
const lines = [];
|
|
965
|
+
for (const key of keys) {
|
|
966
|
+
const value = obj[key];
|
|
967
|
+
const valueStr = toYaml(value, indent + 2);
|
|
968
|
+
if (valueStr.includes("\n")) {
|
|
969
|
+
lines.push(`${spaces}${key}:`);
|
|
970
|
+
lines.push(valueStr);
|
|
971
|
+
} else {
|
|
972
|
+
lines.push(`${spaces}${key}: ${valueStr}`);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
return lines.join("\n");
|
|
976
|
+
}
|
|
977
|
+
function quoteStringIfNeeded(str) {
|
|
978
|
+
if (str === "" || str.startsWith('"') || str.startsWith("'")) {
|
|
979
|
+
return `"${escapeString(str)}"`;
|
|
980
|
+
}
|
|
981
|
+
if (/^[\-\?:\[\]{},&*#|!<>@%'"`]/.test(str)) {
|
|
982
|
+
return `"${escapeString(str)}"`;
|
|
983
|
+
}
|
|
984
|
+
if (/[:\s#]/.test(str)) {
|
|
985
|
+
return `"${escapeString(str)}"`;
|
|
986
|
+
}
|
|
987
|
+
if (/^(true|false|null|NaN|Infinity|\-?\d+(\.\d+)?)$/.test(str)) {
|
|
988
|
+
return `"${escapeString(str)}"`;
|
|
989
|
+
}
|
|
990
|
+
return escapeString(str);
|
|
991
|
+
}
|
|
992
|
+
function escapeString(str) {
|
|
993
|
+
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// src/generators/compose.ts
|
|
997
|
+
function generateCompose(config, context) {
|
|
998
|
+
const { name, app, services: services2 } = config;
|
|
999
|
+
const compose = {
|
|
1000
|
+
version: "3.9",
|
|
1001
|
+
services: {
|
|
1002
|
+
app: {
|
|
1003
|
+
build: ".",
|
|
1004
|
+
ports: [`${app.port}:${app.port}`],
|
|
1005
|
+
environment: generateAppEnvironment(config, context),
|
|
1006
|
+
depends_on: Object.keys(services2),
|
|
1007
|
+
volumes: app.volumes.length > 0 ? app.volumes : void 0
|
|
1008
|
+
}
|
|
1009
|
+
},
|
|
1010
|
+
volumes: generateVolumeDefinitions(services2)
|
|
1011
|
+
};
|
|
1012
|
+
const servicesRecord = compose.services;
|
|
1013
|
+
if (app.healthcheck && servicesRecord.app) {
|
|
1014
|
+
servicesRecord.app.healthcheck = {
|
|
1015
|
+
test: [`CMD`, `curl`, `-f`, `http://localhost:${app.port}${app.healthcheck}`],
|
|
1016
|
+
interval: "30s",
|
|
1017
|
+
timeout: "3s",
|
|
1018
|
+
retries: 3
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
for (const [serviceName, serviceConfig] of Object.entries(services2)) {
|
|
1022
|
+
servicesRecord[serviceName] = generateServiceConfig(serviceName, serviceConfig);
|
|
1023
|
+
}
|
|
1024
|
+
return toYaml(compose);
|
|
1025
|
+
}
|
|
1026
|
+
function generateAppEnvironment(config, context) {
|
|
1027
|
+
const env = ["NODE_ENV=production", `PORT=${config.app.port}`];
|
|
1028
|
+
for (const [serviceName, serviceConfig] of Object.entries(config.services)) {
|
|
1029
|
+
if (serviceConfig.connectionString) {
|
|
1030
|
+
const key = `${serviceName.toUpperCase()}_URL`;
|
|
1031
|
+
env.push(`${key}=${serviceConfig.connectionString}`);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (config.services.postgres) {
|
|
1035
|
+
const pg = config.services.postgres;
|
|
1036
|
+
env.push(`POSTGRES_HOST=${pg.name}`);
|
|
1037
|
+
env.push(`POSTGRES_PORT=${pg.port}`);
|
|
1038
|
+
env.push(`POSTGRES_USER=${pg.env.POSTGRES_USER || "postgres"}`);
|
|
1039
|
+
env.push(`POSTGRES_PASSWORD=${pg.env.POSTGRES_PASSWORD || "postgres"}`);
|
|
1040
|
+
env.push(`POSTGRES_DB=${pg.env.POSTGRES_DB || config.name}`);
|
|
1041
|
+
}
|
|
1042
|
+
if (config.services.mysql) {
|
|
1043
|
+
const mysql = config.services.mysql;
|
|
1044
|
+
env.push(`MYSQL_HOST=${mysql.name}`);
|
|
1045
|
+
env.push(`MYSQL_PORT=${mysql.port}`);
|
|
1046
|
+
env.push(`MYSQL_USER=${mysql.env.MYSQL_USER || "root"}`);
|
|
1047
|
+
env.push(`MYSQL_PASSWORD=${mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"}`);
|
|
1048
|
+
env.push(`MYSQL_DATABASE=${mysql.env.MYSQL_DATABASE || config.name}`);
|
|
1049
|
+
}
|
|
1050
|
+
if (config.services.mongo) {
|
|
1051
|
+
const mongo = config.services.mongo;
|
|
1052
|
+
env.push(`MONGO_HOST=${mongo.name}`);
|
|
1053
|
+
env.push(`MONGO_PORT=${mongo.port}`);
|
|
1054
|
+
env.push(`MONGO_USER=${mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"}`);
|
|
1055
|
+
env.push(`MONGO_PASSWORD=${mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"}`);
|
|
1056
|
+
env.push(`MONGO_DATABASE=${mongo.env.MONGO_INITDB_DATABASE || config.name}`);
|
|
1057
|
+
}
|
|
1058
|
+
if (config.services.redis) {
|
|
1059
|
+
const redis = config.services.redis;
|
|
1060
|
+
env.push(`REDIS_HOST=${redis.name}`);
|
|
1061
|
+
env.push(`REDIS_PORT=${redis.port}`);
|
|
1062
|
+
}
|
|
1063
|
+
return env;
|
|
1064
|
+
}
|
|
1065
|
+
function generateServiceConfig(name, config) {
|
|
1066
|
+
const service = {
|
|
1067
|
+
image: config.image,
|
|
1068
|
+
ports: [],
|
|
1069
|
+
environment: config.env
|
|
1070
|
+
};
|
|
1071
|
+
const portMappings = [];
|
|
1072
|
+
portMappings.push(`${config.hostPort ?? config.port}:${config.port}`);
|
|
1073
|
+
if (config.ports) {
|
|
1074
|
+
for (const extraPort of config.ports) {
|
|
1075
|
+
portMappings.push(`${extraPort}:${extraPort}`);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
service.ports = portMappings;
|
|
1079
|
+
if (config.volumes.length > 0) {
|
|
1080
|
+
service.volumes = config.volumes;
|
|
1081
|
+
}
|
|
1082
|
+
if (config.command) {
|
|
1083
|
+
service.command = config.command;
|
|
1084
|
+
}
|
|
1085
|
+
if (config.healthcheck) {
|
|
1086
|
+
service.healthcheck = {
|
|
1087
|
+
test: ["CMD-SHELL", config.healthcheck],
|
|
1088
|
+
interval: "10s",
|
|
1089
|
+
timeout: "5s",
|
|
1090
|
+
retries: 5
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
return service;
|
|
1094
|
+
}
|
|
1095
|
+
function generateVolumeDefinitions(services2) {
|
|
1096
|
+
const volumes = {};
|
|
1097
|
+
for (const serviceConfig of Object.values(services2)) {
|
|
1098
|
+
for (const volume of serviceConfig.volumes) {
|
|
1099
|
+
if (volume.includes(":")) {
|
|
1100
|
+
const [name] = volume.split(":");
|
|
1101
|
+
if (name) {
|
|
1102
|
+
volumes[name] = null;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
return volumes;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// src/generators/dockerignore.ts
|
|
1111
|
+
function generateDockerignore(_config) {
|
|
1112
|
+
return `# Dependencies
|
|
1113
|
+
node_modules/
|
|
1114
|
+
npm-debug.log*
|
|
1115
|
+
yarn-debug.log*
|
|
1116
|
+
yarn-error.log*
|
|
1117
|
+
pnpm-debug.log*
|
|
1118
|
+
|
|
1119
|
+
# Environment
|
|
1120
|
+
.env
|
|
1121
|
+
.env.local
|
|
1122
|
+
.env.*.local
|
|
1123
|
+
|
|
1124
|
+
# Git
|
|
1125
|
+
.git/
|
|
1126
|
+
.gitignore
|
|
1127
|
+
.gitattributes
|
|
1128
|
+
|
|
1129
|
+
# Documentation
|
|
1130
|
+
README.md
|
|
1131
|
+
CHANGELOG.md
|
|
1132
|
+
LICENSE
|
|
1133
|
+
SPECIFICATION.md
|
|
1134
|
+
IMPLEMENTATION.md
|
|
1135
|
+
TASKS.md
|
|
1136
|
+
docs/
|
|
1137
|
+
|
|
1138
|
+
# IDE
|
|
1139
|
+
.vscode/
|
|
1140
|
+
.idea/
|
|
1141
|
+
*.swp
|
|
1142
|
+
*.swo
|
|
1143
|
+
*~
|
|
1144
|
+
.DS_Store
|
|
1145
|
+
|
|
1146
|
+
# Testing
|
|
1147
|
+
coverage/
|
|
1148
|
+
.nyc_output/
|
|
1149
|
+
*.test.ts
|
|
1150
|
+
*.spec.ts
|
|
1151
|
+
tests/
|
|
1152
|
+
vitest.config.ts
|
|
1153
|
+
|
|
1154
|
+
# Build
|
|
1155
|
+
dist/
|
|
1156
|
+
build/
|
|
1157
|
+
*.tsbuildinfo
|
|
1158
|
+
|
|
1159
|
+
# Temporary
|
|
1160
|
+
*.log
|
|
1161
|
+
tmp/
|
|
1162
|
+
temp/
|
|
1163
|
+
|
|
1164
|
+
# CI/CD
|
|
1165
|
+
.github/
|
|
1166
|
+
.gitlab-ci.yml
|
|
1167
|
+
.travis.yml
|
|
1168
|
+
|
|
1169
|
+
# Docker
|
|
1170
|
+
Dockerfile
|
|
1171
|
+
docker-compose.yml
|
|
1172
|
+
.dockerignore
|
|
1173
|
+
|
|
1174
|
+
# Package manager lockfiles (optional - uncomment if you want to exclude)
|
|
1175
|
+
# package-lock.json
|
|
1176
|
+
# yarn.lock
|
|
1177
|
+
# pnpm-lock.yaml
|
|
1178
|
+
`;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
// src/generators/env.ts
|
|
1182
|
+
function generateEnvExample(config) {
|
|
1183
|
+
let content = `# Application Configuration
|
|
1184
|
+
NODE_ENV=production
|
|
1185
|
+
PORT=${config.app.port}
|
|
1186
|
+
`;
|
|
1187
|
+
if (config.app.env.length > 0) {
|
|
1188
|
+
content += `
|
|
1189
|
+
# Required Environment Variables
|
|
1190
|
+
`;
|
|
1191
|
+
for (const envVar of config.app.env) {
|
|
1192
|
+
content += `${envVar}=
|
|
1193
|
+
`;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
if (config.services.postgres) {
|
|
1197
|
+
const pg = config.services.postgres;
|
|
1198
|
+
content += `
|
|
1199
|
+
# PostgreSQL
|
|
1200
|
+
`;
|
|
1201
|
+
content += `DATABASE_URL=${pg.connectionString || ""}
|
|
1202
|
+
`;
|
|
1203
|
+
content += `POSTGRES_HOST=${pg.name}
|
|
1204
|
+
`;
|
|
1205
|
+
content += `POSTGRES_PORT=${pg.port}
|
|
1206
|
+
`;
|
|
1207
|
+
content += `POSTGRES_USER=${pg.env.POSTGRES_USER || "postgres"}
|
|
1208
|
+
`;
|
|
1209
|
+
content += `POSTGRES_PASSWORD=${pg.env.POSTGRES_PASSWORD || "postgres"}
|
|
1210
|
+
`;
|
|
1211
|
+
content += `POSTGRES_DB=${pg.env.POSTGRES_DB || config.name}
|
|
1212
|
+
`;
|
|
1213
|
+
}
|
|
1214
|
+
if (config.services.mysql) {
|
|
1215
|
+
const mysql = config.services.mysql;
|
|
1216
|
+
content += `
|
|
1217
|
+
# MySQL
|
|
1218
|
+
`;
|
|
1219
|
+
content += `MYSQL_URL=${mysql.connectionString || ""}
|
|
1220
|
+
`;
|
|
1221
|
+
content += `MYSQL_HOST=${mysql.name}
|
|
1222
|
+
`;
|
|
1223
|
+
content += `MYSQL_PORT=${mysql.port}
|
|
1224
|
+
`;
|
|
1225
|
+
content += `MYSQL_USER=${mysql.env.MYSQL_USER || "root"}
|
|
1226
|
+
`;
|
|
1227
|
+
content += `MYSQL_PASSWORD=${mysql.env.MYSQL_PASSWORD || mysql.env.MYSQL_ROOT_PASSWORD || "root"}
|
|
1228
|
+
`;
|
|
1229
|
+
content += `MYSQL_DATABASE=${mysql.env.MYSQL_DATABASE || config.name}
|
|
1230
|
+
`;
|
|
1231
|
+
}
|
|
1232
|
+
if (config.services.mongo) {
|
|
1233
|
+
const mongo = config.services.mongo;
|
|
1234
|
+
content += `
|
|
1235
|
+
# MongoDB
|
|
1236
|
+
`;
|
|
1237
|
+
content += `MONGO_URL=${mongo.connectionString || ""}
|
|
1238
|
+
`;
|
|
1239
|
+
content += `MONGO_HOST=${mongo.name}
|
|
1240
|
+
`;
|
|
1241
|
+
content += `MONGO_PORT=${mongo.port}
|
|
1242
|
+
`;
|
|
1243
|
+
content += `MONGO_USER=${mongo.env.MONGO_INITDB_ROOT_USERNAME || "mongo"}
|
|
1244
|
+
`;
|
|
1245
|
+
content += `MONGO_PASSWORD=${mongo.env.MONGO_INITDB_ROOT_PASSWORD || "mongo"}
|
|
1246
|
+
`;
|
|
1247
|
+
content += `MONGO_DATABASE=${mongo.env.MONGO_INITDB_DATABASE || config.name}
|
|
1248
|
+
`;
|
|
1249
|
+
}
|
|
1250
|
+
if (config.services.redis) {
|
|
1251
|
+
const redis = config.services.redis;
|
|
1252
|
+
content += `
|
|
1253
|
+
# Redis
|
|
1254
|
+
`;
|
|
1255
|
+
content += `REDIS_URL=${redis.connectionString || ""}
|
|
1256
|
+
`;
|
|
1257
|
+
content += `REDIS_HOST=${redis.name}
|
|
1258
|
+
`;
|
|
1259
|
+
content += `REDIS_PORT=${redis.port}
|
|
1260
|
+
`;
|
|
1261
|
+
if (redis.env.REDIS_PASSWORD) {
|
|
1262
|
+
content += `REDIS_PASSWORD=${redis.env.REDIS_PASSWORD}
|
|
1263
|
+
`;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
return content;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
// src/generators/scripts.ts
|
|
1270
|
+
function generateBuildScript(_config) {
|
|
1271
|
+
return `#!/bin/bash
|
|
1272
|
+
# Docker build script
|
|
1273
|
+
|
|
1274
|
+
set -e
|
|
1275
|
+
|
|
1276
|
+
echo "Building Docker image..."
|
|
1277
|
+
docker-compose build
|
|
1278
|
+
|
|
1279
|
+
echo "Build complete!"
|
|
1280
|
+
`;
|
|
1281
|
+
}
|
|
1282
|
+
function generateUpScript(config) {
|
|
1283
|
+
const services2 = ["app", ...Object.keys(config.services)];
|
|
1284
|
+
return `#!/bin/bash
|
|
1285
|
+
# Docker up script
|
|
1286
|
+
|
|
1287
|
+
set -e
|
|
1288
|
+
|
|
1289
|
+
echo "Starting services..."
|
|
1290
|
+
docker-compose up -d ${services2.join(" ")}
|
|
1291
|
+
|
|
1292
|
+
echo "Services started!"
|
|
1293
|
+
echo ""
|
|
1294
|
+
echo "App running at http://localhost:${config.app.port}"
|
|
1295
|
+
`;
|
|
1296
|
+
}
|
|
1297
|
+
function generateDownScript(_config) {
|
|
1298
|
+
return `#!/bin/bash
|
|
1299
|
+
# Docker down script
|
|
1300
|
+
|
|
1301
|
+
set -e
|
|
1302
|
+
|
|
1303
|
+
echo "Stopping services..."
|
|
1304
|
+
docker-compose down
|
|
1305
|
+
|
|
1306
|
+
echo "Services stopped!"
|
|
1307
|
+
`;
|
|
1308
|
+
}
|
|
1309
|
+
function generateLogsScript(_config) {
|
|
1310
|
+
return `#!/bin/bash
|
|
1311
|
+
# Docker logs script
|
|
1312
|
+
|
|
1313
|
+
set -e
|
|
1314
|
+
|
|
1315
|
+
if [ -n "$1" ]; then
|
|
1316
|
+
docker-compose logs -f "$1"
|
|
1317
|
+
else
|
|
1318
|
+
docker-compose logs -f
|
|
1319
|
+
fi
|
|
1320
|
+
`;
|
|
1321
|
+
}
|
|
1322
|
+
function generateScripts(config) {
|
|
1323
|
+
return {
|
|
1324
|
+
"docker-build.sh": generateBuildScript(config),
|
|
1325
|
+
"docker-up.sh": generateUpScript(config),
|
|
1326
|
+
"docker-down.sh": generateDownScript(config),
|
|
1327
|
+
"docker-logs.sh": generateLogsScript(config)
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// src/generators/makefile.ts
|
|
1332
|
+
function generateMakefile(config) {
|
|
1333
|
+
const services2 = Object.keys(config.services);
|
|
1334
|
+
let content = `.PHONY: help build up down restart logs clean install shell
|
|
1335
|
+
|
|
1336
|
+
# Default target
|
|
1337
|
+
help:
|
|
1338
|
+
@echo "Available targets:"
|
|
1339
|
+
@echo " make build - Build Docker images"
|
|
1340
|
+
@echo " make up - Start all services"
|
|
1341
|
+
@echo " make down - Stop all services"
|
|
1342
|
+
@echo " make restart - Restart all services"
|
|
1343
|
+
@echo " make logs - Show logs from all services"
|
|
1344
|
+
@echo " make shell - Open shell in app container"
|
|
1345
|
+
@echo " make clean - Remove containers and volumes"
|
|
1346
|
+
@echo " make install - Install dependencies"
|
|
1347
|
+
|
|
1348
|
+
# Build Docker images
|
|
1349
|
+
build:
|
|
1350
|
+
docker-compose build
|
|
1351
|
+
|
|
1352
|
+
# Start all services
|
|
1353
|
+
up:
|
|
1354
|
+
docker-compose up -d
|
|
1355
|
+
@echo "App running at http://localhost:${config.app.port}"
|
|
1356
|
+
|
|
1357
|
+
# Stop all services
|
|
1358
|
+
down:
|
|
1359
|
+
docker-compose down
|
|
1360
|
+
|
|
1361
|
+
# Restart all services
|
|
1362
|
+
restart: down up
|
|
1363
|
+
|
|
1364
|
+
# Show logs from all services
|
|
1365
|
+
logs:
|
|
1366
|
+
docker-compose logs -f
|
|
1367
|
+
`;
|
|
1368
|
+
if (services2.length > 0) {
|
|
1369
|
+
content += "\n";
|
|
1370
|
+
for (const service of services2) {
|
|
1371
|
+
content += `
|
|
1372
|
+
logs-${service}:
|
|
1373
|
+
`;
|
|
1374
|
+
content += ` docker-compose logs -f ${service}
|
|
1375
|
+
`;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
content += `
|
|
1379
|
+
# Clean up containers and volumes
|
|
1380
|
+
clean:
|
|
1381
|
+
docker-compose down -v
|
|
1382
|
+
docker system prune -f
|
|
1383
|
+
|
|
1384
|
+
# Install dependencies (in running container)
|
|
1385
|
+
shell:
|
|
1386
|
+
docker-compose exec app sh
|
|
1387
|
+
|
|
1388
|
+
# Install dependencies
|
|
1389
|
+
install:
|
|
1390
|
+
docker-compose exec app npm install
|
|
1391
|
+
`;
|
|
1392
|
+
return content;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// src/generators/index.ts
|
|
1396
|
+
var GENERATORS = {
|
|
1397
|
+
dockerfile: { file: "Dockerfile", generate: generateDockerfile },
|
|
1398
|
+
compose: { file: "docker-compose.yml", generate: generateCompose },
|
|
1399
|
+
dockerignore: { file: ".dockerignore", generate: generateDockerignore },
|
|
1400
|
+
env: { file: ".env.example", generate: generateEnvExample },
|
|
1401
|
+
scripts: { file: "scripts", generate: () => "" },
|
|
1402
|
+
// Special handling
|
|
1403
|
+
makefile: { file: "Makefile", generate: generateMakefile }
|
|
1404
|
+
};
|
|
1405
|
+
function generateAll(config, context) {
|
|
1406
|
+
const result = {};
|
|
1407
|
+
const filesToGenerate = config.output.files ?? Object.keys(GENERATORS);
|
|
1408
|
+
for (const fileType of filesToGenerate) {
|
|
1409
|
+
if (fileType === "scripts") {
|
|
1410
|
+
const scripts = generateScripts(config);
|
|
1411
|
+
for (const [name, content] of Object.entries(scripts)) {
|
|
1412
|
+
result[name] = content;
|
|
1413
|
+
}
|
|
1414
|
+
} else {
|
|
1415
|
+
const generator = GENERATORS[fileType];
|
|
1416
|
+
if (generator) {
|
|
1417
|
+
result[generator.file] = generator.generate(config, context);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return result;
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
// src/utils/fs.ts
|
|
1425
|
+
var import_fs = require("fs");
|
|
1426
|
+
var import_path = require("path");
|
|
1427
|
+
async function ensureDir(path) {
|
|
1428
|
+
try {
|
|
1429
|
+
await import_fs.promises.mkdir(path, { recursive: true });
|
|
1430
|
+
} catch (error) {
|
|
1431
|
+
const err = error;
|
|
1432
|
+
if (err.code !== "EEXIST") {
|
|
1433
|
+
throw error;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
async function writeFile(filePath, content) {
|
|
1438
|
+
const dir = (0, import_path.dirname)(filePath);
|
|
1439
|
+
await ensureDir(dir);
|
|
1440
|
+
const tempPath = (0, import_path.join)(dir, `.${(0, import_path.basename)(filePath)}.tmp`);
|
|
1441
|
+
try {
|
|
1442
|
+
await import_fs.promises.writeFile(tempPath, content, "utf8");
|
|
1443
|
+
await import_fs.promises.rename(tempPath, filePath);
|
|
1444
|
+
} catch (error) {
|
|
1445
|
+
try {
|
|
1446
|
+
await import_fs.promises.unlink(tempPath);
|
|
1447
|
+
} catch {
|
|
1448
|
+
}
|
|
1449
|
+
throw error;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
async function fileExists(filePath) {
|
|
1453
|
+
try {
|
|
1454
|
+
const stat = await import_fs.promises.stat(filePath);
|
|
1455
|
+
return stat.isFile();
|
|
1456
|
+
} catch {
|
|
1457
|
+
return false;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
// src/core/dockcraft.ts
|
|
1462
|
+
var import_path2 = require("path");
|
|
1463
|
+
async function dockcraft(config, options = {}) {
|
|
1464
|
+
const validation = validateConfig(config);
|
|
1465
|
+
if (!validation.valid) {
|
|
1466
|
+
const errorMessages = validation.errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1467
|
+
throw new Error(
|
|
1468
|
+
`Configuration validation failed:
|
|
1469
|
+
${errorMessages}`
|
|
1470
|
+
);
|
|
1471
|
+
}
|
|
1472
|
+
const resolved = await resolveConfig(config);
|
|
1473
|
+
const resolvedValidation = validateResolvedConfig(resolved);
|
|
1474
|
+
if (!resolvedValidation.valid) {
|
|
1475
|
+
const errorMessages = resolvedValidation.errors.map((e) => ` - ${e.message}`).join("\n");
|
|
1476
|
+
throw new Error(
|
|
1477
|
+
`Resolved configuration validation failed:
|
|
1478
|
+
${errorMessages}`
|
|
1479
|
+
);
|
|
1480
|
+
}
|
|
1481
|
+
const context = {
|
|
1482
|
+
config: resolved,
|
|
1483
|
+
ports: new Map(
|
|
1484
|
+
Object.entries(resolved.services).map(([name, svc]) => [name, svc.port])
|
|
1485
|
+
),
|
|
1486
|
+
strings: new Map(
|
|
1487
|
+
Object.entries(resolved.services).filter(([, svc]) => svc.connectionString).map(([name, svc]) => [name, svc.connectionString])
|
|
1488
|
+
)
|
|
1489
|
+
};
|
|
1490
|
+
context.ports.set("app", resolved.app.port);
|
|
1491
|
+
const generated = generateAll(resolved, context);
|
|
1492
|
+
const written = [];
|
|
1493
|
+
const outputDir = resolved.output.dir ?? "./";
|
|
1494
|
+
if (!options.dryRun) {
|
|
1495
|
+
for (const [fileName, content] of Object.entries(generated)) {
|
|
1496
|
+
if (!fileName) continue;
|
|
1497
|
+
const filePath = (0, import_path2.join)(outputDir, fileName);
|
|
1498
|
+
const exists = await fileExists(filePath);
|
|
1499
|
+
if (exists && !resolved.output.overwrite) {
|
|
1500
|
+
continue;
|
|
1501
|
+
}
|
|
1502
|
+
await writeFile(filePath, content);
|
|
1503
|
+
written.push(filePath);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
return {
|
|
1507
|
+
files: new Map(Object.entries(generated)),
|
|
1508
|
+
written,
|
|
1509
|
+
config: resolved
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
function defineConfig(config) {
|
|
1513
|
+
return config;
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// src/plugins/api.ts
|
|
1517
|
+
function defineService(def) {
|
|
1518
|
+
return {
|
|
1519
|
+
name: def.name,
|
|
1520
|
+
services: {
|
|
1521
|
+
[def.name]: def
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
// src/ports/defaults.ts
|
|
1527
|
+
var DEFAULT_PORTS = {
|
|
1528
|
+
postgres: 5432,
|
|
1529
|
+
mysql: 3306,
|
|
1530
|
+
mongo: 27017,
|
|
1531
|
+
mongodb: 27017,
|
|
1532
|
+
redis: 6379,
|
|
1533
|
+
elasticsearch: 9200,
|
|
1534
|
+
rabbitmq: 5672,
|
|
1535
|
+
memcached: 11211,
|
|
1536
|
+
nginx: 80,
|
|
1537
|
+
apache: 80
|
|
1538
|
+
};
|
|
1539
|
+
function getDefaultPort(service) {
|
|
1540
|
+
if (service in DEFAULT_PORTS) {
|
|
1541
|
+
return DEFAULT_PORTS[service];
|
|
1542
|
+
}
|
|
1543
|
+
return void 0;
|
|
1544
|
+
}
|
|
1545
|
+
function listDefaultPorts() {
|
|
1546
|
+
return { ...DEFAULT_PORTS };
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
// src/types.ts
|
|
1550
|
+
var DockcraftError = class extends Error {
|
|
1551
|
+
constructor(message, code) {
|
|
1552
|
+
super(message);
|
|
1553
|
+
this.code = code;
|
|
1554
|
+
this.name = "DockcraftError";
|
|
1555
|
+
}
|
|
1556
|
+
};
|
|
1557
|
+
var ConfigError = class extends DockcraftError {
|
|
1558
|
+
constructor(message, code = "CONFIG_ERROR") {
|
|
1559
|
+
super(message, code);
|
|
1560
|
+
this.name = "ConfigError";
|
|
1561
|
+
}
|
|
1562
|
+
};
|
|
1563
|
+
var ValidationError = class extends DockcraftError {
|
|
1564
|
+
constructor(message, errors, code = "VALIDATION_ERROR") {
|
|
1565
|
+
super(message, code);
|
|
1566
|
+
this.errors = errors;
|
|
1567
|
+
this.name = "ValidationError";
|
|
1568
|
+
}
|
|
1569
|
+
};
|
|
1570
|
+
var PortConflictError = class extends DockcraftError {
|
|
1571
|
+
constructor(port, code = "PORT_CONFLICT") {
|
|
1572
|
+
super(`Port ${port} is already in use`, code);
|
|
1573
|
+
this.name = "PortConflictError";
|
|
1574
|
+
}
|
|
1575
|
+
};
|
|
1576
|
+
var NoPortAvailableError = class extends DockcraftError {
|
|
1577
|
+
constructor(range, code = "NO_PORT_AVAILABLE") {
|
|
1578
|
+
super(`No available port in range ${range[0]}-${range[1]}`, code);
|
|
1579
|
+
this.name = "NoPortAvailableError";
|
|
1580
|
+
}
|
|
1581
|
+
};
|
|
1582
|
+
var FileWriteError = class extends DockcraftError {
|
|
1583
|
+
constructor(path, code = "FILE_WRITE_ERROR") {
|
|
1584
|
+
super(`Failed to write file: ${path}`, code);
|
|
1585
|
+
this.name = "FileWriteError";
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
var PresetNotFoundError = class extends DockcraftError {
|
|
1589
|
+
constructor(name, code = "PRESET_NOT_FOUND") {
|
|
1590
|
+
super(`Preset '${name}' not found`, code);
|
|
1591
|
+
this.name = "PresetNotFoundError";
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
var ServiceNotFoundError = class extends DockcraftError {
|
|
1595
|
+
constructor(name, code = "SERVICE_NOT_FOUND") {
|
|
1596
|
+
super(`Service '${name}' not found`, code);
|
|
1597
|
+
this.name = "ServiceNotFoundError";
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
|
|
1601
|
+
// src/index.ts
|
|
1602
|
+
function getConnectionString(service, config) {
|
|
1603
|
+
switch (service) {
|
|
1604
|
+
case "postgres":
|
|
1605
|
+
const pgUser = config.user ?? "postgres";
|
|
1606
|
+
const pgPass = config.password ?? "postgres";
|
|
1607
|
+
const pgDb = config.database ?? "myapp";
|
|
1608
|
+
return `postgres://${pgUser}:${pgPass}@${config.host}:${config.port}/${pgDb}`;
|
|
1609
|
+
case "mysql":
|
|
1610
|
+
const mysqlUser = config.user ?? "root";
|
|
1611
|
+
const mysqlPass = config.password ?? "root";
|
|
1612
|
+
const mysqlDb = config.database ?? "myapp";
|
|
1613
|
+
return `mysql://${mysqlUser}:${mysqlPass}@${config.host}:${config.port}/${mysqlDb}`;
|
|
1614
|
+
case "mongo":
|
|
1615
|
+
case "mongodb":
|
|
1616
|
+
const mongoUser = config.user ?? "mongo";
|
|
1617
|
+
const mongoPass = config.password ?? "mongo";
|
|
1618
|
+
const mongoDb = config.database ?? "myapp";
|
|
1619
|
+
return `mongodb://${mongoUser}:${mongoPass}@${config.host}:${config.port}/${mongoDb}`;
|
|
1620
|
+
case "redis":
|
|
1621
|
+
return `redis://${config.host}:${config.port}`;
|
|
1622
|
+
default:
|
|
1623
|
+
throw new Error(`Unknown service: ${service}`);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1627
|
+
0 && (module.exports = {
|
|
1628
|
+
ConfigError,
|
|
1629
|
+
DockcraftError,
|
|
1630
|
+
FileWriteError,
|
|
1631
|
+
NoPortAvailableError,
|
|
1632
|
+
PortConflictError,
|
|
1633
|
+
PresetNotFoundError,
|
|
1634
|
+
ServiceNotFoundError,
|
|
1635
|
+
ValidationErrorClass,
|
|
1636
|
+
checkPorts,
|
|
1637
|
+
defineConfig,
|
|
1638
|
+
defineService,
|
|
1639
|
+
dockcraft,
|
|
1640
|
+
getConnectionString,
|
|
1641
|
+
getDefaultPort,
|
|
1642
|
+
getPreset,
|
|
1643
|
+
getService,
|
|
1644
|
+
isPortAvailable,
|
|
1645
|
+
listDefaultPorts,
|
|
1646
|
+
listPresets,
|
|
1647
|
+
listServices,
|
|
1648
|
+
registerService,
|
|
1649
|
+
resolvePort
|
|
1650
|
+
});
|
|
1651
|
+
//# sourceMappingURL=index.cjs.map
|