-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerfile.ts
57 lines (47 loc) · 1.38 KB
/
dockerfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Multistage } from './multistage';
import { Arg } from "./commands/arg";
export const DEFAULT_PREAMBLE = `##############################
# Created with dockerfile.js #
# DO NOT EDIT #
##############################`
export interface DockerfileBuildContext {
dockerfile: Dockerfile;
}
export interface DockerfileOptions {
preamble: string;
}
export class Dockerfile extends Multistage {
protected _args: Arg[] = [];
protected _options : DockerfileOptions;
constructor(options : Partial<DockerfileOptions> = {}) {
super();
this._options = {
...{
preamble: DEFAULT_PREAMBLE
},
...options
};
}
args(): Arg[];
args(arg: Arg, ...args: Arg[]): this;
args(arg?: Arg, ...args: Arg[]): this | Arg[] {
if(arg) {
this._args.push(arg, ...args);
return this;
}
return this._args;
}
toString() {
const buildContext = {
dockerfile: this
};
return [
this._options.preamble,
this._args.map(arg => arg.toDockerCommand()).join('\n'),
...[ ...this ].map(stage => stage.toString(buildContext))
].filter(Boolean).join('\n\n')
}
}
export function dockerfile(...args : ConstructorParameters<typeof Dockerfile>) {
return new Dockerfile(...args);
}