File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 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 ( ) ;
Original file line number Diff line number Diff line change 11const container = require ( './container' ) ;
2+ const history = require ( './history' ) ;
23const store = require ( './store' ) ;
34const recorder = require ( './recorder' ) ;
45const 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 ) ;
You can’t perform that action at this time.
0 commit comments