UNPKG

@wocker/ws

Version:

Docker workspace for web projects

181 lines (180 loc) 7.66 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProxyController = void 0; const core_1 = require("@wocker/core"); const utils_1 = require("@wocker/utils"); const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs")); const project_1 = require("../../project"); const ProxyService_1 = require("../services/ProxyService"); let ProxyController = class ProxyController { constructor(appConfigService, eventService, projectService, proxyService) { this.appConfigService = appConfigService; this.eventService = eventService; this.projectService = projectService; this.proxyService = proxyService; this.eventService.on("project:init", (project) => this.proxyService.init(project)); this.eventService.on("project:start", (project) => this.onProjectStart(project)); this.eventService.on("project:stop", (project) => this.onProjectStop(project)); } async onProjectStart(project) { if (project.domains.length === 0) { return; } console.info(yoctocolors_cjs_1.default.green("Don't forget to add these lines into hosts file:")); for (const domain of project.domains) { console.info(yoctocolors_cjs_1.default.gray(`127.0.0.1 ${domain}`)); } await this.start(); } async onProjectStop(_project) { } getProjectNames() { const projects = this.projectService.search(); return projects.map((project) => project.name); } async init(httpPort, httpsPort, sshPort, sshPassword) { const config = this.appConfigService.config; if (httpPort === null || typeof httpPort === "undefined" || isNaN(httpPort)) { httpPort = await (0, utils_1.promptInput)({ required: true, message: "Http port", type: "number", default: parseInt(config.getMeta("PROXY_HTTP_PORT", "80")) }); } config.setMeta("PROXY_HTTP_PORT", httpPort.toString()); if (httpsPort === null || typeof httpsPort === "undefined" || isNaN(httpsPort)) { httpsPort = await (0, utils_1.promptInput)({ required: true, message: "Https port", type: "number", default: parseInt(config.getMeta("PROXY_HTTPS_PORT", "443")) }); } config.setMeta("PROXY_HTTPS_PORT", httpsPort.toString()); let enableSsh = !sshPassword && !sshPort ? await (0, utils_1.promptConfirm)({ message: "Enable ssh proxy?", default: false }) : true; if (enableSsh) { if (!sshPassword) { sshPassword = await (0, utils_1.promptInput)({ message: "SSH Password", type: "password", required: true, default: config.getMeta("PROXY_SSH_PASSWORD") }); } if (!sshPort) { sshPort = await (0, utils_1.promptInput)({ message: "SSH port", type: "number", default: parseInt(config.getMeta("PROXY_SSH_PORT", "22")) }); } config.setMeta("PROXY_SSH_PASSWORD", sshPassword); config.setMeta("PROXY_SSH_PORT", sshPort.toString()); } else { config.unsetMeta("PROXY_SSH_PASSWORD"); config.unsetMeta("PROXY_SSH_PORT"); } this.appConfigService.save(); } async start(restart, rebuild) { await this.proxyService.start(restart, rebuild); } async stop() { console.info("Proxy stopping..."); await this.proxyService.stop(); } async logs() { await this.proxyService.logs(); } }; exports.ProxyController = ProxyController; __decorate([ (0, core_1.Completion)("name"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Array) ], ProxyController.prototype, "getProjectNames", null); __decorate([ (0, core_1.Command)("proxy:init"), (0, core_1.Description)("Initializes proxy configurations"), __param(0, (0, core_1.Option)("http-port", { type: "number", description: "Http port" })), __param(1, (0, core_1.Option)("https-port", { type: "number", description: "Https port" })), __param(2, (0, core_1.Option)("ssh-port", { type: "number", description: "SSH port" })), __param(3, (0, core_1.Option)("ssh-password", { type: "string", description: "SSH password" })), __metadata("design:type", Function), __metadata("design:paramtypes", [Number, Number, Number, String]), __metadata("design:returntype", Promise) ], ProxyController.prototype, "init", null); __decorate([ (0, core_1.Command)("proxy:start"), (0, core_1.Description)("This command starts the proxy for the project. Options are available to restart or rebuild the proxy if needed."), __param(0, (0, core_1.Option)("restart", { type: "boolean", alias: "r", description: "Restarts the proxy before starting it" })), __param(1, (0, core_1.Option)("rebuild", { type: "boolean", alias: "b", description: "Rebuilds the proxy before starting it" })), __metadata("design:type", Function), __metadata("design:paramtypes", [Boolean, Boolean]), __metadata("design:returntype", Promise) ], ProxyController.prototype, "start", null); __decorate([ (0, core_1.Command)("proxy:stop"), (0, core_1.Description)("This command stops the currently running proxy for the project. It ensures that all proxy-related services are properly halted."), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ProxyController.prototype, "stop", null); __decorate([ (0, core_1.Command)("proxy:logs"), (0, core_1.Description)("Displays the proxy logs"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ProxyController.prototype, "logs", null); exports.ProxyController = ProxyController = __decorate([ (0, core_1.Controller)(), (0, core_1.Description)("Proxy commands"), __metadata("design:paramtypes", [core_1.AppConfigService, core_1.EventService, project_1.ProjectService, ProxyService_1.ProxyService]) ], ProxyController);