11'use strict' ;
22const electron = require ( 'electron' ) ;
3- const { app, BrowserWindow, Menu, protocol, ipcMain} = require ( 'electron' ) ;
3+ const { app, BrowserWindow, Menu, protocol, ipcMain, dialog } = require ( 'electron' ) ;
44
55const log = require ( 'electron-log' ) ;
6- const { autoUpdater} = require ( "electron-updater" ) ;
6+ const { autoUpdater } = require ( "electron-updater" ) ;
77
88autoUpdater . logger = log ;
99autoUpdater . logger . transports . file . level = 'info' ;
1010log . info ( 'App starting...' ) ;
1111
1212function sendStatusToWindow ( text ) {
13- log . info ( text ) ;
14- mainWindow . webContents . send ( 'message' , text ) ;
13+ log . info ( text ) ;
14+ mainWindow . webContents . send ( 'message' , text ) ;
1515}
1616autoUpdater . on ( 'checking-for-update' , ( ) => {
17- sendStatusToWindow ( 'Checking for update...' ) ;
17+ sendStatusToWindow ( 'Checking for update...' ) ;
1818} )
1919autoUpdater . on ( 'update-available' , ( info ) => {
20- sendStatusToWindow ( 'Update available.' ) ;
20+ sendStatusToWindow ( 'Update available.' ) ;
2121} )
2222autoUpdater . on ( 'update-not-available' , ( info ) => {
23- sendStatusToWindow ( 'Update not available.' ) ;
23+ sendStatusToWindow ( 'Update not available.' ) ;
2424} )
2525autoUpdater . on ( 'error' , ( err ) => {
26- sendStatusToWindow ( 'Error in auto-updater. ' + err ) ;
26+ sendStatusToWindow ( 'Error in auto-updater. ' + err ) ;
2727} )
2828autoUpdater . on ( 'download-progress' , ( progressObj ) => {
29- let log_message = "Download speed: " + progressObj . bytesPerSecond ;
30- log_message = log_message + ' - Downloaded ' + progressObj . percent + '%' ;
31- log_message = log_message + ' (' + progressObj . transferred + "/" + progressObj . total + ')' ;
32- sendStatusToWindow ( log_message ) ;
29+ let log_message = "Download speed: " + progressObj . bytesPerSecond ;
30+ log_message = log_message + ' - Downloaded ' + progressObj . percent + '%' ;
31+ log_message = log_message + ' (' + progressObj . transferred + "/" + progressObj . total + ')' ;
32+ sendStatusToWindow ( log_message ) ;
3333} )
3434autoUpdater . on ( 'update-downloaded' , ( info ) => {
35- sendStatusToWindow ( 'Update downloaded' ) ;
35+ sendStatusToWindow ( 'Update downloaded' ) ;
3636} ) ;
3737
3838var fs = require ( 'fs' ) ;
@@ -84,15 +84,48 @@ const path = require('path');
8484const data_file_path = path . join ( app . getPath ( 'userData' ) , 'snippets.json' ) ;
8585
8686ipc . on ( 'get_snippets' , ( event , arg ) => {
87- fs . readFile ( data_file_path , 'utf8' , function ( err , data ) {
87+ fs . readFile ( data_file_path , 'utf8' , function ( err , data ) {
8888 if ( err ) {
8989 // try to recover existing snippet data from old version
90- fs . readFile ( 'snippets.json' , 'utf8' , function ( err_ , data_ ) {
90+ fs . readFile ( 'snippets.json' , 'utf8' , function ( err_ , data_ ) {
9191 if ( err_ ) {
92- event . returnValue = null ;
93- return console . error ( ) ; ( err_ ) ;
92+ // ask the user to manually select the old snippets.json
93+ let is_new_user = dialog . showMessageBox ( mainWindow , {
94+ 'type' : 'question' ,
95+ 'buttons' : [ 'Yes' , 'No' ] ,
96+ 'title' : 'Have you created any snippets?' ,
97+ 'message' : 'Have you created any snippets yet?\n\nIf so, they need to be migrated to the new storage location.'
98+ } )
99+ if ( is_new_user == 1 ) {
100+ event . returnValue = null ;
101+ return ;
102+ }
103+ dialog . showMessageBox ( mainWindow , {
104+ 'type' : 'info' ,
105+ 'message' : 'You will be prompted to choose a file named snippets.json using the file chooser.\n\nLook in the repo\'s src/ directory.'
106+ } )
107+ let old_snippets_path = dialog . showOpenDialog ( mainWindow , {
108+ 'type' : 'warning' ,
109+ 'title' : 'Choose the snippets.json file from the local repo\'s src/ directory' ,
110+ 'message' : 'Choose the snippets.json file from the local repo\'s src/ directory' ,
111+ 'filters' : [
112+ {
113+ 'name' : 'JSON' ,
114+ 'extensions' : [ 'json' ]
115+ }
116+ ]
117+ } ) [ 0 ]
118+ fs . readFile ( old_snippets_path , 'utf8' , function ( err , data ) {
119+ if ( err ) {
120+ event . returnValue = null ;
121+ return console . error ( ) ; ( err ) ;
122+ } else {
123+ fs . writeFile ( data_file_path , data , 'utf8' , function ( ) { } ) ;
124+ event . returnValue = data ;
125+ }
126+ } )
94127 } else {
95- fs . writeFile ( data_file_path , data_ , 'utf8' , function ( ) { } ) ;
128+ fs . writeFile ( data_file_path , data_ , 'utf8' , function ( ) { } ) ;
96129 event . returnValue = data_ ;
97130 }
98131 } ) ;
@@ -103,6 +136,6 @@ ipc.on('get_snippets', (event, arg) => {
103136} ) ;
104137
105138ipc . on ( 'save_snippets' , ( event , snippets ) => {
106- fs . writeFile ( data_file_path , snippets , 'utf8' , function ( ) { } ) ;
139+ fs . writeFile ( data_file_path , snippets , 'utf8' , function ( ) { } ) ;
107140 event . returnValue = 1 ; // Required for sendSync or it hangs forever! You can send back anything here.
108141} ) ;
0 commit comments