UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

100 lines (99 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DockerCompose = exports.DockerContainer = void 0; const node_child_process_1 = require("child_process"); const list_files_js_1 = require("../modules/helpers/list-files.js"); const os_js_1 = require("../parsers/os.js"); const write_js_1 = require("../services/write.js"); const runDockerCommand = (command, args, options, verbose) => new Promise((resolve) => { const dockerProcess = (0, node_child_process_1.spawn)(command, args, { ...options, shell: os_js_1.isWindows, }); if (verbose) { dockerProcess.stdout.setEncoding('utf8'); dockerProcess.stderr.setEncoding('utf8'); dockerProcess.stdout.on('data', write_js_1.log); dockerProcess.stderr.on('data', write_js_1.log); } dockerProcess.on('close', (code) => resolve(code === 0)); dockerProcess.on('error', () => resolve(false)); }); class DockerContainer { constructor(configs) { const { context, file, tagName, containerName, ports, environments, cache, detach, envFile, cwd, verbose, } = configs; this.tagName = tagName; this.containerName = containerName; this.file = file || './Dockerfile'; this.context = context || '.'; this.ports = ports ?? []; this.cache = cache; this.environments = environments ?? []; this.envFile = envFile; this.detach = detach; this.cwd = cwd ? (0, list_files_js_1.sanitizePath)(cwd) : undefined; this.verbose = verbose; } async build() { const args = ['build']; if (this.cache === false) args.push('--no-cache'); await runDockerCommand('docker', [...args, '-t', this.tagName, '-f', this.file, this.context], { cwd: this.cwd }, this.verbose); } start() { const args = ['run']; args.push(this.detach !== false ? '-d' : '--init'); args.push(...['--name', this.containerName]); for (const port of this.ports) args.push(...['-p', port]); for (const environment of this.environments) args.push(...['-e', environment]); if (this.envFile) args.push(...['--env-file', this.envFile]); return runDockerCommand('docker', [...args, this.tagName], { cwd: this.cwd }, this.verbose); } stop() { return runDockerCommand('docker', ['stop', this.containerName], { cwd: this.cwd }, this.verbose); } async remove() { await runDockerCommand('docker', ['rm', '-f', this.containerName], { cwd: this.cwd }, this.verbose); await runDockerCommand('docker', ['image', 'rm', '-f', this.tagName], { cwd: this.cwd }, this.verbose); } } exports.DockerContainer = DockerContainer; class DockerCompose { constructor(configs) { const { file, projectName, build, serviceName, envFile, detach, cwd, verbose, } = configs; this.file = file ?? './docker-compose.yml'; this.build = build; this.serviceName = serviceName; this.envFile = envFile; this.projectName = projectName; this.detach = detach; this.cwd = cwd ? (0, list_files_js_1.sanitizePath)(cwd) : undefined; this.verbose = verbose; } up() { const args = ['compose', '-f', this.file]; if (this.envFile) args.push(...['--env-file', this.envFile]); if (this.projectName) args.push(...['-p', this.projectName]); args.push('up'); args.push(this.detach !== false ? '-d' : '--abort-on-container-exit'); if (this.build) args.push('--build'); if (this.serviceName) args.push(this.serviceName); return runDockerCommand('docker', args, { cwd: this.cwd }, this.verbose); } down() { const args = ['-f', this.file]; if (this.envFile) args.push(...['--env-file', this.envFile]); if (this.projectName) args.push(...['-p', this.projectName]); return runDockerCommand('docker', ['compose', ...args, 'down'], { cwd: this.cwd }, this.verbose); } } exports.DockerCompose = DockerCompose;