v1.1.0
translated to typescript and added .ts file support
This commit is contained in:
52
src/repl.ts
Normal file
52
src/repl.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import * as repl from 'repl'
|
||||
import { Context, createContext, runInContext } from 'vm'
|
||||
import { readInput, SupportedLoader } from './build'
|
||||
import { logv } from './util'
|
||||
|
||||
export function runRepl(
|
||||
context: Context,
|
||||
loader: SupportedLoader,
|
||||
exported?: string[]
|
||||
) {
|
||||
if (exported) {
|
||||
console.log(`Exports: ${exported.join(', ')}`)
|
||||
}
|
||||
console.log(`REPL Loader: ${loader.toUpperCase()}`)
|
||||
|
||||
const r = repl
|
||||
.start({
|
||||
useGlobal: true,
|
||||
eval: (cmd, context, _filename, callback) => {
|
||||
//add async wrapper
|
||||
// context.__FILE_NODE__INTERNAL__ = callback
|
||||
// cmd = `(async () => {
|
||||
// ${cmd}
|
||||
|
||||
// })()`
|
||||
|
||||
let js = readInput(cmd, loader)
|
||||
logv(`Transformed: ${js}`)
|
||||
try {
|
||||
let val = runInContext(js, context)
|
||||
callback(null, val)
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
callback(e, null)
|
||||
} else {
|
||||
callback(new Error((e as any).toString()), null)
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
.on('close', () => process.exit(0))
|
||||
|
||||
Object.keys(context).forEach(key =>
|
||||
Object.defineProperty(r.context, key, {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
value: context[key],
|
||||
})
|
||||
)
|
||||
|
||||
createContext(r.context)
|
||||
}
|
||||
Reference in New Issue
Block a user