-
Notifications
You must be signed in to change notification settings - Fork 0
/
.node-repl.js
65 lines (50 loc) · 1.25 KB
/
.node-repl.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
#!/usr/bin/node
const os = require('os');
const path = require('path');
const repl = require('repl');
const CONTEXT_FILENAME = '.node-context.js';
const CONTEXT_PATH = path.join(os.homedir(), CONTEXT_FILENAME);
const EMPTY = Symbol('EMPTY');
const request = moduleName => {
try {
return require(moduleName);
} catch(err) {
return err;
}
};
const _repl = repl.start({
preview: false,
useGlobal: true
});
const replEval = cmd => _repl.eval(cmd, null, '', Function.prototype);
// replEval("require('@std/esm');");
_repl.setupHistory(process.env.NODE_REPL_HISTORY, () => {
Object.assign(_repl.context, {
_repl,
env: process.env
});
try {
const context = require(CONTEXT_PATH);
Object.keys(context).forEach(key => {
let cache = EMPTY;
Object.defineProperty(_repl.context, key, {
get() {
let value = context[key];
if (typeof value === 'string') {
return request(value);
}
if (typeof value === 'function') {
if (cache === EMPTY) {
cache = value();
}
return cache;
}
return null;
},
enumerable: true
});
});
} catch(err) {
// could not load local context
}
});