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

feat(runner): load service by matched path #1181

Open
wants to merge 2 commits into
base: master
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
64 changes: 35 additions & 29 deletions src/runner-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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 @@ -387,44 +387,50 @@ export default class MoleculerRunner {

if (patterns.length > 0) {
let serviceFiles = [];
const allServiceFiles = glob(path.join(svcDir, fileMask), { absolute: true });

patterns
.map(s => s.trim())
.forEach(p => {
const skipping = p[0] == "!";
const skipping = p[0] === "!";
if (skipping) p = p.slice(1);

let files;
const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
// Check is it a directory?
if (this.isDirectory(svcPath)) {
if (this.config.hotReload) {
this.watchFolders.push(svcPath);
}
files = glob.sync(svcPath + "/" + fileMask, { absolute: true });
if (files.length == 0)
return this.broker.logger.warn(
kleur
.yellow()
.bold(`There is no service files in directory: '${svcPath}'`)
);
} else if (this.isServiceFile(svcPath)) {
files = [svcPath.replace(/\\/g, "/")];
} else if (this.isServiceFile(svcPath + ".service.js")) {
files = [svcPath.replace(/\\/g, "/") + ".service.js"];
if (p.startsWith("npm:")) {
// Load NPM module
this.loadNpmModule(p.slice(4));
} else {
// Load with glob
files = glob.sync(p, { cwd: svcDir, absolute: true });
if (files.length == 0)
const files = [];
const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
// Check is it a directory?
if (this.isDirectory(svcPath)) {
if (this.config.hotReload) {
this.watchFolders.push(svcPath);
}
files.push(...glob(svcPath + "/" + fileMask, { absolute: true }));
} else if (this.isServiceFile(svcPath)) {
files.push(svcPath.replace(/\\/g, "/"));
} else if (this.isServiceFile(svcPath + ".service.js")) {
files.push(svcPath.replace(/\\/g, "/") + ".service.js");
} else {
// Load with glob
files.push(...glob(p, { cwd: svcDir, absolute: true }));
}

if (files.length === 0) {
// eslint-disable-next-line security/detect-non-literal-regexp
const re = new RegExp(`${_.escapeRegExp(`${p}.service.`)}[tj]s$`);
files.push(...allServiceFiles.filter(file => re.test(file)));
}

if (files.length === 0) {
this.broker.logger.warn(
kleur.yellow().bold(`There is no matched file for pattern: '${p}'`)
);
}

if (files && files.length > 0) {
if (skipping)
serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
else serviceFiles.push(...files);
} else {
if (skipping)
serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
else serviceFiles.push(...files);
}
}
});

Expand Down
43 changes: 20 additions & 23 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MoleculerRunner {
-r, --repl Start REPL mode (disabled by default)
-s, --silent Silent mode. No logger (disabled by default)
-v, --version Output the version number
*/
*/
processFlags(procArgs) {
Args.option("config", "Load the configuration from a file")
.option("repl", "Start REPL mode", false)
Expand Down Expand Up @@ -259,7 +259,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 @@ -385,49 +385,46 @@ class MoleculerRunner {

if (patterns.length > 0) {
let serviceFiles = [];
const allServiceFiles = glob(path.join(svcDir, fileMask), { absolute: true });

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:")) {
// Load NPM module
this.loadNpmModule(p.slice(4));
} else {
let files;
const files = [];
const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
// Check is it a directory?
if (this.isDirectory(svcPath)) {
if (this.config.hotReload) {
this.watchFolders.push(svcPath);
}
files = glob(svcPath + "/" + fileMask, { absolute: true });
if (files.length == 0)
return this.broker.logger.warn(
kleur
.yellow()
.bold(
`There is no service files in directory: '${svcPath}'`
)
);
files.push(...glob(svcPath + "/" + fileMask, { absolute: true }));
} else if (this.isServiceFile(svcPath)) {
files = [svcPath.replace(/\\/g, "/")];
files.push(svcPath.replace(/\\/g, "/"));
} else if (this.isServiceFile(svcPath + ".service.js")) {
files = [svcPath.replace(/\\/g, "/") + ".service.js"];
files.push(svcPath.replace(/\\/g, "/") + ".service.js");
} else {
// Load with glob
files = glob(p, { cwd: svcDir, absolute: true });
if (files.length == 0)
this.broker.logger.warn(
kleur
.yellow()
.bold(`There is no matched file for pattern: '${p}'`)
);
files.push(...glob(p, { cwd: svcDir, absolute: true }));
}

if (files.length === 0) {
// eslint-disable-next-line security/detect-non-literal-regexp
const re = new RegExp(`${_.escapeRegExp(`${p}.service.`)}[tj]s$`);
files.push(...allServiceFiles.filter(file => re.test(file)));
}

if (files && files.length > 0) {
if (files.length === 0) {
this.broker.logger.warn(
kleur.yellow().bold(`There is no matched file for pattern: '${p}'`)
);
} else {
if (skipping)
serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
else serviceFiles.push(...files);
Expand Down