jgloo 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/package.json +1 -1
  3. package/server.js +23 -7
package/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ <a name="1.7.0"></a>
2
+
3
+ ## [1.7.0](https://github.com/zosma180/jgloo/compare/1.6.1...1.7.0) (2022-07-28)
4
+
5
+ ### Features
6
+
7
+ - Added the support for the recursive folder loading (api and middlewares)
8
+
9
+ ---
10
+
1
11
  <a name="1.6.1"></a>
2
12
 
3
13
  ## [1.6.1](https://github.com/zosma180/jgloo/compare/1.6.0...1.6.1) (2022-07-22)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jgloo",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "The coldest mock server.",
5
5
  "homepage": "https://github.com/zosma180/jgloo",
6
6
  "repository": {
package/server.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const path = require('path');
2
- const { existsSync, readdirSync } = require('fs');
2
+ const { existsSync, readdirSync, lstatSync } = require('fs');
3
3
  const express = require('express');
4
4
  const cors = require('cors');
5
5
  const bodyParser = require('body-parser');
@@ -22,6 +22,22 @@ if (!existsSync(root)) {
22
22
  process.exit(2);
23
23
  }
24
24
 
25
+ // Utility to deep load files
26
+ const walk = (entry, level = 1) => {
27
+ let list = [];
28
+ const items = readdirSync(entry);
29
+
30
+ items.forEach(item => {
31
+ const subEntry = path.join(entry, item);
32
+
33
+ lstatSync(subEntry).isDirectory() && level < 10
34
+ ? list = list.concat(walk(subEntry, level + 1))
35
+ : list.push(subEntry);
36
+ });
37
+
38
+ return list;
39
+ };
40
+
25
41
  // Config
26
42
  app.use(cors());
27
43
  app.use(bodyParser.json());
@@ -29,10 +45,10 @@ app.use(multer({ dest: staticPath }).any());
29
45
 
30
46
  // Add middlewares
31
47
  if (existsSync(middlewarePath)) {
32
- readdirSync(middlewarePath)
33
- .filter((file) => file.endsWith('.js'))
34
- .forEach((file) => {
35
- const middleware = require(path.join(middlewarePath, file));
48
+ walk(middlewarePath)
49
+ .filter(file => file.endsWith('.js'))
50
+ .forEach(file => {
51
+ const middleware = require(file);
36
52
  app.use(middleware);
37
53
  });
38
54
  }
@@ -48,7 +64,7 @@ if (!existsSync(apiPath)) {
48
64
  process.exit(2);
49
65
  }
50
66
 
51
- const api = readdirSync(apiPath).filter((file) => file.endsWith('.js'));
67
+ const api = walk(apiPath).filter(file => file.endsWith('.js') );
52
68
 
53
69
  if (!api.length) {
54
70
  console.error(`No API file defined. Create one.`);
@@ -56,7 +72,7 @@ if (!api.length) {
56
72
  }
57
73
 
58
74
  api.forEach((file) => {
59
- const config = require(path.join(apiPath, file));
75
+ const config = require(file);
60
76
 
61
77
  // Add the API delay, if it is provided
62
78
  if (config.delay) {