Skip to content

Commit 83884bf

Browse files
committed
feat: add typed compose object support
Add a `compose` option to `IDockerComposeOptions` that accepts a typed `ComposeSpecification` object. The object is serialized to YAML internally and piped via stdin using the existing `configAsString` path. TypeScript types are generated from the official Compose Specification JSON Schema (Apache 2.0) using json-schema-to-typescript.
1 parent 96b0294 commit 83884bf

8 files changed

Lines changed: 4580 additions & 3 deletions

File tree

docs/api.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ compose.upAll({
7070
| `executablePath` | `string` | Path to docker-compose executable if not in `$PATH` |
7171
| `config` | `string \| string[]` | Custom yml file(s), relative to `cwd` |
7272
| `configAsString` | `string` | Configuration as string (ignores `config` if set) |
73+
| `compose` | `ComposeSpecification` | Typed compose configuration object (converted to YAML internally) |
7374
| `log` | `boolean` | Enable console logging |
7475
| `composeOptions` | `string[] \| Array<string \| string[]>` | Options for all commands (e.g., `--verbose`) |
7576
| `commandOptions` | `string[] \| Array<string \| string[]>` | Options for specific command |
@@ -89,3 +90,30 @@ compose.upAll({
8990
commandOptions: ['--build', ['--timeout', '30']]
9091
})
9192
```
93+
94+
### Example with typed compose object
95+
96+
Instead of using a YAML file, you can pass a typed `ComposeSpecification` object directly. This gives you full TypeScript autocompletion and type checking for the Docker Compose configuration.
97+
98+
```typescript
99+
import { upAll, ComposeSpecification } from 'docker-compose'
100+
101+
const compose: ComposeSpecification = {
102+
services: {
103+
web: {
104+
image: 'nginx:latest',
105+
ports: ['8080:80']
106+
},
107+
db: {
108+
image: 'postgres:16',
109+
environment: {
110+
POSTGRES_PASSWORD: 'secret'
111+
}
112+
}
113+
}
114+
}
115+
116+
await upAll({ compose })
117+
```
118+
119+
The `ComposeSpecification` type is generated from the official [Compose Specification JSON Schema](https://github.com/compose-spec/compose-spec), so it covers all valid compose file options.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"ci": "yarn install --frozen-lockfile",
99
"test": "npx vitest --dir test --test-timeout=30000",
1010
"lint": "eslint src/**/*.ts test/**/*.ts",
11-
"build": "tsc",
12-
"prepublishOnly": "tsc",
11+
"generate:types": "json2ts schema/compose-spec.json src/compose-spec.d.ts",
12+
"build": "yarn generate:types && tsc",
13+
"prepublishOnly": "yarn generate:types && tsc",
1314
"release": "yarn build && standard-version"
1415
},
1516
"repository": {
@@ -94,6 +95,7 @@
9495
"eslint-plugin-prettier": "^3.3.1",
9596
"eslint-watch": "^7.0.0",
9697
"husky": "^6.0.0",
98+
"json-schema-to-typescript": "^15.0.4",
9799
"prettier": "^2.2.1",
98100
"standard-version": "9.5.0",
99101
"typescript": "^5.0.0",

schema/NOTICE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The file compose-spec.json is sourced from the Compose Specification project:
2+
https://github.com/compose-spec/compose-go
3+
4+
Copyright The Compose Specification Authors.
5+
Licensed under the Apache License, Version 2.0.
6+
See https://www.apache.org/licenses/LICENSE-2.0

0 commit comments

Comments
 (0)