-
Notifications
You must be signed in to change notification settings - Fork 9
/
console.js
37 lines (34 loc) · 1.17 KB
/
console.js
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
const repl = require('repl'),
fs = require('fs'),
models = require('./app/models'),
pjson = require('./package.json');
const convertFunctionToAsync = f => async (...args) => {
const result = await f(...args);
console.log(JSON.stringify(result, null, 2)); // eslint-disable-line no-console
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;
});
});
});