@@ -14,35 +14,43 @@ const { InteractiveSolidityShell, SolidityStatement } = require('../src/handler'
1414const { convert, multilineInput } = require ( '../src/utils' ) ;
1515
1616const CONFIG_HOME = path . join ( os . homedir ( ) , '.solidity-shell' ) ;
17- const CONFIG_FILE = path . join ( CONFIG_HOME , '.config' ) ;
17+ const CONFIG_FILE = '.config' ;
18+
19+ var SESSION = 'previous.session' ;
1820
1921/** static funcs */
20- function tryLoadSettings ( ) {
21- let settings = { } ;
22+ function loadFile ( name ) {
23+ let cfgFile = path . join ( CONFIG_HOME , name ) ;
2224
23- if ( fs . existsSync ( CONFIG_FILE ) ) {
24- settings = JSON . parse ( fs . readFileSync ( CONFIG_FILE ) ) ;
25+ if ( fs . existsSync ( cfgFile ) ) {
26+ return JSON . parse ( fs . readFileSync ( cfgFile ) ) ;
2527 }
26- return settings ;
28+ return { } ;
2729}
2830
29- function trySaveSettings ( settings ) {
31+ function saveFile ( name , data ) {
32+ let cfgFile = path . join ( CONFIG_HOME , name ) ;
33+
3034 if ( ! fs . existsSync ( CONFIG_HOME ) ) {
3135 fs . mkdirSync ( CONFIG_HOME ) ;
3236 }
33- fs . writeFileSync ( CONFIG_FILE , JSON . stringify ( settings ) ) ;
37+ fs . writeFileSync ( cfgFile , JSON . stringify ( data ) ) ;
3438}
3539
3640/** MAIN */
3741
38- const shell = new InteractiveSolidityShell ( tryLoadSettings ( ) ) ;
42+ const shell = new InteractiveSolidityShell ( loadFile ( CONFIG_FILE ) ) ;
3943
4044const vorpal = new Vorpal ( )
4145 . delimiter ( '' )
4246 . show ( )
4347 . parse ( process . argv ) ;
4448
45- process . on ( 'exit' , ( ) => { shell . blockchain . stopService ( ) ; trySaveSettings ( shell . settings ) } ) ;
49+ process . on ( 'exit' , ( ) => {
50+ shell . blockchain . stopService ( ) ;
51+ saveFile ( CONFIG_FILE , shell . settings )
52+ saveFile ( SESSION , shell . dumpSession ( ) )
53+ } ) ;
4654
4755
4856vorpal
@@ -56,21 +64,31 @@ vorpal
5664 let command = multilineInput ( input ) ;
5765 if ( command . startsWith ( '.' ) ) {
5866 let commandParts = command . split ( ' ' ) ;
67+ let ret = undefined ;
5968 switch ( commandParts [ 0 ] ) {
6069 case '.help' :
6170 cb ( `
6271📚 Help:
6372 -----
6473
65- .help ... this help :)
66- .exit ... exit the shell
74+ General:
75+ .help ... this help :)
76+ .exit ... exit the shell
6777
68- .config ... show settings
69- .set <key> <value> ... set setting
70- .unset <key> ... clear setting
71-
72- .reset ... reset cmd history. start from scratch.
73- .undo ... undo last command
78+ Settings:
79+ .config ... show settings
80+ set <key> <value> ... set setting
81+ unset <key> ... unset setting
82+ Session:
83+ .session ... list sessions
84+ load <id> ... load session
85+ save <id> ... save session
86+
87+ .undo ... undo last command
88+ .reset ... reset cmd history. start from scratch.
89+
90+ Debug:
91+ .dump ... (debug) show template contract
7492
7593
7694cheers 🙌
@@ -79,23 +97,44 @@ cheers 🙌
7997` ) ;
8098
8199 break ; //show usage
82- case '.exit' : process . exit ( ) ; return ; //exit -> no more cb()
100+ case '.exit' : process . exit ( ) ; //exit -> no more cb()
83101 case '.reset' : shell . reset ( ) ; break ; //reset complete state
84102 case '.undo' : shell . revert ( ) ; break ; //revert last action
85- case '.set' : shell . setSetting ( commandParts [ 1 ] , convert ( commandParts [ 2 ] ) ) ; break ;
86- case '.unset' : shell . setSetting ( commandParts [ 1 ] , undefined ) ; break ;
87- case '.config' : return cb ( shell . settings ) ; break ;
103+ case '.config' :
104+ switch ( commandParts [ 1 ] ) {
105+ case 'set' : shell . setSetting ( commandParts [ 2 ] , convert ( commandParts [ 3 ] ) ) ; break ;
106+ case 'del' : delete shell . settings [ commandParts [ 2 ] ] ; break ;
107+ default : return cb ( shell . settings ) ;
108+ } break ;
109+ case '.session' :
110+ switch ( commandParts [ 1 ] ) {
111+ default :
112+ let sessions = fs . readdirSync ( CONFIG_HOME ) . filter ( file => file . endsWith ( '.session' ) ) ;
113+ return cb ( ' - ' + sessions . map ( s => s . replace ( '.session' , '' ) ) . join ( '\n - ' ) ) ;
114+ case 'load' :
115+ shell . loadSession ( loadFile ( `${ commandParts [ 2 ] } .session` ) )
116+ break ;
117+ case 'save' :
118+ SESSION = `${ commandParts [ 2 ] } .session` ;
119+ saveFile ( SESSION , shell . dumpSession ( ) )
120+ break ;
121+ } ; break ;
122+ case '.dump' : return cb ( shell . template ( ) ) ;
123+
88124 default :
89125 console . error ( `unknown command: ${ command } . type '.help' for a list of commands.` ) ;
90126 }
91127 // meta commands
92- return cb ( ) ;
128+ return cb ( ret ) ;
93129 }
94130
95131 const statement = new SolidityStatement ( command ) ;
96132
97133 /* REPL cmd */
98134 shell . run ( statement ) . then ( res => {
135+ if ( typeof res === 'object' ) {
136+ return cb ( ) ;
137+ }
99138 cb ( c . bold ( c . yellow ( res ) ) ) ;
100139 } ) . catch ( errors => {
101140 console . error ( errors )
0 commit comments