UNPKG

@wocker/ws

Version:

Docker workspace for web projects

116 lines (115 loc) 4.05 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerService = void 0; const core_1 = require("@wocker/core"); const ModemService_1 = require("./ModemService"); let ContainerService = class ContainerService { constructor(logService, modemService) { this.logService = logService; this.modemService = modemService; } get docker() { return this.modemService.docker; } async get(name) { const containers = await this.docker.listContainers({ all: true, filters: { name: [name] } }); const container = containers.find((container) => { return container.Names.indexOf("/" + name) >= 0; }); if (!container) { return null; } return this.docker.getContainer(container.Id); } async rm(name) { const container = await this.get(name); if (!container) { return; } const { State: { Status } } = await container.inspect(); if (Status === "running" || Status === "restarting") { try { await container.stop(); } catch (err) { this.logService.error("DockerService.removeContainer", err.message); } } try { await container.remove(); } catch (err) { this.logService.error("DockerService.removeContainer: ", err.message); } } async exec(nameOrContainer, options, _tty) { const container = typeof nameOrContainer === "string" ? await this.get(nameOrContainer) : nameOrContainer; if (!container) { return; } const { cmd = [], tty = false, user } = Array.isArray(options) ? { cmd: options, tty: _tty } : options; const exec = await container.exec({ AttachStdin: true, AttachStdout: true, AttachStderr: true, Tty: tty, User: user, Cmd: cmd, ConsoleSize: [ process.stdout.rows, process.stdout.columns ] }); const stream = await exec.start({ hijack: true, stdin: tty, Tty: tty }); if (tty) { const handleResize = async () => { const [width, height] = process.stdout.getWindowSize(); this.logService.debug("Exec resize", { width, height }); await exec.resize({ w: width, h: height }); }; process.on("SIGWINCH", handleResize); try { await this.modemService.attachStream(stream); } finally { process.off("SIGWINCH", handleResize); } } return stream; } }; exports.ContainerService = ContainerService; exports.ContainerService = ContainerService = __decorate([ (0, core_1.Injectable)(), __metadata("design:paramtypes", [core_1.LogService, ModemService_1.ModemService]) ], ContainerService);