UNPKG

@wocker/ws

Version:

Docker workspace for web projects

167 lines (166 loc) 6.92 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 NpmService_1 = require("./NpmService"); const makes_1 = require("../makes"); const utils_1 = require("../utils"); let PluginService = class PluginService { constructor(appConfigService, npmService, logService, cli) { this.appConfigService = appConfigService; this.npmService = npmService; this.logService = logService; this.cli = cli; } 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, beta) { const [, prefix = "@wocker/", name, suffix = "-plugin"] = /^(@wocker\/)?(\w+)(-plugin)?$/.exec(pluginName) || []; const fullName = `${prefix}${name}${suffix}`; try { if (await this.checkPlugin(fullName)) { this.appConfigService.addPlugin(fullName); this.appConfigService.save(); console.info(`Plugin ${fullName} activated`); return; } const packageInfo = await this.npmService.getPackageInfo(fullName); const env = packageInfo["dist-tags"].beta && beta ? "beta" : "latest"; await this.npmService.install(fullName, env); if (await this.checkPlugin(fullName)) { this.appConfigService.addPlugin(fullName, env); this.appConfigService.save(); console.info(`Plugin ${fullName}@${env} activated`); return; } } catch (err) { this.logService.error(err.message); } } async uninstall(pluginName) { const [, prefix = "@wocker/", name, suffix = "-plugin"] = /^(@wocker\/)?(\w+)(-plugin)?$/.exec(pluginName) || []; const fullName = `${prefix}${name}${suffix}`; 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) { return; } for (const plugin of this.appConfigService.plugins) { console.info(`Checking ${plugin.name}...`); try { const current = await this.getCurrentVersion(plugin.name); const res = await makes_1.Http.get("https://registry.npmjs.org") .send(plugin.name); if (res.status !== 200) { continue; } const { "dist-tags": { latest } } = res.data; this.logService.info(plugin.name, current, latest); if (!current || current < latest) { console.log(`Updating ${plugin.name}...`); await (0, utils_1.spawn)("npm", ["i", "-g", plugin.name]); } } catch (err) { this.logService.error(err.message); } } console.info("Done"); } async getCurrentVersion(name) { try { const { dependencies: { [name]: { version } } } = JSON.parse(await (0, utils_1.exec)(`npm ls --json -g ${name}`)); return 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, NpmService_1.NpmService, core_1.LogService, core_1.Cli]) ], PluginService);