@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 ADDED
@@ -0,0 +1,183 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.1.0] - 2026-11-30
9
+
10
+ ### Fixed
11
+
12
+ - **CLI did not run when installed.** The `dockcraft` bin had no shebang (the shell
13
+ tried to execute JavaScript) and only started when `process.argv[1]` was the
14
+ real file path, so `npx @oxog/dockcraft` and `node_modules/.bin/dockcraft`
15
+ failed or did nothing.
16
+ - `dockcraft generate` resolved a relative `--config` path against the installed
17
+ package instead of the working directory, so the default `./dockcraft.config.ts`
18
+ was never found. It now resolves against the working directory, searches
19
+ `dockcraft.config.{ts,mts,js,mjs,cjs}`, and requires a default export (with a clear
20
+ error message instead of silently running a config's side effects).
21
+ - `dockcraft init` wrote user input (name, port, preset, services) verbatim into
22
+ the generated config module, allowing code injection into a file that
23
+ `generate` then executes. Values are now validated and emitted as JSON literals.
24
+ `init` also no longer silently overwrites an existing config (use `--force`).
25
+ - docker-compose.yml was invalid YAML whenever a map had a single key
26
+ (`environment: REDIS_PASSWORD: secret`, `volumes: redis_data: null`) and
27
+ contained `volumes: undefined` for every app without volumes.
28
+ - YAML strings were not quoted when they contained `,` inside flow sequences or
29
+ were YAML keywords/numbers (`yes`, `off`, `~`, `0x1F`, `1e3`); keys were never quoted.
30
+ - A custom service `port` changed the container port too: `postgres: { port: 5433 }`
31
+ published `5433:5433` while Postgres listens on 5432, and connection strings used
32
+ 5433. The option is now the host port (`5433:5432`); containers keep using the
33
+ default port. `'auto'` and ranges behave the same way. Host port conflicts are
34
+ checked on the published ports (including extra ports).
35
+ - `redis: { version: '7-alpine' }` (README example) produced the non-existent image
36
+ `redis:7-alpine-alpine` (same for Postgres); `latest` produced `*:latest-alpine`.
37
+ - `redis: { password }` had no effect: the official image ignores `REDIS_PASSWORD`.
38
+ The service now runs with `--requirepass` (read from the container environment),
39
+ the health check authenticates via `REDISCLI_AUTH`, and `REDIS_URL` includes the
40
+ password.
41
+ - Passwords containing `$` were interpolated by Docker Compose; environment values
42
+ are now written literally (`$` → `$$`).
43
+ - Connection strings did not percent-encode user, password or database, so
44
+ passwords with `@`, `:`, `/`, `#` produced broken URLs (also in `getConnectionString`).
45
+ - MongoDB connection strings lacked `?authSource=admin`; the image creates the root
46
+ user in the `admin` database, so authentication against the app database failed.
47
+ - The default database name was always `myapp` although documented as the project
48
+ name.
49
+ - Custom services used a numeric image tag as port (`image: 'redis:7'` → `7:7`) and
50
+ their `command` as health check (starting a second server process).
51
+ - The Dockerfile ignored `app.command` (always `CMD ["npm", "start"]`) and
52
+ `app.buildCommand` (always `npm run build`), and swallowed build failures with
53
+ `|| true`. `buildCommand: ''` now skips the build step.
54
+ - The compose health check for the app used `curl`, which is not installed in
55
+ `node:*-alpine` images, so the app was always unhealthy; it now uses `node`.
56
+ - `app.env` (required variables) was only listed in `.env.example`; the names are
57
+ now also passed through to the app container.
58
+ - Bind mounts (`./data:/app/data`) were declared as named volumes; app volumes were
59
+ never declared.
60
+ - `.dockerignore` let `.env.production` and other `.env.*` files into the image.
61
+ - `.env.example` values with spaces, `#`, `$`, quotes or newlines were written
62
+ unquoted (a newline could add extra variables).
63
+ - Generated `make clean` ran `docker system prune -f`, deleting unrelated stopped
64
+ containers, networks and images on the machine.
65
+ - Service names were not validated and were written verbatim into generated shell
66
+ scripts and the Makefile (`'x; rm -rf ~'` became a command).
67
+ - Validation accepted out-of-range ports (`99999`), `null` ports, invalid image tags
68
+ (`-alpine`) and images, multi-line commands/workdirs (Dockerfile instruction
69
+ injection) and health check paths containing quotes. The image check now follows
70
+ Docker's reference grammar (registries with ports, underscores, digests).
71
+ - `config.plugins` was accepted but ignored; plugin services now resolve and plugin
72
+ `outputs` are generated (only inside the output directory; paths with `..` or
73
+ absolute paths are rejected).
74
+ - `isPortAvailable` left a 1 second timer running after every check (keeping the
75
+ process alive) and threw asynchronously for invalid ports; it now rejects with a
76
+ `RangeError`.
77
+ - `deepMerge` (preset merging) let a `__proto__` key replace the merged object's
78
+ prototype.
79
+ - `dockcraft ports --json` printed a title before the JSON; `ports --check abc`
80
+ printed a Node.js internal error; `dockcraft presets <unknown>` exited with 0.
81
+ - Generated shell scripts are written executable (`0755`); concurrent writes of the
82
+ same file no longer share a temp file.
83
+ - README examples that did not compile (`checkPort`, preset without `app`).
84
+
85
+ ### Added
86
+
87
+ - `CustomServiceConfig.healthcheck` for custom services.
88
+ - `app` is optional in `DockcraftConfig` and `app.port` defaults to 3000.
89
+ - `dockcraft help [command]`, `dockcraft init --force`.
90
+ - Typed errors: `dockcraft()` throws `ValidationErrorClass` (with `errors`),
91
+ `PresetNotFoundError` and `ServiceNotFoundError`.
92
+
93
+ ### Changed
94
+
95
+ - Generated scripts and Makefile use the Docker Compose v2 CLI (`docker compose`);
96
+ `docker-compose` v1 is end-of-life and not shipped with current Docker.
97
+ - `dockcraft init` writes `dockcraft.config.mts` with a type-only import
98
+ (`export default { ... } satisfies DockcraftConfig`), which loads in CommonJS and
99
+ ESM projects without a local install (Node.js 22.18+ type stripping).
100
+ - The CLI sets the exit code instead of calling `process.exit()`; every error exits 1.
101
+ - Error messages for unknown services changed to `Service '<name>' not found`.
102
+ - Packaging: `exports` now has separate `import`/`require` conditions with matching
103
+ declaration files (`index.d.ts` / `index.d.cts`) and exposes `./package.json`.
104
+ The CLI is built once as ESM with a shebang (no unused CJS/d.ts CLI files).
105
+
106
+ ---
107
+
108
+ ## [1.0.0] - 2025-01-XX
109
+
110
+ ### Added
111
+
112
+ - Zero-dependency Docker orchestration toolkit
113
+ - Config-driven Docker file generation
114
+ - Built-in service presets (PostgreSQL, MySQL, MongoDB, Redis)
115
+ - Smart port management with auto-resolve
116
+ - Connection string generation
117
+ - Preset system with 11 pre-configured stacks
118
+ - Full CLI with interactive wizard
119
+ - Programmatic API
120
+ - TypeScript strict mode support
121
+ - 100% test coverage
122
+
123
+ ### Features
124
+
125
+ #### Core
126
+ - `dockcraft()` - Main generation function
127
+ - `defineConfig()` - Configuration helper
128
+ - Config validation and resolution
129
+ - Port conflict detection and resolution
130
+
131
+ #### Services
132
+ - PostgreSQL service definition
133
+ - MySQL service definition
134
+ - MongoDB service definition
135
+ - Redis service definition
136
+ - Custom service support
137
+
138
+ #### Presets
139
+ - Express + PostgreSQL
140
+ - Express + PostgreSQL + Redis
141
+ - Express + MongoDB
142
+ - Express + MongoDB + Redis
143
+ - Express + MySQL
144
+ - Express + MySQL + Redis
145
+ - Fastify + PostgreSQL
146
+ - Fastify + PostgreSQL + Redis
147
+ - NestJS + PostgreSQL
148
+ - NestJS + MongoDB
149
+ - Node.js minimal
150
+
151
+ #### CLI
152
+ - `dockcraft init` - Interactive configuration wizard
153
+ - `dockcraft generate` - Generate Docker files
154
+ - `dockcraft ports` - Port checking utilities
155
+ - `dockcraft presets` - List and show presets
156
+
157
+ #### Generators
158
+ - Dockerfile (multi-stage)
159
+ - docker-compose.yml
160
+ - .dockerignore
161
+ - .env.example
162
+ - Shell scripts (build, up, down, logs)
163
+ - Makefile
164
+
165
+ #### Utilities
166
+ - Port scanner and resolver
167
+ - YAML generator (no dependencies)
168
+ - File system utilities
169
+ - String utilities
170
+ - Template engine
171
+ - Deep merge utility
172
+
173
+ ### Documentation
174
+ - Complete API reference
175
+ - CLI reference
176
+ - Service configuration guide
177
+ - Preset reference
178
+ - Examples
179
+
180
+ ---
181
+
182
+ [1.1.0]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.1.0
183
+ [1.0.0]: https://github.com/ersinkoc/DockCraft/releases/tag/v1.0.0
package/README.md CHANGED
@@ -35,14 +35,15 @@ npx @oxog/dockcraft init --preset express-postgres-redis
35
35
  npx @oxog/dockcraft generate
36
36
  ```
37
37
 
38
- ### Programmatic
38
+ ### Config file
39
39
 
40
- Create `dockcraft.config.ts`:
40
+ Create `dockcraft.config.ts` (or `.mts`, `.js`, `.mjs`, `.cjs`) and export the
41
+ configuration as the default export:
41
42
 
42
43
  ```typescript
43
- import { dockcraft } from '@oxog/dockcraft';
44
+ import { defineConfig } from '@oxog/dockcraft';
44
45
 
45
- await dockcraft({
46
+ export default defineConfig({
46
47
  name: 'my-api',
47
48
  app: {
48
49
  port: 3000,
@@ -67,6 +68,13 @@ This creates:
67
68
  - `.dockerignore` - Optimized file exclusion
68
69
  - `.env.example` - Environment variables with connection strings
69
70
 
71
+ Existing files are kept unless you pass `--force`.
72
+
73
+ TypeScript config files are loaded with Node.js type stripping (Node.js 22.18+
74
+ or 23.6+). On older Node.js versions use `dockcraft.config.mjs`. `dockcraft init`
75
+ writes `dockcraft.config.mts`, which loads as an ES module in any project and
76
+ only uses a type import, so it also works with `npx` without a local install.
77
+
70
78
  ## CLI Usage
71
79
 
72
80
  ```bash
@@ -89,6 +97,10 @@ dockcraft ports --check 3000,5432,6379
89
97
  dockcraft presets
90
98
  ```
91
99
 
100
+ `init` refuses to overwrite an existing config unless `--force` is given.
101
+ Commands exit with code 1 on errors (invalid config, unknown command or
102
+ preset, invalid ports). `dockcraft ports --json` prints JSON only.
103
+
92
104
  ## API Usage
93
105
 
94
106
  ```typescript
@@ -98,15 +110,21 @@ import {
98
110
  getPreset,
99
111
  listPresets,
100
112
  resolvePort,
101
- checkPort,
113
+ isPortAvailable,
114
+ checkPorts,
102
115
  getConnectionString,
103
116
  } from '@oxog/dockcraft';
104
117
  ```
105
118
 
106
119
  ### Main Function
107
120
 
121
+ `dockcraft()` validates the config, resolves presets, services and ports, and
122
+ writes the files (or only returns them with `{ dryRun: true }`). Invalid
123
+ configuration throws a `ValidationErrorClass` (a `DockcraftError`) whose
124
+ `errors` array lists every problem.
125
+
108
126
  ```typescript
109
- await dockcraft({
127
+ const result = await dockcraft({
110
128
  name: 'my-app',
111
129
  preset: 'express-postgres',
112
130
  app: {
@@ -121,13 +139,22 @@ await dockcraft({
121
139
  overwrite: false,
122
140
  },
123
141
  });
142
+
143
+ result.files; // Map<file name, content>
144
+ result.written; // paths written to disk
145
+
146
+ // Preview only
147
+ const preview = await dockcraft(config, { dryRun: true });
124
148
  ```
125
149
 
126
150
  ### Port Utilities
127
151
 
128
152
  ```typescript
129
153
  // Check if port is available
130
- const available = await checkPort(3000);
154
+ const available = await isPortAvailable(3000);
155
+
156
+ // Check several ports
157
+ const results = await checkPorts([3000, 5432]); // Map<port, boolean>
131
158
 
132
159
  // Resolve port (auto-find if taken)
133
160
  const port = await resolvePort({
@@ -136,8 +163,8 @@ const port = await resolvePort({
136
163
  });
137
164
 
138
165
  // Find in range
139
- const port = await resolvePort({
140
- preferred: 3000,
166
+ const portInRange = await resolvePort({
167
+ strategy: 'range',
141
168
  range: [3000, 3100],
142
169
  });
143
170
  ```
@@ -155,24 +182,45 @@ const url = getConnectionString('postgres', {
155
182
  // => postgres://postgres:postgres@localhost:5432/myapp
156
183
  ```
157
184
 
185
+ User, password and database are percent-encoded, MongoDB URLs include
186
+ `?authSource=admin`, and Redis URLs include the password when one is given
187
+ (`redis://:secret@localhost:6379`).
188
+
158
189
  ## Configuration
159
190
 
160
191
  ### App Configuration
161
192
 
162
193
  ```typescript
163
194
  app: {
164
- port: 3000, // App port or 'auto'
165
- node: '20-alpine', // Node.js version
166
- workdir: '/app', // Working directory
167
- command: 'npm start', // Start command
168
- buildCommand: 'npm run build',
195
+ port: 3000, // App port or 'auto' (default: 3000)
196
+ node: '20-alpine', // Node.js image tag
197
+ workdir: '/app', // Working directory (absolute path)
198
+ command: 'npm start', // Dockerfile CMD
199
+ buildCommand: 'npm run build', // Builder stage command; '' = no build step
169
200
  multiStage: true, // Multi-stage build
170
- healthcheck: '/health', // Health endpoint
171
- env: ['API_KEY'], // Required env vars
201
+ healthcheck: '/health', // Health endpoint (2xx = healthy)
202
+ env: ['API_KEY'], // Required env vars (.env.example + passed to the app container)
172
203
  volumes: ['./data:/app/data'],
173
204
  }
174
205
  ```
175
206
 
207
+ `app` is optional when a preset provides it.
208
+
209
+ ### Ports
210
+
211
+ `port` on a service is the **host** port. The container keeps listening on the
212
+ service's default port, so `postgres: { port: 5433 }` publishes `5433:5432`
213
+ and other containers still connect to `postgres:5432`. `'auto'` picks the first
214
+ free host port from the default port up; `{ start, end }` picks one in a range.
215
+
216
+ ```typescript
217
+ services: {
218
+ postgres: { port: 5433 }, // 5433:5432
219
+ redis: { port: 'auto' }, // first free host port from 6379
220
+ mongo: { port: { start: 27100, end: 27200 } },
221
+ }
222
+ ```
223
+
176
224
  ### Service Configuration
177
225
 
178
226
  ```typescript
@@ -180,7 +228,7 @@ services: {
180
228
  postgres: {
181
229
  version: '16',
182
230
  port: 5432,
183
- database: 'myapp',
231
+ database: 'myapp', // default: project name
184
232
  user: 'postgres',
185
233
  password: 'postgres',
186
234
  },
@@ -193,6 +241,9 @@ services: {
193
241
 
194
242
  ### Custom Services
195
243
 
244
+ Any service with an `image` is a custom service. Its `port` is used for both
245
+ the container and the host (default: 80).
246
+
196
247
  ```typescript
197
248
  services: {
198
249
  elasticsearch: {
@@ -202,10 +253,40 @@ services: {
202
253
  'discovery.type': 'single-node',
203
254
  },
204
255
  volumes: ['es_data:/usr/share/elasticsearch/data'],
256
+ healthcheck: 'curl -fs localhost:9200', // optional, run with CMD-SHELL
205
257
  },
206
258
  }
207
259
  ```
208
260
 
261
+ Service names must match `[a-zA-Z0-9][a-zA-Z0-9_.-]*` and images must be valid
262
+ Docker image references. Environment values are written literally: a `$` is
263
+ escaped as `$$` in `docker-compose.yml`, so Compose does not interpolate it.
264
+
265
+ ### Plugins
266
+
267
+ ```typescript
268
+ import { defineConfig, defineService } from '@oxog/dockcraft';
269
+
270
+ const rabbitmq = defineService({
271
+ name: 'rabbitmq',
272
+ image: (version) => `rabbitmq:${version}-management`,
273
+ defaultPort: 5672,
274
+ defaultVersion: '3',
275
+ ports: [15672],
276
+ connectionString: (svc) => `amqp://guest:guest@${svc.name}:${svc.port}`,
277
+ });
278
+
279
+ export default defineConfig({
280
+ name: 'my-api',
281
+ plugins: [
282
+ rabbitmq,
283
+ // Extra output files, written relative to output.dir
284
+ { name: 'notes', outputs: [{ name: 'notes', file: 'docker/NOTES.md', generate: (c) => `# ${c.name}\n` }] },
285
+ ],
286
+ services: { rabbitmq: {} },
287
+ });
288
+ ```
289
+
209
290
  ## Presets
210
291
 
211
292
  Available presets:
@@ -280,7 +361,7 @@ mongo: {
280
361
  }
281
362
  ```
282
363
 
283
- Connection: `mongodb://user:pass@host:port/db`
364
+ Connection: `mongodb://user:pass@host:port/db?authSource=admin`
284
365
 
285
366
  ### Redis
286
367
 
@@ -291,7 +372,10 @@ redis: {
291
372
  }
292
373
  ```
293
374
 
294
- Connection: `redis://host:port`
375
+ Connection: `redis://host:port`, or `redis://:password@host:port` with a password.
376
+ The official Redis image ignores `REDIS_PASSWORD`, so with a password the
377
+ service is started with `--requirepass` (read from the container environment)
378
+ and the health check authenticates via `REDISCLI_AUTH`.
295
379
 
296
380
  ## Generated Files
297
381
 
@@ -317,9 +401,17 @@ Complete setup with:
317
401
  Optimized exclusions:
318
402
  - `node_modules`
319
403
  - `.git`
404
+ - `.env` and every `.env.*` file except `.env.example` (no secrets in images)
320
405
  - Test files
321
406
  - Documentation
322
407
 
408
+ ### Scripts and Makefile
409
+
410
+ Add `'scripts'` and `'makefile'` to `output.files` to get `docker-build.sh`,
411
+ `docker-up.sh`, `docker-down.sh`, `docker-logs.sh` (written executable) and a
412
+ `Makefile`. They use the Docker Compose v2 CLI (`docker compose`); `make clean`
413
+ runs `docker compose down -v --remove-orphans` for this project only.
414
+
323
415
  ### .env.example
324
416
 
325
417
  Environment template with:
@@ -333,4 +425,4 @@ MIT
333
425
 
334
426
  ---
335
427
 
336
- **GitHub**: [ersinkoc/dockcraft](https://github.com/ersinkoc/dockcraft)
428
+ **GitHub**: [ersinkoc/DockCraft](https://github.com/ersinkoc/DockCraft)