UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

86 lines (85 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionsInitializer = void 0; const path = require("path"); const index_1 = require("../index"); const safeGlob_1 = require("../modules/utils/safeGlob"); class ActionsInitializer extends index_1.Initializer { constructor() { super(); this.name = "actions"; this.loadPriority = 410; } async initialize() { index_1.api.actions = { actions: {}, versions: {}, middleware: {}, globalMiddleware: [], }; index_1.api.actions.loadFile = async (fullFilePath, reload = false) => { const loadMessage = (action) => { if (reload) { (0, index_1.log)(`action reloaded: ${action.name} @ v${action.version}, ${fullFilePath}`, "info"); } else { (0, index_1.log)(`action loaded: ${action.name} @ v${action.version}, ${fullFilePath}`, "debug"); } }; let action; try { let collection = await Promise.resolve(`${fullFilePath}`).then(s => require(s)); if (typeof collection === "function") { collection = [collection]; } for (const i in collection) { action = new collection[i](); await action.validate(index_1.api); if (!index_1.api.actions.actions[action.name]) { index_1.api.actions.actions[action.name] = {}; } if (!index_1.api.actions.versions[action.name]) { index_1.api.actions.versions[action.name] = []; } if (index_1.api.actions.actions[action.name][action.version] && !reload) { (0, index_1.log)(`an existing action with the same name \`${action.name}\` will be overridden by the file ${fullFilePath}`, "warning"); } index_1.api.actions.actions[action.name][action.version] = action; index_1.api.actions.versions[action.name].push(action.version); index_1.api.actions.versions[action.name].sort(); loadMessage(action); } } catch (error) { try { index_1.api.exceptionHandlers.initializer(error, fullFilePath); delete index_1.api.actions.actions[action.name][action.version]; } catch (_error) { throw error; } } }; for (const p of index_1.config.general.paths.action) { let files = (0, safeGlob_1.safeGlobSync)(path.join(p, "**", "**/*(*.js|*.ts)")); files = index_1.utils.ensureNoTsHeaderOrSpecFiles(files); for (const j in files) { await index_1.api.actions.loadFile(files[j]); } } for (const plugin of Object.values(index_1.config.plugins)) { if (plugin.actions !== false) { const pluginPath = path.normalize(plugin.path); // old style at the root of the project let files = (0, safeGlob_1.safeGlobSync)(path.join(pluginPath, "actions", "**", "*.js")); files = files.concat((0, safeGlob_1.safeGlobSync)(path.join(pluginPath, "dist", "actions", "**", "*.js"))); index_1.utils .ensureNoTsHeaderOrSpecFiles(files) .forEach((f) => index_1.api.actions.loadFile(f)); } } // now that the actions are loaded, we can add all the inputs to api.params index_1.api.params.buildPostVariables(); } } exports.ActionsInitializer = ActionsInitializer;