Skip to content

Commit 7228b99

Browse files
hubiduDavertMik
authored andcommitted
Record repl commands into history file (#1708)
* Add german translation * Add trailing comma * Record repl commands into a history file * Remove translation files * Save history file to output directory
1 parent 43b513e commit 7228b99

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/history.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const output = require('./output');
5+
const colors = require('chalk');
6+
7+
/**
8+
* REPL history records REPL commands and stores them in
9+
* a file (~history) when session ends.
10+
*/
11+
class ReplHistory {
12+
constructor() {
13+
this.commands = [];
14+
}
15+
16+
push(cmd) {
17+
this.commands.push(cmd);
18+
}
19+
20+
pop() {
21+
this.commands.pop();
22+
}
23+
24+
save() {
25+
if (this.commands.length === 0) {
26+
return;
27+
}
28+
29+
const historyFile = path.join(global.output_dir, 'cli-history');
30+
const commandSnippet = `\n\n<<< Recorded commands on ${new Date()}\n${this.commands.join('\n')}`;
31+
fs.appendFileSync(historyFile, commandSnippet);
32+
33+
output.print(colors.yellow(` Commands have been saved to ${historyFile}`));
34+
35+
this.commands = [];
36+
}
37+
}
38+
39+
module.exports = new ReplHistory();

lib/pause.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const container = require('./container');
2+
const history = require('./history');
23
const store = require('./store');
34
const recorder = require('./recorder');
45
const event = require('./event');
@@ -58,18 +59,27 @@ function parseInput(cmd) {
5859
finish();
5960
recorder.session.restore();
6061
rl.close();
62+
history.save();
6163
return nextStep();
6264
}
6365
store.debugMode = true;
6466
try {
6567
const locate = global.locate; // enable locate in this context
6668
const I = container.support('I');
67-
eval(`I.${cmd}`); // eslint-disable-line no-eval
69+
70+
const fullCommand = `I.${cmd}`;
71+
eval(fullCommand); // eslint-disable-line no-eval
72+
73+
history.push(fullCommand); // add command to history when successful
6874
} catch (err) {
6975
output.print(output.styles.error(' ERROR '), err.message);
7076
}
7177
recorder.session.catch((err) => {
7278
const msg = err.cliMessage ? err.cliMessage() : err.message;
79+
80+
// pop latest command from history because it failed
81+
history.pop();
82+
7383
return output.print(output.styles.error(' FAIL '), msg);
7484
});
7585
recorder.add('ask for next step', askForStep);

0 commit comments

Comments
 (0)