Skip to content

Commit 467a13c

Browse files
committed
feat: patch moleculer
allow to load multi-service dir
1 parent 215cb9a commit 467a13c

File tree

3 files changed

+353
-59
lines changed

3 files changed

+353
-59
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"mongoose-findorcreate": "3.0.0"
7171
},
7272
"patchedDependencies": {
73-
73+
74+
7475
}
7576
}
7677
}

patches/[email protected]

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
diff --git a/src/runner-esm.mjs b/src/runner-esm.mjs
2+
index 97b78b59c8931ed2c71e719c12d23bd5172da977..b14e9e0c61be8b0460e60f448781103c49f563e5 100644
3+
--- a/src/runner-esm.mjs
4+
+++ b/src/runner-esm.mjs
5+
@@ -332,83 +332,87 @@ export default class MoleculerRunner {
6+
this.watchFolders.length = 0;
7+
const fileMask = this.flags.mask || "**/*.service.js";
8+
9+
- const serviceDir = process.env.SERVICEDIR || "";
10+
- const svcDir = path.isAbsolute(serviceDir)
11+
- ? serviceDir
12+
- : path.resolve(process.cwd(), serviceDir);
13+
+ const serviceDirList = (process.env.SERVICEDIR || "").split(',');
14+
15+
- let patterns = this.servicePaths;
16+
+ for (const serviceDir of serviceDirList) {
17+
+ const serviceDir = process.env.SERVICEDIR || "";
18+
+ const svcDir = path.isAbsolute(serviceDir)
19+
+ ? serviceDir
20+
+ : path.resolve(process.cwd(), serviceDir);
21+
22+
- if (process.env.SERVICES || process.env.SERVICEDIR) {
23+
- if (this.isDirectory(svcDir) && !process.env.SERVICES) {
24+
- // Load all services from directory (from subfolders too)
25+
- this.broker.loadServices(svcDir, fileMask);
26+
+ let patterns = this.servicePaths;
27+
28+
- if (this.config.hotReload) {
29+
- this.watchFolders.push(svcDir);
30+
+ if (process.env.SERVICES || process.env.SERVICEDIR) {
31+
+ if (this.isDirectory(svcDir) && !process.env.SERVICES) {
32+
+ // Load all services from directory (from subfolders too)
33+
+ this.broker.loadServices(svcDir, fileMask);
34+
+
35+
+ if (this.config.hotReload) {
36+
+ this.watchFolders.push(svcDir);
37+
+ }
38+
+ } else if (process.env.SERVICES) {
39+
+ // Load services from env list
40+
+ patterns = Array.isArray(process.env.SERVICES)
41+
+ ? process.env.SERVICES
42+
+ : process.env.SERVICES.split(",");
43+
}
44+
- } else if (process.env.SERVICES) {
45+
- // Load services from env list
46+
- patterns = Array.isArray(process.env.SERVICES)
47+
- ? process.env.SERVICES
48+
- : process.env.SERVICES.split(",");
49+
}
50+
- }
51+
52+
- if (patterns.length > 0) {
53+
- let serviceFiles = [];
54+
-
55+
- patterns
56+
- .map(s => s.trim())
57+
- .forEach(p => {
58+
- const skipping = p[0] == "!";
59+
- if (skipping) p = p.slice(1);
60+
-
61+
- let files;
62+
- const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
63+
- // Check is it a directory?
64+
- if (this.isDirectory(svcPath)) {
65+
- if (this.config.hotReload) {
66+
- this.watchFolders.push(svcPath);
67+
+ if (patterns.length > 0) {
68+
+ let serviceFiles = [];
69+
+
70+
+ patterns
71+
+ .map(s => s.trim())
72+
+ .forEach(p => {
73+
+ const skipping = p[0] == "!";
74+
+ if (skipping) p = p.slice(1);
75+
+
76+
+ let files;
77+
+ const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
78+
+ // Check is it a directory?
79+
+ if (this.isDirectory(svcPath)) {
80+
+ if (this.config.hotReload) {
81+
+ this.watchFolders.push(svcPath);
82+
+ }
83+
+ files = glob.sync(svcPath + "/" + fileMask, { absolute: true });
84+
+ if (files.length == 0)
85+
+ return this.broker.logger.warn(
86+
+ kleur
87+
+ .yellow()
88+
+ .bold(
89+
+ `There is no service files in directory: '${svcPath}'`
90+
+ )
91+
+ );
92+
+ } else if (this.isServiceFile(svcPath)) {
93+
+ files = [svcPath.replace(/\\/g, "/")];
94+
+ } else if (this.isServiceFile(svcPath + ".service.js")) {
95+
+ files = [svcPath.replace(/\\/g, "/") + ".service.js"];
96+
+ } else {
97+
+ // Load with glob
98+
+ files = glob.sync(p, { cwd: svcDir, absolute: true });
99+
+ if (files.length == 0)
100+
+ this.broker.logger.warn(
101+
+ kleur
102+
+ .yellow()
103+
+ .bold(`There is no matched file for pattern: '${p}'`)
104+
+ );
105+
}
106+
- files = glob.sync(svcPath + "/" + fileMask, { absolute: true });
107+
- if (files.length == 0)
108+
- return this.broker.logger.warn(
109+
- kleur
110+
- .yellow()
111+
- .bold(
112+
- `There is no service files in directory: '${svcPath}'`
113+
- )
114+
- );
115+
- } else if (this.isServiceFile(svcPath)) {
116+
- files = [svcPath.replace(/\\/g, "/")];
117+
- } else if (this.isServiceFile(svcPath + ".service.js")) {
118+
- files = [svcPath.replace(/\\/g, "/") + ".service.js"];
119+
- } else {
120+
- // Load with glob
121+
- files = glob.sync(p, { cwd: svcDir, absolute: true });
122+
- if (files.length == 0)
123+
- this.broker.logger.warn(
124+
- kleur
125+
- .yellow()
126+
- .bold(`There is no matched file for pattern: '${p}'`)
127+
- );
128+
- }
129+
130+
- if (files && files.length > 0) {
131+
- if (skipping)
132+
- serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
133+
- else serviceFiles.push(...files);
134+
- }
135+
- });
136+
+ if (files && files.length > 0) {
137+
+ if (skipping)
138+
+ serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
139+
+ else serviceFiles.push(...files);
140+
+ }
141+
+ });
142+
143+
- await Promise.all(_.uniq(serviceFiles).map(async f => {
144+
- const mod = await import(f.startsWith("/") ? f : "/" + f);
145+
- const content = mod.default;
146+
+ await Promise.all(_.uniq(serviceFiles).map(async f => {
147+
+ const mod = await import(f.startsWith("/") ? f : "/" + f);
148+
+ const content = mod.default;
149+
150+
- const svc = this.broker.createService(content);
151+
- svc.__filename = f;
152+
- }));
153+
+ const svc = this.broker.createService(content);
154+
+ svc.__filename = f;
155+
+ }));
156+
+ }
157+
}
158+
}
159+
160+
diff --git a/src/runner.js b/src/runner.js
161+
index 695e5affcb21b456ce5b0f69f2c02e776ad4216a..34cc885fd80279b1b91f8013263c522ff6f1d42b 100644
162+
--- a/src/runner.js
163+
+++ b/src/runner.js
164+
@@ -335,83 +335,87 @@ class MoleculerRunner {
165+
this.watchFolders.length = 0;
166+
const fileMask = this.flags.mask || "**/*.service.js";
167+
168+
- const serviceDir = process.env.SERVICEDIR || "";
169+
- const svcDir = path.isAbsolute(serviceDir)
170+
- ? serviceDir
171+
- : path.resolve(process.cwd(), serviceDir);
172+
+ const serviceDirList = (process.env.SERVICEDIR || "").split(',');
173+
174+
- let patterns = this.servicePaths;
175+
+ for (const serviceDir of serviceDirList) {
176+
+ const svcDir = path.isAbsolute(serviceDir)
177+
+ ? serviceDir
178+
+ : path.resolve(process.cwd(), serviceDir);
179+
180+
- if (process.env.SERVICES || process.env.SERVICEDIR) {
181+
- if (this.isDirectory(svcDir) && !process.env.SERVICES) {
182+
- // Load all services from directory (from subfolders too)
183+
- this.broker.loadServices(svcDir, fileMask);
184+
+ let patterns = this.servicePaths;
185+
186+
- if (this.config.hotReload) {
187+
- this.watchFolders.push(svcDir);
188+
+ if (process.env.SERVICES || process.env.SERVICEDIR) {
189+
+ if (this.isDirectory(svcDir) && !process.env.SERVICES) {
190+
+ // Load all services from directory (from subfolders too)
191+
+ this.broker.loadServices(svcDir, fileMask);
192+
+
193+
+ if (this.config.hotReload) {
194+
+ this.watchFolders.push(svcDir);
195+
+ }
196+
+ } else if (process.env.SERVICES) {
197+
+ // Load services from env list
198+
+ patterns = Array.isArray(process.env.SERVICES)
199+
+ ? process.env.SERVICES
200+
+ : process.env.SERVICES.split(",");
201+
}
202+
- } else if (process.env.SERVICES) {
203+
- // Load services from env list
204+
- patterns = Array.isArray(process.env.SERVICES)
205+
- ? process.env.SERVICES
206+
- : process.env.SERVICES.split(",");
207+
}
208+
- }
209+
210+
- if (patterns.length > 0) {
211+
- let serviceFiles = [];
212+
-
213+
- patterns
214+
- .map(s => s.trim())
215+
- .forEach(p => {
216+
- const skipping = p[0] == "!";
217+
- if (skipping) p = p.slice(1);
218+
-
219+
- if (p.startsWith("npm:")) {
220+
- // Load NPM module
221+
- this.loadNpmModule(p.slice(4));
222+
- } else {
223+
- let files;
224+
- const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
225+
- // Check is it a directory?
226+
- if (this.isDirectory(svcPath)) {
227+
- if (this.config.hotReload) {
228+
- this.watchFolders.push(svcPath);
229+
- }
230+
- files = glob(svcPath + "/" + fileMask, { absolute: true });
231+
- if (files.length == 0)
232+
- return this.broker.logger.warn(
233+
- kleur
234+
- .yellow()
235+
- .bold(
236+
- `There is no service files in directory: '${svcPath}'`
237+
- )
238+
- );
239+
- } else if (this.isServiceFile(svcPath)) {
240+
- files = [svcPath.replace(/\\/g, "/")];
241+
- } else if (this.isServiceFile(svcPath + ".service.js")) {
242+
- files = [svcPath.replace(/\\/g, "/") + ".service.js"];
243+
+ if (patterns.length > 0) {
244+
+ let serviceFiles = [];
245+
+
246+
+ patterns
247+
+ .map(s => s.trim())
248+
+ .forEach(p => {
249+
+ const skipping = p[0] == "!";
250+
+ if (skipping) p = p.slice(1);
251+
+
252+
+ if (p.startsWith("npm:")) {
253+
+ // Load NPM module
254+
+ this.loadNpmModule(p.slice(4));
255+
} else {
256+
- // Load with glob
257+
- files = glob(p, { cwd: svcDir, absolute: true });
258+
- if (files.length == 0)
259+
- this.broker.logger.warn(
260+
- kleur
261+
- .yellow()
262+
- .bold(`There is no matched file for pattern: '${p}'`)
263+
- );
264+
- }
265+
+ let files;
266+
+ const svcPath = path.isAbsolute(p) ? p : path.resolve(svcDir, p);
267+
+ // Check is it a directory?
268+
+ if (this.isDirectory(svcPath)) {
269+
+ if (this.config.hotReload) {
270+
+ this.watchFolders.push(svcPath);
271+
+ }
272+
+ files = glob(svcPath + "/" + fileMask, { absolute: true });
273+
+ if (files.length == 0)
274+
+ return this.broker.logger.warn(
275+
+ kleur
276+
+ .yellow()
277+
+ .bold(
278+
+ `There is no service files in directory: '${svcPath}'`
279+
+ )
280+
+ );
281+
+ } else if (this.isServiceFile(svcPath)) {
282+
+ files = [svcPath.replace(/\\/g, "/")];
283+
+ } else if (this.isServiceFile(svcPath + ".service.js")) {
284+
+ files = [svcPath.replace(/\\/g, "/") + ".service.js"];
285+
+ } else {
286+
+ // Load with glob
287+
+ files = glob(p, { cwd: svcDir, absolute: true });
288+
+ if (files.length == 0)
289+
+ this.broker.logger.warn(
290+
+ kleur
291+
+ .yellow()
292+
+ .bold(`There is no matched file for pattern: '${p}'`)
293+
+ );
294+
+ }
295+
296+
- if (files && files.length > 0) {
297+
- if (skipping)
298+
- serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
299+
- else serviceFiles.push(...files);
300+
+ if (files && files.length > 0) {
301+
+ if (skipping)
302+
+ serviceFiles = serviceFiles.filter(f => files.indexOf(f) === -1);
303+
+ else serviceFiles.push(...files);
304+
+ }
305+
}
306+
- }
307+
- });
308+
+ });
309+
310+
- _.uniq(serviceFiles).forEach(f => this.broker.loadService(f));
311+
+ _.uniq(serviceFiles).forEach(f => this.broker.loadService(f));
312+
+ }
313+
}
314+
+
315+
}
316+
317+
/**

0 commit comments

Comments
 (0)