-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.js
More file actions
38 lines (35 loc) · 1.19 KB
/
console.js
File metadata and controls
38 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* eslint-disable global-require */
const repl = require('repl');
const fs = require('fs');
const pjson = require('./package.json');
const models = require('./app/models');
const convertFunctionToAsync = f => async (...args) => {
const result = await f(...args);
console.log(JSON.stringify(result, null, 2));
return result;
};
const convertObjectFunctionsToAsync = serviceMethods => {
const asyncServiceMethods = {};
Object.keys(serviceMethods).forEach(key => {
if (typeof serviceMethods[key] === 'function') {
asyncServiceMethods[key] = convertFunctionToAsync(serviceMethods[key]);
} else {
asyncServiceMethods[key] = serviceMethods[key];
}
});
return asyncServiceMethods;
};
Promise.resolve().then(() => {
const replServer = repl.start({
prompt: `${pjson.name}> `
});
replServer.context.models = models;
const servicesPath = './app/services/';
fs.readdir(servicesPath, (err, files) => {
files.forEach(file => {
const serviceMethods = require(`${servicesPath}${file}`);
const asyncServiceMethods = convertObjectFunctionsToAsync(serviceMethods);
replServer.context[`${file.split('.')[0]}Service`] = asyncServiceMethods;
});
});
});