-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
68 lines (56 loc) · 1.61 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
const repl = require('./repl');
const DEFAULT_REPL_CONFIG = {
quiet: false,
prompt: 'loopback > ',
useGlobal: true,
ignoreUndefined: true,
historyPath: process.env.LOOPBACK_CONSOLE_HISTORY,
};
const DEFAULT_HANDLE_INFO = {
app: 'The Loopback app handle',
cb: 'A simplistic results callback that stores and prints',
result: 'The handle on which cb() stores results',
};
const LoopbackConsole = module.exports = {
activated() {
return process.env.LOOPBACK_CONSOLE == 'true' ||
process.env.LOOPBACK_CONSOLE == 1 ||
process.argv.includes('--console');
},
start(app, config) {
if (this._started) {
return Promise.resolve(this._ctx);
}
this._started = true;
config = Object.assign({}, DEFAULT_REPL_CONFIG, config);
const ctx = this._ctx = {
app,
config,
handles: config.handles || {},
handleInfo: config.handleInfo || {},
models: {},
};
Object.keys(app.models).forEach(modelName => {
if (!(modelName in ctx.handles)) {
ctx.models[modelName] = ctx.handles[modelName] = app.models[modelName];
}
});
if (!('app' in ctx.handles)) {
ctx.handles.app = app;
ctx.handleInfo.app = DEFAULT_HANDLE_INFO.app;
}
if (ctx.handles.cb === true || !('cb' in ctx.handles)) {
ctx.handles.cb = true;
ctx.handleInfo.cb = DEFAULT_HANDLE_INFO.cb;
ctx.handleInfo.result = DEFAULT_HANDLE_INFO.result;
}
if (!config.quiet) {
console.log(repl.usage(ctx));
}
return repl.start(ctx).then(repl => {
ctx.repl = repl;
return ctx;
});
},
};