Skip to content

Commit

Permalink
Merge pull request #1284 from shawnmcknight/allow-default-export-conf…
Browse files Browse the repository at this point in the history
…iguration-function

Fix runner use of moleculer.config function with default export
  • Loading branch information
icebob committed May 20, 2024
2 parents 2bfa49a + ca7cb2e commit ab3372d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 44 deletions.
11 changes: 11 additions & 0 deletions examples/runner/moleculer.config.async.ts
Original file line number Diff line number Diff line change
@@ -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();
}
16 changes: 16 additions & 0 deletions examples/runner/moleculer.config.ts
Original file line number Diff line number Diff line change
@@ -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!");
}
};
40 changes: 20 additions & 20 deletions src/runner-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`));
}
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
48 changes: 24 additions & 24 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class MoleculerRunner {
this.watchFolders = [];

this.flags = null;
this.configFile = null;
this.config = null;
this.servicePaths = null;
this.broker = null;
Expand Down Expand Up @@ -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}`));
}
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ab3372d

Please sign in to comment.