From 9969a7456155825420d2a0fd50b31a3a71451ee7 Mon Sep 17 00:00:00 2001 From: David Worms Date: Fri, 17 Nov 2023 20:09:58 +0100 Subject: [PATCH] refactor(core): mark requires as async --- packages/core/lib/actions/call/index.js | 4 ++-- packages/core/lib/plugins/tools/schema.js | 2 +- packages/core/lib/registry.js | 12 ++++++------ packages/core/lib/session.js | 2 ++ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/core/lib/actions/call/index.js b/packages/core/lib/actions/call/index.js index 81731d527..c01f9c0ef 100644 --- a/packages/core/lib/actions/call/index.js +++ b/packages/core/lib/actions/call/index.js @@ -6,7 +6,7 @@ const {mutate} = require('mixme'); // Action module.exports = { hooks: { - on_action: function(action) { + on_action: async function(action) { if (typeof action.metadata.argument !== 'string') { return; } @@ -16,7 +16,7 @@ module.exports = { if (mod.startsWith('.')) { mod = path.resolve(process.cwd(), mod); } - mod = require.main.require(mod); + mod = await require.main.require(mod); // The loaded action can have its own interpretation of an argument. // In order to avoid any conflict, we simply remove the // `action.metadata.argument` property. diff --git a/packages/core/lib/plugins/tools/schema.js b/packages/core/lib/plugins/tools/schema.js index 39d1fea45..3d537cf0d 100644 --- a/packages/core/lib/plugins/tools/schema.js +++ b/packages/core/lib/plugins/tools/schema.js @@ -65,7 +65,7 @@ module.exports = { switch (protocol) { case 'module:': try { - const act = require.main.require(pathname); + const act = await require.main.require(pathname); return accept({ definitions: act.metadata.definitions }); diff --git a/packages/core/lib/registry.js b/packages/core/lib/registry.js index 06a90b2c8..9b9e38c85 100644 --- a/packages/core/lib/registry.js +++ b/packages/core/lib/registry.js @@ -51,11 +51,11 @@ const create = function({chain, on_register, parent, plugins} = {}) { Load an action from the module name. */ - obj.load = function(module) { + obj.load = async function(module) { if (typeof module !== 'string') { throw Error(`Invalid Argument: module must be a string, got ${module.toString()}`); } - const action = require.main.require(module); + const action = await require.main.require(module); if (typeof action === 'function') { action = { handler: action @@ -265,7 +265,7 @@ const create = function({chain, on_register, parent, plugins} = {}) { return obj.chain || obj; } if (typeof action === 'string') { - action = obj.load(action); + action = await obj.load(action); } else if (typeof action === 'function') { action = { handler: action @@ -293,7 +293,7 @@ const create = function({chain, on_register, parent, plugins} = {}) { results.push((await walk(namespace, action))); } else { if (typeof action === 'string') { - action = obj.load(action); + action = await obj.load(action); } else if (typeof action === 'function') { action = { handler: action @@ -336,14 +336,14 @@ const create = function({chain, on_register, parent, plugins} = {}) { ``` */ - obj.deprecate = function(old_name, new_name, action) { + obj.deprecate = async function(old_name, new_name, action) { let handler; if (arguments.length === 2) { handler = new_name; new_name = null; } if (typeof action === 'string') { - action = obj.load(action); + action = await obj.load(action); } if (typeof handler === 'function') { action = { diff --git a/packages/core/lib/session.js b/packages/core/lib/session.js index 511cd254c..eae8ba6ac 100644 --- a/packages/core/lib/session.js +++ b/packages/core/lib/session.js @@ -50,6 +50,7 @@ const session = function(args, options = {}) { }; // Building the namespace before calling an action const on_get = function(target, name) { + // Return static properties if ((target[name] != null) && !action.registry.registered(name)) { if (typeof target[name] === 'function') { return target[name].bind(target); @@ -57,6 +58,7 @@ const session = function(args, options = {}) { return target[name]; } } + // Return the plugins instance in the root session if (namespace.length === 0) { switch (name) { case 'plugins':