From 84e8a80ec0ce9008590bb940db2cca86483b3690 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Mon, 30 Jan 2023 11:22:29 +0700 Subject: [PATCH 1/2] feat(runner): load service by matched path --- src/runner-esm.mjs | 60 +++++++++++++++++++++++++--------------------- src/runner.js | 39 ++++++++++++++---------------- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/runner-esm.mjs b/src/runner-esm.mjs index 45ba76e11..01ec453db 100644 --- a/src/runner-esm.mjs +++ b/src/runner-esm.mjs @@ -387,6 +387,7 @@ export default class MoleculerRunner { if (patterns.length > 0) { let serviceFiles = []; + const allServiceFiles = glob(path.join(svcDir, fileMask), { absolute: true }); patterns .map(s => s.trim()) @@ -394,37 +395,42 @@ export default class MoleculerRunner { 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); + } } }); diff --git a/src/runner.js b/src/runner.js index 27513b3c5..fcdaaa176 100644 --- a/src/runner.js +++ b/src/runner.js @@ -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) @@ -385,6 +385,7 @@ class MoleculerRunner { if (patterns.length > 0) { let serviceFiles = []; + const allServiceFiles = glob(path.join(svcDir, fileMask), { absolute: true }); patterns .map(s => s.trim()) @@ -396,38 +397,34 @@ class MoleculerRunner { // 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); From 94519919aabf78c8ea847feedcfda5c5976d827c Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Mon, 30 Jan 2023 11:43:27 +0700 Subject: [PATCH 2/2] feat(runner): replace == by === --- src/runner-esm.mjs | 6 +++--- src/runner.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/runner-esm.mjs b/src/runner-esm.mjs index 01ec453db..821c37f29 100644 --- a/src/runner-esm.mjs +++ b/src/runner-esm.mjs @@ -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); @@ -392,7 +392,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); if (p.startsWith("npm:")) { @@ -422,7 +422,7 @@ export default class MoleculerRunner { files.push(...allServiceFiles.filter(file => re.test(file))); } - if (files.length == 0) { + if (files.length === 0) { this.broker.logger.warn( kleur.yellow().bold(`There is no matched file for pattern: '${p}'`) ); diff --git a/src/runner.js b/src/runner.js index fcdaaa176..fcb5b8faa 100644 --- a/src/runner.js +++ b/src/runner.js @@ -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); @@ -390,7 +390,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:")) { @@ -420,7 +420,7 @@ class MoleculerRunner { files.push(...allServiceFiles.filter(file => re.test(file))); } - if (files.length == 0) { + if (files.length === 0) { this.broker.logger.warn( kleur.yellow().bold(`There is no matched file for pattern: '${p}'`) );