-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
42 lines (35 loc) · 1.36 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
const welcome =
`
\x1b[3mWelcome to the \x1b[1;32mJavaScript Barnyard!
\x1b[0;34mPlease play with the animals. Enjoy the fresh air!
\x1b[0mType \x1b[1;32m.reload\x1b[0m to load changes you've made to files.
See README.md or visit https://github.com/jeremyrrose/js-barnyard for directions.
`
console.log(welcome)
const repl = require('repl')
const replServer = repl.start({
// prompt: "JS Barnyard > "
prompt: ""
})
const loadContext = (loadInstances=true) => {
console.log(`Loading REPL context...`)
Object.keys(require.cache).forEach(function(key) { delete require.cache[key] })
const classes = require('./classes.js')
for (const [k,v] of Object.entries(classes)) {
replServer.context[k] = v
}
console.log(`Available JS classes: ${Object.keys(classes).map(item => `\x1b[0;34m${item}\x1b[0m`).toString()}`)
if (loadInstances) {
const instances = require('./instances.js')
for (const [k,v] of Object.entries(instances)) {
replServer.context[k] = v
}
console.log(`Available instances: ${Object.keys(instances).map(item => `\x1b[0;34m${item}\x1b[0m`).toString()}`)
}
console.log(`\n`)
replServer.setPrompt("JS Barnyard > ")
replServer.displayPrompt()
}
loadContext()
replServer.defineCommand('reload', loadContext)
replServer.defineCommand('reloadClasses', () => loadContext(false))