-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow source file to be passed to repl (#396)
- Loading branch information
1 parent
c756088
commit 30543cb
Showing
2 changed files
with
43 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,48 @@ | ||
import fs = require('fs') | ||
import repl = require('repl') // 'repl' here refers to the module named 'repl' in index.d.ts | ||
import { createContext, parseError, runInContext } from '../index' | ||
import { createContext, IOptions, parseError, runInContext } from '../index' | ||
|
||
function startRepl(chapter = 1, useSubst: boolean) { | ||
function startRepl(chapter = 1, useSubst: boolean, prelude = '') { | ||
// use defaults for everything | ||
const context = createContext(chapter) | ||
repl.start( | ||
// the object being passed as argument fits the interface ReplOptions in the repl module. | ||
{ | ||
eval: (cmd, unusedContext, unusedFilename, callback) => { | ||
runInContext(cmd, context, { scheduler: 'preemptive', useSubst }).then(obj => { | ||
if (obj.status === 'finished') { | ||
callback(null, obj.value) | ||
} else { | ||
callback(new Error(parseError(context.errors)), undefined) | ||
const options: Partial<IOptions> = { scheduler: 'preemptive', useSubst } | ||
runInContext(prelude, context, options).then(preludeResult => { | ||
if (preludeResult.status === 'finished') { | ||
console.log(preludeResult.value) | ||
repl.start( | ||
// the object being passed as argument fits the interface ReplOptions in the repl module. | ||
{ | ||
eval: (cmd, unusedContext, unusedFilename, callback) => { | ||
runInContext(cmd, context, options).then(obj => { | ||
if (obj.status === 'finished') { | ||
callback(null, obj.value) | ||
} else { | ||
callback(new Error(parseError(context.errors)), undefined) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
) | ||
} else { | ||
throw new Error(parseError(context.errors)) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
function main() { | ||
const chapter = process.argv.length > 2 ? parseInt(process.argv[2], 10) : 1 | ||
const useSubst = process.argv.length > 3 ? process.argv[3] === 'subst' : false | ||
startRepl(chapter, useSubst) | ||
const firstArg = process.argv[2] | ||
if (process.argv.length === 3 && String(Number(firstArg)) !== firstArg.trim()) { | ||
fs.readFile(firstArg, 'utf8', (err, data) => { | ||
if (err) { | ||
throw err | ||
} | ||
startRepl(4, false, data) | ||
}) | ||
} else { | ||
const chapter = process.argv.length > 2 ? parseInt(firstArg, 10) : 1 | ||
const useSubst = process.argv.length > 3 ? process.argv[3] === 'subst' : false | ||
startRepl(chapter, useSubst) | ||
} | ||
} | ||
|
||
main() |