UNPKG

@xec-sh/core

Version:

Universal shell execution engine

146 lines 4.55 kB
export class DockerFluentAPI { constructor(engine) { this.engine = engine; this.options = {}; } ephemeral(image) { this.options = { ...this.options, image }; return this; } container(name) { this.options = { ...this.options, container: name }; return this; } volumes(volumes) { if ('image' in this.options) { this.options.volumes = volumes; } else { console.warn('[xec-core] volumes() is only applicable for ephemeral containers'); } return this; } workdir(path) { this.options = { ...this.options, workdir: path }; return this; } user(user) { this.options = { ...this.options, user }; return this; } env(env) { this.options = { ...this.options, env }; return this; } network(network) { if ('image' in this.options) { this.options.network = network; } else { console.warn('[xec-core] network() is only applicable for ephemeral containers'); } return this; } ports(ports) { if ('image' in this.options) { this.options.ports = ports; } else { console.warn('[xec-core] ports() is only applicable for ephemeral containers'); } return this; } labels(labels) { if ('image' in this.options) { this.options.labels = labels; } else { console.warn('[xec-core] labels() is only applicable for ephemeral containers'); } return this; } privileged(privileged = true) { if ('image' in this.options) { this.options.privileged = privileged; } else { console.warn('[xec-core] privileged() is only applicable for ephemeral containers'); } return this; } exec(strings, ...values) { return this.run(strings, ...values); } run(strings, ...values) { if (!('image' in this.options) && !('container' in this.options)) { throw new Error('[xec-core] Docker fluent API requires either ephemeral() or container() to be called first'); } const dockerEngine = this.engine.docker(this.options); if ('run' in dockerEngine && typeof dockerEngine.run === 'function') { return dockerEngine.run(strings, ...values); } return dockerEngine.run(strings, ...values); } build(context, tag) { return new DockerFluentBuildAPI(this.engine, context, tag); } } export class DockerFluentBuildAPI { constructor(engine, context, tag) { this.engine = engine; this.buildOptions = { context, tag }; } dockerfile(path) { this.buildOptions.dockerfile = path; return this; } buildArgs(args) { this.buildOptions.buildArgs = args; return this; } target(target) { this.buildOptions.target = target; return this; } noCache(noCache = true) { this.buildOptions.noCache = noCache; return this; } async execute() { const args = ['build']; if (this.buildOptions.tag) { args.push('-t', this.buildOptions.tag); } if (this.buildOptions.dockerfile) { args.push('-f', this.buildOptions.dockerfile); } if (this.buildOptions.buildArgs) { for (const [key, value] of Object.entries(this.buildOptions.buildArgs)) { args.push('--build-arg', `${key}=${value}`); } } if (this.buildOptions.target) { args.push('--target', this.buildOptions.target); } if (this.buildOptions.noCache) { args.push('--no-cache'); } args.push(this.buildOptions.context); const commandStr = args.join(' '); const strings = [`docker ${commandStr}`]; strings.raw = strings; const result = await this.engine.run(strings); if (!result.ok) { throw new Error(`Docker build failed: ${result.stderr}`); } } async ephemeral(imageTag) { await this.execute(); const tag = imageTag || this.buildOptions.tag; if (!tag) { throw new Error('[xec-core] Image tag required for ephemeral container after build'); } return new DockerFluentAPI(this.engine).ephemeral(tag); } } //# sourceMappingURL=docker-fluent-api.js.map