Created
June 27, 2018 15:29
-
-
Save allain/d553deee5038e09935ad51c87af23cc1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const vm = require('vm') | |
| const repl = require('repl') | |
| const globals = { | |
| Date, | |
| JSON | |
| } | |
| const state = {} | |
| const environment = new Proxy(state, { | |
| get (target, key) { | |
| if (key === 'context') return state | |
| return globals[key] || target[key] | |
| }, | |
| set (target, key, value) { | |
| target[key] = value | |
| } | |
| }) | |
| const ctx = vm.createContext(environment) | |
| repl.start({ | |
| prompt: '> ', | |
| eval: (command, context, filename, cb) => | |
| Promise.resolve(vm.runInContext(command, ctx)).then(result => cb(null, result)) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment