From 127cb9deb49b014b865a9dd21e59c9f3944fb718 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Mon, 21 Jan 2019 12:48:38 -0500 Subject: [PATCH 1/2] fix: invalid "eval" function type --- src/bin.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 9ca3dd656..4f421ab45 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -276,13 +276,13 @@ function startRepl () { /** * Eval code from the REPL. */ -function replEval (code: string, _context: any, _filename: string, callback: (err?: Error, result?: any) => any) { +function replEval (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any) { let err: Error | undefined let result: any // TODO: Figure out how to handle completion here. if (code === '.scope') { - callback() + callback(null) return } @@ -295,14 +295,13 @@ function replEval (code: string, _context: any, _filename: string, callback: (er err = new Recoverable(error) } else { console.error(error.diagnosticText) - err = undefined } } else { err = error } } - callback(err, result) + callback(err || null, result) } /** From c575d0453d43e094bae873b3c09ebe2be6c92815 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Mon, 21 Jan 2019 14:14:44 -0500 Subject: [PATCH 2/2] feat: command history Try upgrading to the latest Node 11 if you experience any issues. Closes #259 --- src/bin.ts | 4 ++++ src/history.ts | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/history.ts diff --git a/src/bin.ts b/src/bin.ts index 4f421ab45..bee1ae9ad 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -10,6 +10,7 @@ import { diffLines } from 'diff' import { Script } from 'vm' import { readFileSync, statSync } from 'fs' import { register, VERSION, DEFAULTS, TSError, parse } from './index' +import { useHistory } from './history' interface Argv { // Node.js-like options. @@ -241,6 +242,9 @@ function startRepl () { useGlobal: true }) + // Initialize the command history. + useHistory(repl) + // Bookmark the point where we should reset the REPL state. const resetEval = appendEval('') diff --git a/src/history.ts b/src/history.ts new file mode 100644 index 000000000..fd23b23dd --- /dev/null +++ b/src/history.ts @@ -0,0 +1,20 @@ +import { join } from 'path' +import * as os from 'os' +import * as fs from 'fs' + +// TODO: Use `repl: REPLServer` when history-related properties are added to @types/node +export function useHistory (repl: any) { + repl.historySize = process.env.TS_NODE_HISTORY !== '' + ? Number(process.env.TS_NODE_HISTORY_SIZE || 1000) + : 0 + + if (repl.historySize > 0) { + const file = process.env.TS_NODE_HISTORY || join(os.homedir(), '.ts_node_history') + if (fs.existsSync(file)) { + repl.history = fs.readFileSync(file, 'utf8').split(os.EOL) + } + repl.on('exit', () => { + fs.writeFileSync(file, repl.history.join(os.EOL)) + }) + } +}