UNPKG

@wocker/ws

Version:

Docker workspace for web projects

165 lines (164 loc) 7.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); 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 __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginService = void 0; const core_1 = require("@wocker/core"); const cli_table3_1 = __importDefault(require("cli-table3")); const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs")); const package_manager_1 = require("../../package-manager"); const makes_1 = require("../../../makes"); let PluginService = class PluginService { constructor(appConfigService, pm, registryService, logService, cli) { this.appConfigService = appConfigService; this.pm = pm; this.registryService = registryService; this.logService = logService; this.cli = cli; this.rule = "1.x.x"; } getPluginsTable() { const table = new cli_table3_1.default({ head: ["Name", "Env"], colWidths: [30] }); if (this.appConfigService.plugins.length === 0) { return yoctocolors_cjs_1.default.gray("No plugins installed"); } for (const plugin of this.appConfigService.plugins) { table.push([plugin.name, plugin.env]); } return table.toString(); } async checkPlugin(pluginName) { try { await this.import(pluginName); return true; } catch (err) { this.logService.error(err.message, { pluginName }); } return false; } async install(pluginName, version = "latest") { const [, prefix = "@wocker/", name, suffix = "-plugin"] = /^(@wocker\/)?(\w+)(-plugin)?$/.exec(pluginName) || []; const fullName = `${prefix}${name}${suffix}`; const currentVersion = await this.getCurrentVersion(fullName), wRule = makes_1.VersionRule.parse(this.rule), rule = makes_1.VersionRule.parse(version === "latest" ? "x" : version || this.rule); const packageInfo = await this.registryService.getPackageInfo(fullName); const versions = Object.keys(packageInfo.versions) .filter((version) => { return wRule.match(version, true) && rule.match(version, true); }) .sort((a, b) => { return makes_1.Version.parse(b).compare(a); }); const bestSatisfyingVersion = versions.find((version) => rule.match(version)) ?? versions.find((version) => rule.match(version, true)); if (!bestSatisfyingVersion) { throw new Error(`No matching version found for ${fullName}@${version}.`); } if ((!currentVersion || currentVersion !== bestSatisfyingVersion) || !await this.checkPlugin(fullName)) { await this.pm.install(fullName, bestSatisfyingVersion); } this.appConfigService.addPlugin(fullName, version); this.appConfigService.save(); console.info(`Plugin ${fullName} activated`); } async uninstall(pluginName) { const [, prefix = "@wocker/", name, suffix = "-plugin"] = /^(@wocker\/)?(\w+)(-plugin)?$/.exec(pluginName) || []; const fullName = `${prefix}${name}${suffix}`; if (await this.checkPlugin(fullName)) { await this.pm.uninstall(fullName); } this.appConfigService.removePlugin(fullName); this.appConfigService.save(); console.info(`Plugin ${fullName} deactivated`); } async import(name) { const { default: type } = await Promise.resolve(`${name}`).then(s => __importStar(require(s))); return new makes_1.Plugin(type); } async update() { if (this.appConfigService.plugins.length === 0) { console.info("No plugins installed"); return; } for (const plugin of this.appConfigService.plugins) { console.info(`Checking ${plugin.name}...`); try { await this.install(plugin.name, plugin.env); } catch (err) { console.info(err.message); this.logService.error(err.message); } } console.info("Done"); } async getCurrentVersion(name) { try { const packages = await this.pm.getPackages(), package1 = packages.find((p) => p.name === name); if (package1) { return package1.version; } } catch (err) { this.logService.error(`Failed to get current version of "${name}"`); } return null; } }; exports.PluginService = PluginService; exports.PluginService = PluginService = __decorate([ (0, core_1.Injectable)(), __metadata("design:paramtypes", [core_1.AppConfigService, package_manager_1.PackageManager, package_manager_1.RegistryService, core_1.LogService, core_1.Cli]) ], PluginService);