embark/cmd/dashboard/command_history.js
Cryptomental 47dcb1552c
console, dashboard: Add persistent, automatically loaded history.
Add persistent automatically loaded history file for repl console
and Embark dashboard.

Default location of the history file is stored in DEFAULT_CMD_HISTORY_PATH
pointing to DAPP_PATH/.embark/cmd_history.

The history is automatically saved and loaded on startup.

test/console: Pass Embark object to constructor.

Update console test to pass Embark object to constructor.

Refs: https://github.com/embark-framework/embark/issues/939
2018-10-22 19:54:43 +02:00

45 lines
950 B
JavaScript

let fs = require('../../lib/core/fs');
class CommandHistory {
constructor(options = {}) {
this.cmdHistoryFile = options.cmdHistoryFile
|| process.env.DEFAULT_CMD_HISTORY_PATH;
this.history = [];
this.pointer = -1;
this.loadHistory();
}
addCommand(cmd) {
this.history.push(cmd);
this.pointer = this.history.length;
}
getPreviousCommand() {
if (this.pointer >= 0) {
this.pointer--;
}
return this.history[this.pointer];
}
getNextCommand() {
if (this.pointer >= this.history.length) {
this.pointer = this.history.length - 1;
return '';
}
this.pointer++;
return this.history[this.pointer];
}
loadHistory() {
if (fs.existsSync(this.cmdHistoryFile)) {
fs.readFileSync(this.cmdHistoryFile)
.toString()
.split('\n')
.reverse()
.forEach((cmd) => { this.addCommand(cmd); })
}
}
}
module.exports = CommandHistory;