From d9af637f6984cade27a7a9115c6974b740417a11 Mon Sep 17 00:00:00 2001 From: Shawn McKnight Date: Sat, 18 May 2024 17:51:49 -0400 Subject: [PATCH 1/2] Support config from function for default exports --- src/runner-esm.mjs | 40 +++++++++++++++++++------------------- src/runner.js | 48 +++++++++++++++++++++++----------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/runner-esm.mjs b/src/runner-esm.mjs index ba17f24bc..e3c1c091a 100644 --- a/src/runner-esm.mjs +++ b/src/runner-esm.mjs @@ -56,7 +56,6 @@ export default class MoleculerRunner { this.watchFolders = []; this.flags = null; - this.configFile = null; this.config = null; this.servicePaths = null; this.broker = null; @@ -182,23 +181,24 @@ export default class MoleculerRunner { filePath = this.tryConfigPath(path.resolve(process.cwd(), "moleculer.config.json")); } - if (filePath != null) { - const ext = path.extname(filePath); - switch (ext) { - case ".json": - case ".js": - case ".mjs": - case ".ts": { - const mod = await import(filePath.startsWith("/") ? filePath : "/" + filePath); - let content = mod.default; - - if (utils.isFunction(content)) content = await content.call(this); - this.configFile = content; - break; - } - default: - return Promise.reject(new Error(`Not supported file extension: ${ext}`)); + if (filePath == null) { + return Promise.resolve({}); + } + + const ext = path.extname(filePath); + switch (ext) { + case ".json": + case ".js": + case ".mjs": + case ".ts": + case ".mts": { + const mod = await import(filePath.startsWith("/") ? filePath : "/" + filePath); + let content = mod.default; + + return utils.isFunction(content) ? content.call(this) : content; } + default: + return Promise.reject(new Error(`Not supported file extension: ${ext}`)); } } @@ -299,8 +299,8 @@ export default class MoleculerRunner { * Env variable: `CIRCUITBREAKER_ENABLED` * */ - mergeOptions() { - this.config = _.defaultsDeep(this.configFile, ServiceBroker.defaultOptions); + mergeOptions(configFromFile) { + this.config = _.defaultsDeep(configFromFile, ServiceBroker.defaultOptions); this.config = this.overwriteFromEnv(this.config); @@ -515,7 +515,7 @@ export default class MoleculerRunner { return Promise.resolve() .then(() => this.loadEnvFile()) .then(() => this.loadConfigFile()) - .then(() => this.mergeOptions()) + .then(configFromFile => this.mergeOptions(configFromFile)) .then(() => this.startBroker()) .catch(err => { logger.error(err); diff --git a/src/runner.js b/src/runner.js index 70507238c..2424d8312 100644 --- a/src/runner.js +++ b/src/runner.js @@ -53,7 +53,6 @@ class MoleculerRunner { this.watchFolders = []; this.flags = null; - this.configFile = null; this.config = null; this.servicePaths = null; this.broker = null; @@ -176,27 +175,28 @@ class MoleculerRunner { filePath = this.tryConfigPath(path.resolve(process.cwd(), "moleculer.config.json")); } - if (filePath != null) { - const ext = path.extname(filePath); - switch (ext) { - case ".json": - case ".js": - case ".ts": { - const content = require(filePath); - return Promise.resolve() - .then(() => { - if (utils.isFunction(content)) return content.call(this); - else return content; - }) - .then( - res => - (this.configFile = - res.default != null && res.__esModule ? res.default : res) - ); - } - default: - return Promise.reject(new Error(`Not supported file extension: ${ext}`)); + if (filePath == null) { + // no configuration file found + return Promise.resolve({}); + } + + const ext = path.extname(filePath); + switch (ext) { + case ".json": + case ".js": + case ".ts": { + return Promise.resolve(require(filePath)) + .then(content => { + return content.default != null && content.__esModule + ? content.default + : content; + }) + .then(mod => { + return utils.isFunction(mod) ? mod.call(this) : mod; + }); } + default: + return Promise.reject(new Error(`Not supported file extension: ${ext}`)); } } @@ -297,8 +297,8 @@ class MoleculerRunner { * Env variable: `CIRCUITBREAKER_ENABLED` * */ - mergeOptions() { - this.config = _.defaultsDeep(this.configFile, ServiceBroker.defaultOptions); + mergeOptions(configFromFile) { + this.config = _.defaultsDeep(configFromFile, ServiceBroker.defaultOptions); this.config = this.overwriteFromEnv(this.config); @@ -525,7 +525,7 @@ class MoleculerRunner { return Promise.resolve() .then(() => this.loadEnvFile()) .then(() => this.loadConfigFile()) - .then(() => this.mergeOptions()) + .then(configFromFile => this.mergeOptions(configFromFile)) .then(() => this.startBroker()) .catch(err => { logger.error(err); From ca7cb2e30f2bff3f5065f87b3fd4129395134008 Mon Sep 17 00:00:00 2001 From: Shawn McKnight Date: Sat, 18 May 2024 18:12:33 -0400 Subject: [PATCH 2/2] Add some ts config files with default exports for testing --- examples/runner/moleculer.config.async.ts | 11 +++++++++++ examples/runner/moleculer.config.ts | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 examples/runner/moleculer.config.async.ts create mode 100644 examples/runner/moleculer.config.ts diff --git a/examples/runner/moleculer.config.async.ts b/examples/runner/moleculer.config.async.ts new file mode 100644 index 000000000..39b2e507e --- /dev/null +++ b/examples/runner/moleculer.config.async.ts @@ -0,0 +1,11 @@ +import fetch from "node-fetch"; + +/** + * Test: + * + * npx ts-node -T bin\moleculer-runner.js -c examples\runner\moleculer.config.async.ts -r examples/user.service.js + */ +export default async function () { + const res = await fetch("https://pastebin.com/raw/SLZRqfHX"); + return await res.json(); +} diff --git a/examples/runner/moleculer.config.ts b/examples/runner/moleculer.config.ts new file mode 100644 index 000000000..efc91c474 --- /dev/null +++ b/examples/runner/moleculer.config.ts @@ -0,0 +1,16 @@ +/** + * Test: + * + * npx ts-node -T bin\moleculer-runner.js -c examples\runner\moleculer.config.ts -r examples/user.service.js + */ +export default { + namespace: "bbb", + logger: true, + logLevel: "debug", + //transporter: "TCP" + hotReload: true, + + created(broker) { + broker.logger.info("Typescript configuration loaded!"); + } +};