@@ -46,17 +46,30 @@ app.on('ready', () => {
4646} ) ;
4747
4848const ipc = require ( 'electron' ) . ipcMain ;
49+ const path = require ( 'path' ) ;
50+
51+ const data_file_path = path . join ( app . getPath ( 'userData' ) , 'snippets.json' ) ;
4952
5053ipc . on ( 'get_snippets' , ( event , arg ) => {
51- fs . readFile ( 'snippets.json' , 'utf8' , function ( err , data ) {
54+ fs . readFile ( data_file_path , 'utf8' , function ( err , data ) {
5255 if ( err ) {
53- return console . error ( ) ; ( err ) ;
56+ // try to recover existing snippet data from old version
57+ fs . readFile ( 'snippets.json' , 'utf8' , function ( err_ , data_ ) {
58+ if ( err_ ) {
59+ event . returnValue = null ;
60+ return console . error ( ) ; ( err_ ) ;
61+ } else {
62+ fs . writeFile ( data_file_path , data_ , 'utf8' , function ( ) { } ) ;
63+ event . returnValue = data_ ;
64+ }
65+ } ) ;
66+ } else {
67+ event . returnValue = data ;
5468 }
55- event . returnValue = data ;
5669 } ) ;
5770} ) ;
5871
5972ipc . on ( 'save_snippets' , ( event , snippets ) => {
60- fs . writeFile ( 'snippets.json' , snippets , 'utf8' , function ( ) { } ) ;
73+ fs . writeFile ( data_file_path , snippets , 'utf8' , function ( ) { } ) ;
6174 event . returnValue = 1 ; // Required for sendSync or it hangs forever! You can send back anything here.
6275} ) ;
0 commit comments