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
This commit is contained in:
parent
5fe746df51
commit
47dcb1552c
|
@ -36,6 +36,8 @@ process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules')
|
||||||
(process.env.NODE_PATH || '');
|
(process.env.NODE_PATH || '');
|
||||||
|
|
||||||
process.env.DEFAULT_DIAGRAM_PATH = utils.joinPath(process.env.DAPP_PATH, 'diagram.svg');
|
process.env.DEFAULT_DIAGRAM_PATH = utils.joinPath(process.env.DAPP_PATH, 'diagram.svg');
|
||||||
|
process.env.DEFAULT_CMD_HISTORY_PATH = utils.joinPath(process.env.DAPP_PATH, '.embark', 'cmd_history');
|
||||||
|
process.env.DEFAULT_CMD_HISTORY_SIZE = 20;
|
||||||
|
|
||||||
class Cmd {
|
class Cmd {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
|
let fs = require('../../lib/core/fs');
|
||||||
|
|
||||||
class CommandHistory {
|
class CommandHistory {
|
||||||
constructor() {
|
constructor(options = {}) {
|
||||||
|
this.cmdHistoryFile = options.cmdHistoryFile
|
||||||
|
|| process.env.DEFAULT_CMD_HISTORY_PATH;
|
||||||
this.history = [];
|
this.history = [];
|
||||||
this.pointer = -1;
|
this.pointer = -1;
|
||||||
|
this.loadHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
addCommand(cmd) {
|
addCommand(cmd) {
|
||||||
|
@ -24,6 +29,16 @@ class CommandHistory {
|
||||||
this.pointer++;
|
this.pointer++;
|
||||||
return this.history[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;
|
module.exports = CommandHistory;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const repl = require("repl");
|
const repl = require("repl");
|
||||||
const util = require("util");
|
const util = require("util");
|
||||||
|
let fs = require('../../lib/core/fs');
|
||||||
|
|
||||||
class REPL {
|
class REPL {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -28,6 +29,12 @@ class REPL {
|
||||||
writer: this.enhancedWriter.bind(this)
|
writer: this.enhancedWriter.bind(this)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.events.request('console:history', (err, history) => {
|
||||||
|
history
|
||||||
|
.split('\n')
|
||||||
|
.forEach((cmd) => { this.replServer.history.push(cmd); });
|
||||||
|
});
|
||||||
|
|
||||||
this.events.request("runcode:getContext", (context) => {
|
this.events.request("runcode:getContext", (context) => {
|
||||||
this.replServer.context = context;
|
this.replServer.context = context;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
let fs = require('../../core/fs');
|
||||||
let utils = require('../../utils/utils');
|
let utils = require('../../utils/utils');
|
||||||
const EmbarkJS = require('embarkjs');
|
const EmbarkJS = require('embarkjs');
|
||||||
const IpfsApi = require('ipfs-api');
|
const IpfsApi = require('ipfs-api');
|
||||||
|
@ -5,18 +6,24 @@ const Web3 = require('web3');
|
||||||
|
|
||||||
class Console {
|
class Console {
|
||||||
constructor(_embark, options) {
|
constructor(_embark, options) {
|
||||||
|
this.embark = _embark;
|
||||||
this.events = options.events;
|
this.events = options.events;
|
||||||
this.plugins = options.plugins;
|
this.plugins = options.plugins;
|
||||||
this.version = options.version;
|
this.version = options.version;
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
this.ipc = options.ipc;
|
this.ipc = options.ipc;
|
||||||
this.config = options.config;
|
this.config = options.config;
|
||||||
|
this.history = [];
|
||||||
|
this.cmdHistoryFile = options.cmdHistoryFile || process.env.DEFAULT_CMD_HISTORY_PATH;
|
||||||
|
this.loadHistory();
|
||||||
|
|
||||||
if (this.ipc.isServer()) {
|
if (this.ipc.isServer()) {
|
||||||
this.ipc.on('console:executeCmd', this.executeCmd.bind(this));
|
this.ipc.on('console:executeCmd', this.executeCmd.bind(this));
|
||||||
}
|
}
|
||||||
this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this));
|
this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this));
|
||||||
|
this.events.setCommandHandler("console:history", (cb) => this.getHistory(process.env.DEFAULT_CMD_HISTORY_SIZE, cb));
|
||||||
this.registerEmbarkJs();
|
this.registerEmbarkJs();
|
||||||
|
this.registerConsoleCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
processEmbarkCmd (cmd) {
|
processEmbarkCmd (cmd) {
|
||||||
|
@ -26,6 +33,7 @@ class Console {
|
||||||
'',
|
'',
|
||||||
__('possible commands are:'),
|
__('possible commands are:'),
|
||||||
'versions - ' + __('display versions in use for libraries and tools like web3 and solc'),
|
'versions - ' + __('display versions in use for libraries and tools like web3 and solc'),
|
||||||
|
'history - ' + __('display console commands history'),
|
||||||
// TODO: only if the blockchain is actually active!
|
// TODO: only if the blockchain is actually active!
|
||||||
// will need to pass te current embark state here
|
// will need to pass te current embark state here
|
||||||
'ipfs - ' + __('instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)'),
|
'ipfs - ' + __('instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)'),
|
||||||
|
@ -44,6 +52,10 @@ class Console {
|
||||||
}
|
}
|
||||||
|
|
||||||
executeCmd(cmd, callback) {
|
executeCmd(cmd, callback) {
|
||||||
|
if (!(cmd.split(' ')[0] === 'history' || cmd === __('history'))) {
|
||||||
|
this.history.push(cmd);
|
||||||
|
this.saveHistory();
|
||||||
|
}
|
||||||
var pluginCmds = this.plugins.getPluginsProperty('console', 'console');
|
var pluginCmds = this.plugins.getPluginsProperty('console', 'console');
|
||||||
for (let pluginCmd of pluginCmds) {
|
for (let pluginCmd of pluginCmds) {
|
||||||
let pluginResult = pluginCmd.call(this, cmd, {});
|
let pluginResult = pluginCmd.call(this, cmd, {});
|
||||||
|
@ -112,6 +124,50 @@ class Console {
|
||||||
return acc;
|
return acc;
|
||||||
}, '');
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerConsoleCommands() {
|
||||||
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
||||||
|
let [cmdName, length] = cmd.split(' ');
|
||||||
|
return {
|
||||||
|
match: () => cmdName === 'history',
|
||||||
|
process: (callback) => this.getHistory(length, callback)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
loadHistory() {
|
||||||
|
if (fs.existsSync(this.cmdHistoryFile)) {
|
||||||
|
fs.readFileSync(this.cmdHistoryFile)
|
||||||
|
.toString()
|
||||||
|
.split('\n')
|
||||||
|
.reverse()
|
||||||
|
.forEach((cmd) => { this.history.push(cmd); });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getHistory(_length, callback) {
|
||||||
|
if (typeof _length === "string") {
|
||||||
|
_length = parseInt(_length, 10);
|
||||||
|
if (isNaN(_length)) return callback("Invalid argument. Please provide an integer.");
|
||||||
|
}
|
||||||
|
let length = _length || process.env.DEFAULT_CMD_HISTORY_SIZE;
|
||||||
|
return callback(null, this.history
|
||||||
|
.slice(Math.max(0, this.history.length - length))
|
||||||
|
.filter(line => line.trim())
|
||||||
|
.reverse()
|
||||||
|
.join('\n'));
|
||||||
|
}
|
||||||
|
|
||||||
|
saveHistory() {
|
||||||
|
if (fs.existsSync(utils.dirname(this.cmdHistoryFile))) {
|
||||||
|
fs.writeFileSync(this.cmdHistoryFile,
|
||||||
|
this.history
|
||||||
|
.slice(Math.max(0, this.history.length - process.env.DEFAULT_CMD_HISTORY_SIZE))
|
||||||
|
.reverse()
|
||||||
|
.filter(line => line.trim())
|
||||||
|
.join('\n'));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Console;
|
module.exports = Console;
|
||||||
|
|
|
@ -9,7 +9,20 @@ describe('embark.Console', function() {
|
||||||
let ipc = new IPC({ipcRole: 'none'});
|
let ipc = new IPC({ipcRole: 'none'});
|
||||||
let plugins = new Plugins({plugins: {}});
|
let plugins = new Plugins({plugins: {}});
|
||||||
let events = {once: () => {}, setCommandHandler: () => {}, emit: () => {}};
|
let events = {once: () => {}, setCommandHandler: () => {}, emit: () => {}};
|
||||||
let console = new Console({}, {plugins, version, ipc, events});
|
let embarkObject = {
|
||||||
|
events: events,
|
||||||
|
logger: plugins.logger,
|
||||||
|
registerConsoleCommand: (cmd, opt) => {},
|
||||||
|
embarkConfig: {
|
||||||
|
options: {
|
||||||
|
solc: {
|
||||||
|
"optimize": true,
|
||||||
|
"optimize-runs": 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let console = new Console(embarkObject, {plugins, version, ipc, events});
|
||||||
|
|
||||||
describe('#executeCmd', function() {
|
describe('#executeCmd', function() {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue