jgloo 1.9.4 → 2.0.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.
@@ -1,99 +1,101 @@
1
- const { join } = require('path');
2
- const { existsSync, readdirSync, lstatSync } = require('fs');
3
- const express = require('express');
4
- const cors = require('cors');
5
- const bodyParser = require('body-parser');
6
- const multer = require('multer');
7
-
8
- const { configureResource } = require(join(__dirname, 'rest'));
9
- const { getDelayMiddleware } = require(join(__dirname, 'jgloo'));
10
- const app = express();
11
- const params = process.argv.slice(2);
12
- const root = join(process.cwd(), params[0]);
13
- const port = params[1];
14
- const staticUrl = params[2];
15
-
16
- const apiPath = `${root}/api`;
17
- const middlewarePath = `${root}/middlewares`;
18
- const staticPath = `${root}/static`;
19
-
20
- if (!existsSync(root)) {
21
- console.error(`The root folder "${root}" doesn't exist.`);
22
- process.exit(2);
23
- }
24
-
25
- if (!port) {
26
- console.error(`You must provide the port parameter.`);
27
- process.exit(2);
28
- }
29
-
30
- // Utility to deep load files
31
- const walk = (entry, level = 1) => {
32
- let list = [];
33
- const items = readdirSync(entry);
34
-
35
- items.forEach(item => {
36
- const subEntry = join(entry, item);
37
-
38
- lstatSync(subEntry).isDirectory() && level < 10
39
- ? list = list.concat(walk(subEntry, level + 1))
40
- : list.push(subEntry);
41
- });
42
-
43
- return list;
44
- };
45
-
46
- // Config
47
- app.use(cors());
48
- app.use(bodyParser.json());
49
- app.use(multer({ dest: staticPath }).any());
50
-
51
- // Add middlewares
52
- if (existsSync(middlewarePath)) {
53
- walk(middlewarePath)
54
- .filter(file => file.endsWith('js'))
55
- .forEach(file => {
56
- const middleware = require(file);
57
- app.use(middleware);
58
- });
59
- }
60
-
61
- // Add the static folder
62
- if (existsSync(staticPath)) {
63
- app.use(`/${staticUrl}`, express.static(staticPath));
64
- }
65
-
66
- // Add API
67
- if (!existsSync(apiPath)) {
68
- console.error(`No "api" folder found in the folder "${root}".`);
69
- process.exit(2);
70
- }
71
-
72
- const api = walk(apiPath)
73
- .filter(file => file.endsWith('js'))
74
- .map(file => require(file))
75
- .sort((a, b) => (b.priority || 0) - (a.priority || 0));
76
-
77
- if (!api.length) {
78
- console.error(`No API file defined. Create one.`);
79
- process.exit(2);
80
- }
81
-
82
- api.forEach(config => {
83
- // Add the API delay, if it is provided
84
- const middleware = config.delay
85
- ? getDelayMiddleware(config.delay)
86
- : (_, __, next) => next();
87
-
88
- if (config.method === 'resource') {
89
- // ReST resource
90
- configureResource(app, config);
91
- } else {
92
- // Custom endpoint
93
- app[config.method](config.path, middleware, config.callback);
94
- }
95
- });
96
-
97
- const message = `jgloo builded on the ice shelf "${root}" and the port ${port}.`;
98
- app.listen(port, () => console.log('\x1b[36m', message));
99
-
1
+ import { join } from 'path';
2
+ import { existsSync, readdirSync, lstatSync } from 'fs';
3
+ import express from 'express';
4
+ import cors from 'cors';
5
+ import bodyParser from 'body-parser';
6
+ import multer from 'multer';
7
+ import { configureResource } from './rest.mjs';
8
+ import { getDelayMiddleware } from './jgloo.mjs';
9
+
10
+ const app = express();
11
+ const params = process.argv.slice(2);
12
+ const root = join(process.cwd(), params[0]);
13
+ const port = params[1];
14
+ const staticUrl = params[2];
15
+
16
+ const apiPath = `${root}/api`;
17
+ const middlewarePath = `${root}/middlewares`;
18
+ const staticPath = `${root}/static`;
19
+
20
+ if (!existsSync(root)) {
21
+ console.error(`The root folder "${root}" doesn't exist.`);
22
+ process.exit(2);
23
+ }
24
+
25
+ if (!port) {
26
+ console.error(`You must provide the port parameter.`);
27
+ process.exit(2);
28
+ }
29
+
30
+ // Utility to deep load files
31
+ const walk = (entry, level = 1) => {
32
+ let list = [];
33
+ const items = readdirSync(entry);
34
+
35
+ items.forEach(item => {
36
+ const subEntry = join(entry, item);
37
+
38
+ lstatSync(subEntry).isDirectory() && level < 10
39
+ ? list = list.concat(walk(subEntry, level + 1))
40
+ : list.push(subEntry);
41
+ });
42
+
43
+ return list;
44
+ };
45
+
46
+ // Config
47
+ app.use(cors());
48
+ app.use(bodyParser.json());
49
+ app.use(multer({ dest: staticPath }).any());
50
+
51
+ // Add middlewares
52
+ if (existsSync(middlewarePath)) {
53
+ const middlewareQueue = walk(middlewarePath)
54
+ .filter(file => file.endsWith('js'))
55
+ .map(file => import(file));
56
+
57
+ const middlewareFiles = await Promise.all(middlewareQueue);
58
+ middlewareFiles.forEach(m => app.use(m.default));
59
+ }
60
+
61
+ // Add the static folder
62
+ if (existsSync(staticPath)) {
63
+ app.use(`/${staticUrl}`, express.static(staticPath));
64
+ }
65
+
66
+ // Add API
67
+ if (!existsSync(apiPath)) {
68
+ console.error(`No "api" folder found in the folder "${root}".`);
69
+ process.exit(2);
70
+ }
71
+
72
+ const apiQueue = walk(apiPath)
73
+ .filter(file => file.endsWith('js'))
74
+ .map(file => import(file));
75
+
76
+ const apiFiles = await Promise.all(apiQueue);
77
+ const api = apiFiles.map(f => f.default).sort((a, b) => (b.priority || 0) - (a.priority || 0));
78
+
79
+ if (!api.length) {
80
+ console.error(`No API file defined. Create one.`);
81
+ process.exit(2);
82
+ }
83
+
84
+ api.forEach(config => {
85
+ // Add the API delay, if it is provided
86
+ const middleware = config.delay
87
+ ? getDelayMiddleware(config.delay)
88
+ : (_, __, next) => next();
89
+
90
+ if (config.method === 'resource') {
91
+ // ReST resource
92
+ configureResource(app, config, middleware);
93
+ } else {
94
+ // Custom endpoint
95
+ app[config.method](config.path, middleware, config.callback);
96
+ }
97
+ });
98
+
99
+ const message = `jgloo builded on the ice shelf "${root}" and the port ${port}.`;
100
+ app.listen(port, () => console.log('\x1b[36m', message));
101
+
package/cli DELETED
@@ -1,19 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- 'use strict';
4
- const { exec } = require('child_process');
5
- const minimist = require('minimist');
6
-
7
- const params = minimist(process.argv.slice(2));
8
- const folder = params.f || params.folder || 'mock';
9
- const port = params.p || params.port || 3000;
10
- const staticUrl = params.s || params.static || 'static';
11
- const server = `${__dirname}/server.js`;
12
-
13
- const child = exec(
14
- `nodemon --watch "${folder}" --ignore "db/*.json" "${server}" "${folder}" "${port}" "${staticUrl}"`
15
- );
16
-
17
- child.stdout.on('data', (data) => console.log(String(data)));
18
- child.stderr.on('data', (data) => console.error(String(data)));
19
- child.on('close', (code) => process.exit(code));
package/jgloo.js DELETED
@@ -1,30 +0,0 @@
1
- const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs');
2
-
3
- const params = process.argv.slice(2);
4
- const db = `${params[0]}/db`;
5
-
6
- module.exports = {
7
- getResource: name => {
8
- const path = `${db}/${name}.json`;
9
- if (!existsSync(path)) { return null; }
10
- const file = readFileSync(path);
11
-
12
- try {
13
- return JSON.parse(file);
14
- } catch (error) {
15
- throw new Error(`${file}.json is an invalid JSON.`);
16
- }
17
- },
18
-
19
- setResource: (name, value) => {
20
- if (!existsSync(db)) { mkdirSync(db); }
21
- const path = `${db}/${name}.json`;
22
- writeFileSync(path, JSON.stringify(value), 'utf8');
23
- },
24
-
25
- getDelayMiddleware: (delay) => {
26
- return (_, __, next) => {
27
- setTimeout(next, delay * 1000);
28
- };
29
- },
30
- }
package/rest.js DELETED
@@ -1,95 +0,0 @@
1
- const path = require('path');
2
- const { getResource, setResource } = require(path.join(__dirname, 'jgloo'));
3
-
4
- module.exports = {
5
- configureResource: (app, config, middleware) => {
6
- const fallback = config.path.split('/').filter(Boolean).join('-');
7
- const resourceName = config.name || fallback;
8
- const skips = config.not || [];
9
-
10
- // List
11
- if (skips.includes('LIST') === false) {
12
- app.get(config.path, middleware, (req, res) => {
13
- const resource = getResource(resourceName) || [];
14
- res.json(resource);
15
- });
16
- }
17
-
18
- // Read
19
- if (skips.includes('READ') === false) {
20
- app.get(`${config.path}/:id`, middleware, (req, res) => {
21
- const id = Number(req.params.id);
22
-
23
- const resource = getResource(resourceName) || [];
24
- const model = resource.find(r => r.id === id);
25
- if (!model) { return res.sendStatus(404); }
26
-
27
- res.json(model);
28
- });
29
- }
30
-
31
- // Create
32
- if (skips.includes('CREATE') === false) {
33
- app.post(config.path, middleware, (req, res) => {
34
- const resource = getResource(resourceName) || [];
35
- const model = req.body;
36
-
37
- model.id = Date.now();
38
- resource.push(model);
39
- setResource(resourceName, resource);
40
-
41
- res.json(model);
42
- });
43
- }
44
-
45
- // Update
46
- if (skips.includes('UPDATE') === false) {
47
- app.put(`${config.path}/:id`, middleware, (req, res) => {
48
- const id = Number(req.params.id);
49
-
50
- const resource = getResource(resourceName) || [];
51
- const index = resource.findIndex(r => r.id === id);
52
- if (index === -1) { return res.sendStatus(404); }
53
-
54
- resource[index] = req.body;
55
- resource[index].id = id;
56
- setResource(resourceName, resource);
57
-
58
- res.json(resource[index]);
59
- });
60
- }
61
-
62
- // Patch
63
- if (skips.includes('PATCH') === false) {
64
- app.patch(`${config.path}/:id`, middleware, (req, res) => {
65
- const id = Number(req.params.id);
66
-
67
- const resource = getResource(resourceName) || [];
68
- const index = resource.findIndex(r => r.id === id);
69
- if (index === -1) { return res.sendStatus(404); }
70
-
71
- resource[index] = { ...resource[index], ...req.body };
72
- resource[index].id = id;
73
- setResource(resourceName, resource);
74
-
75
- res.json(resource[index]);
76
- });
77
- }
78
-
79
- // Delete
80
- if (skips.includes('DELETE') === false) {
81
- app.delete(`${config.path}/:id`, middleware, (req, res) => {
82
- const id = Number(req.params.id);
83
-
84
- let resource = getResource(resourceName) || [];
85
- const index = resource.findIndex(r => r.id === id);
86
- if (index === -1) { return res.sendStatus(404); }
87
-
88
- resource = resource.filter(r => r.id !== id);
89
- setResource(resourceName, resource);
90
-
91
- res.json(id);
92
- });
93
- }
94
- }
95
- }