UNPKG

@grouparoo/core

Version:
123 lines (122 loc) 4.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginUninstall = exports.PluginInstall = exports.PluginsList = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const optionallyAuthenticatedAction_1 = require("../classes/actions/optionallyAuthenticatedAction"); const plugins_1 = require("../modules/plugins"); const actionhero_1 = require("actionhero"); const apiData_1 = require("../modules/apiData"); const restartSleepTime = 100; class PluginsList extends optionallyAuthenticatedAction_1.OptionallyAuthenticatedAction { constructor() { super(...arguments); this.name = "plugins:list"; this.description = "I will return a list of the installed and available grouparoo plugins"; this.permission = { topic: "system", mode: "read" }; this.inputs = { includeInstalled: { required: false, formatter: apiData_1.APIData.ensureBoolean, }, includeAvailable: { required: false, formatter: apiData_1.APIData.ensureBoolean, }, includeVersions: { required: false, formatter: apiData_1.APIData.ensureBoolean, }, }; } async runWithinTransaction({ params }) { return { plugins: await (0, plugins_1.listPlugins)(params.includeInstalled, params.includeAvailable, params.includeVersions), }; } } exports.PluginsList = PluginsList; class PluginInstall extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "plugin:install"; this.description = "I install a Grouparoo plugin"; this.permission = { topic: "system", mode: "write" }; this.inputs = { plugin: { required: true }, restart: { required: false, default: false, formatter: apiData_1.APIData.ensureBoolean, }, }; } async runWithinTransaction({ params, }) { const response = await (0, plugins_1.install)(params.plugin); if (!response.success) return { success: false }; // Return if did not ask to restart if (!params.restart) return { success: response.success }; // Otherwise, return, then restart the server. setTimeout(() => safelyRestart(), restartSleepTime); return { success: response.success, checkIn: restartSleepTime * 4 }; } } exports.PluginInstall = PluginInstall; class PluginUninstall extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "plugin:uninstall"; this.description = "I uninstall a Grouparoo plugin"; this.permission = { topic: "system", mode: "write" }; this.inputs = { plugin: { required: true }, restart: { required: false, default: false, formatter: apiData_1.APIData.ensureBoolean, }, }; } async runWithinTransaction({ params, }) { const response = await (0, plugins_1.uninstall)(params.plugin); if (!response.success) return { success: false }; // Return if did not ask to restart if (!params.restart) return { success: response.success }; // Otherwise, return, then restart the server. setTimeout(() => safelyRestart(), restartSleepTime); return { success: response.success, checkIn: restartSleepTime * 4 }; } } exports.PluginUninstall = PluginUninstall; async function safelyRestart() { var _a; try { clearPluginConfigCache(); await actionhero_1.api.process.restart(); } catch (error) { console.log(""); console.log("*** There was a problem restarting Grouparoo ***"); console.log((_a = error.message) !== null && _a !== void 0 ? _a : error); console.log(""); console.log("Please restart the application"); process.exit(1); } } function clearPluginConfigCache() { const files = ["plugins.js"]; files.forEach((file) => { const fullPath = path_1.default.join(__dirname, "..", "config", file); if (fs_1.default.existsSync(fullPath)) { delete require.cache[require.resolve(fullPath)]; } }); }