Skip to content

Commit

Permalink
Use service creation pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcManiez committed Sep 4, 2020
1 parent ada5afd commit 9ab7eca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { homedir } from 'os'
import {
_eval,
appendEval,
createReplEval,
createReplService,
EVAL_FILENAME,
EvalState,
exec,
Expand Down Expand Up @@ -319,7 +319,7 @@ function startRepl (service: Register, state: EvalState, code?: string) {
output: process.stdout,
// Mimicking node's REPL implementation: https://github.com/nodejs/node/blob/168b22ba073ee1cbf8d0bcb4ded7ff3099335d04/lib/internal/repl.js#L28-L30
terminal: process.stdout.isTTY && !parseInt(process.env.NODE_NO_READLINE!, 10),
eval: createReplEval(service, state),
eval: createReplService(service, state).eval,
useGlobal: true
})

Expand Down
52 changes: 27 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,39 +1001,41 @@ export class EvalState {
*
* @returns an evaluator for the node REPL
*/
export function createReplEval (
export function createReplService (
service: Register,
state: EvalState = new EvalState(join(process.cwd(), EVAL_FILENAME))
) {
/**
* Eval code from the REPL.
*/
return function (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any) {
let err: Error | null = null
let result: any

// TODO: Figure out how to handle completion here.
if (code === '.scope') {
callback(err)
return
}
return {
/**
* Eval code from the REPL.
*/
eval: function (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any) {
let err: Error | null = null
let result: any

// TODO: Figure out how to handle completion here.
if (code === '.scope') {
callback(err)
return
}

try {
result = _eval(service, state, code)
} catch (error) {
if (error instanceof TSError) {
// Support recoverable compilations using >= node 6.
if (Recoverable && isRecoverable(error)) {
err = new Recoverable(error)
try {
result = _eval(service, state, code)
} catch (error) {
if (error instanceof TSError) {
// Support recoverable compilations using >= node 6.
if (Recoverable && isRecoverable(error)) {
err = new Recoverable(error)
} else {
console.error(error)
}
} else {
console.error(error)
err = error
}
} else {
err = error
}
}

return callback(err, result)
return callback(err, result)
}
}
}

Expand Down

0 comments on commit 9ab7eca

Please sign in to comment.