Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runner multi env load #1188

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/runner-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class MoleculerRunner {
* Available options:
-c, --config Load the configuration from a file
-e, --env Load .env file from the current directory
-E, --envfile Load a specified .env file
-E, --envfile Load specified .env files using glob patterns that are separated by commas. Example "-E '.env.dev.\*'": loads the all .env.dev.* files.
-h, --help Output usage information
-H, --hot Hot reload services if changed (disabled by default)
-i, --instances Launch [number] instances node (load balanced)
Expand All @@ -84,7 +84,10 @@ export default class MoleculerRunner {
.option(["H", "hot"], "Hot reload services if changed", false)
.option("silent", "Silent mode. No logger", false)
.option("env", "Load .env file from the current directory")
.option("envfile", "Load a specified .env file")
.option(
"envfile",
"Load specified .env files using glob patterns that are separated by commas. Example \"-E '.env.dev.*'\": loads the all .env.dev.* files."
)
.option("instances", "Launch [number] instances node (load balanced)")
.option("mask", "Filemask for service loading");

Expand Down Expand Up @@ -116,8 +119,17 @@ export default class MoleculerRunner {
try {
const dotenv = await import("dotenv");

if (this.flags.envfile) dotenv.config({ path: this.flags.envfile });
else dotenv.config();
if (this.flags.envfile) {
this.flags.envfile.split(",").forEach(envFile => {
glob.sync(path.join(process.cwd(), envFile), { absolute: true }).forEach(
envFile => {
dotenv.config({ path: envFile });
}
);
});
} else {
dotenv.config();
}
} catch (err) {
throw new Error(
"The 'dotenv' package is missing! Please install it with 'npm install dotenv --save' command."
Expand Down Expand Up @@ -261,7 +273,7 @@ export default class MoleculerRunner {
level
.split("_")
.map((value, index) => {
if (index == 0) {
if (index === 0) {
return value;
} else {
return value[0].toUpperCase() + value.substring(1);
Expand Down Expand Up @@ -391,7 +403,7 @@ export default class MoleculerRunner {
patterns
.map(s => s.trim())
.forEach(p => {
const skipping = p[0] == "!";
const skipping = p[0] === "!";
if (skipping) p = p.slice(1);

let files;
Expand All @@ -402,7 +414,7 @@ export default class MoleculerRunner {
this.watchFolders.push(svcPath);
}
files = glob.sync(svcPath + "/" + fileMask, { absolute: true });
if (files.length == 0)
if (files.length === 0)
return this.broker.logger.warn(
kleur
.yellow()
Expand All @@ -415,7 +427,7 @@ export default class MoleculerRunner {
} else {
// Load with glob
files = glob.sync(p, { cwd: svcDir, absolute: true });
if (files.length == 0)
if (files.length === 0)
this.broker.logger.warn(
kleur.yellow().bold(`There is no matched file for pattern: '${p}'`)
);
Expand Down
34 changes: 23 additions & 11 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ServiceBroker = require("./service-broker");
const utils = require("./utils");
const fs = require("fs");
const path = require("path");
const glob = require("glob").sync;
const glob = require("glob");
const _ = require("lodash");
const Args = require("args");
const os = require("os");
Expand Down Expand Up @@ -66,7 +66,7 @@ class MoleculerRunner {
* Available options:
-c, --config Load the configuration from a file
-e, --env Load .env file from the current directory
-E, --envfile Load a specified .env file
-E, --envfile Load specified .env files using glob patterns that are separated by commas. Example "-E '.env.dev.\*'": loads the all .env.dev.* files.
-h, --help Output usage information
-H, --hot Hot reload services if changed (disabled by default)
-i, --instances Launch [number] instances node (load balanced)
Expand All @@ -81,7 +81,10 @@ class MoleculerRunner {
.option(["H", "hot"], "Hot reload services if changed", false)
.option("silent", "Silent mode. No logger", false)
.option("env", "Load .env file from the current directory")
.option("envfile", "Load a specified .env file")
.option(
"envfile",
"Load specified .env files using glob patterns that are separated by commas. Example \"-E '.env.dev.*'\": loads the all .env.dev.* files."
)
.option("instances", "Launch [number] instances node (load balanced)")
.option("mask", "Filemask for service loading");

Expand Down Expand Up @@ -113,8 +116,17 @@ class MoleculerRunner {
try {
const dotenv = require("dotenv");

if (this.flags.envfile) dotenv.config({ path: this.flags.envfile });
else dotenv.config();
if (this.flags.envfile) {
this.flags.envfile.split(",").forEach(envFile => {
glob.sync(path.join(process.cwd(), envFile), { absolute: true }).forEach(
envFile => {
dotenv.config({ path: envFile });
}
);
});
} else {
dotenv.config();
}
} catch (err) {
throw new Error(
"The 'dotenv' package is missing! Please install it with 'npm install dotenv --save' command."
Expand Down Expand Up @@ -259,7 +271,7 @@ class MoleculerRunner {
level
.split("_")
.map((value, index) => {
if (index == 0) {
if (index === 0) {
return value;
} else {
return value[0].toUpperCase() + value.substring(1);
Expand Down Expand Up @@ -389,7 +401,7 @@ class MoleculerRunner {
patterns
.map(s => s.trim())
.forEach(p => {
const skipping = p[0] == "!";
const skipping = p[0] === "!";
if (skipping) p = p.slice(1);

if (p.startsWith("npm:")) {
Expand All @@ -403,8 +415,8 @@ class MoleculerRunner {
if (this.config.hotReload) {
this.watchFolders.push(svcPath);
}
files = glob(svcPath + "/" + fileMask, { absolute: true });
if (files.length == 0)
files = glob.sync(svcPath + "/" + fileMask, { absolute: true });
if (files.length === 0)
return this.broker.logger.warn(
kleur
.yellow()
Expand All @@ -418,8 +430,8 @@ class MoleculerRunner {
files = [svcPath.replace(/\\/g, "/") + ".service.js"];
} else {
// Load with glob
files = glob(p, { cwd: svcDir, absolute: true });
if (files.length == 0)
files = glob.sync(p, { cwd: svcDir, absolute: true });
if (files.length === 0)
this.broker.logger.warn(
kleur
.yellow()
Expand Down