Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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('')

Expand Down Expand Up @@ -276,13 +280,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
}

Expand All @@ -295,14 +299,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)
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/history.ts
Original file line number Diff line number Diff line change
@@ -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 !== ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a place for configuration loaded from environment variables, we should re-use that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer edits are enabled, if you want to help out.

? 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work with two processes running? It seems like one would just overwrite the other's history?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's how the node repl works. We could re-read the file in the exit listener and prepend/dedupe the repl.history, but I'd prefer someone else add that in another PR.

})
}
}