|
1 | 1 | 'use strict'; |
2 | 2 | const electron = require('electron'); |
| 3 | +const { app, BrowserWindow, Menu, protocol, ipcMain, dialog } = require('electron'); |
3 | 4 |
|
4 | | -var fs = require('fs'); |
| 5 | +const log = require('electron-log'); |
| 6 | +const { autoUpdater } = require("electron-updater"); |
| 7 | + |
| 8 | +autoUpdater.logger = log; |
| 9 | +autoUpdater.logger.transports.file.level = 'info'; |
| 10 | +log.info('App starting...'); |
5 | 11 |
|
6 | | -const app = electron.app; |
| 12 | +function sendStatusToWindow(text) { |
| 13 | + log.info(text); |
| 14 | + mainWindow.webContents.send('message', text); |
| 15 | +} |
| 16 | +autoUpdater.on('checking-for-update', () => { |
| 17 | + sendStatusToWindow('Checking for update...'); |
| 18 | +}) |
| 19 | +autoUpdater.on('update-available', (info) => { |
| 20 | + sendStatusToWindow('Update available.'); |
| 21 | +}) |
| 22 | +autoUpdater.on('update-not-available', (info) => { |
| 23 | + sendStatusToWindow('Update not available.'); |
| 24 | +}) |
| 25 | +autoUpdater.on('error', (err) => { |
| 26 | + sendStatusToWindow('Error in auto-updater. ' + err); |
| 27 | +}) |
| 28 | +autoUpdater.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); |
| 33 | +}) |
| 34 | +autoUpdater.on('update-downloaded', (info) => { |
| 35 | + sendStatusToWindow('Update downloaded'); |
| 36 | +}); |
| 37 | + |
| 38 | +var fs = require('fs'); |
7 | 39 |
|
8 | 40 | // adds debug features like hotkeys for triggering dev tools and reload |
9 | 41 | require('electron-debug')(); |
@@ -43,20 +75,67 @@ app.on('activate', () => { |
43 | 75 |
|
44 | 76 | app.on('ready', () => { |
45 | 77 | mainWindow = createMainWindow(); |
| 78 | + autoUpdater.checkForUpdatesAndNotify(); |
46 | 79 | }); |
47 | 80 |
|
48 | | -let ipc = require('electron').ipcMain; |
| 81 | +const ipc = require('electron').ipcMain; |
| 82 | +const path = require('path'); |
| 83 | + |
| 84 | +const data_file_path = path.join(app.getPath('userData'), 'snippets.json'); |
49 | 85 |
|
50 | 86 | ipc.on('get_snippets', (event, arg) => { |
51 | | - fs.readFile('snippets.json', 'utf8', function(err, data) { |
| 87 | + fs.readFile(data_file_path, 'utf8', function (err, data) { |
52 | 88 | if (err) { |
53 | | - return console.error();(err); |
| 89 | + // try to recover existing snippet data from old version |
| 90 | + fs.readFile('snippets.json', 'utf8', function (err_, data_) { |
| 91 | + if (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 | + }) |
| 127 | + } else { |
| 128 | + fs.writeFile(data_file_path, data_, 'utf8', function () { }); |
| 129 | + event.returnValue = data_; |
| 130 | + } |
| 131 | + }); |
| 132 | + } else { |
| 133 | + event.returnValue = data; |
54 | 134 | } |
55 | | - event.returnValue = data; |
56 | 135 | }); |
57 | 136 | }); |
58 | 137 |
|
59 | 138 | ipc.on('save_snippets', (event, snippets) => { |
60 | | - fs.writeFile('snippets.json', snippets, 'utf8', function(){}); |
| 139 | + fs.writeFile(data_file_path, snippets, 'utf8', function () { }); |
61 | 140 | event.returnValue = 1; // Required for sendSync or it hangs forever! You can send back anything here. |
62 | 141 | }); |
0 commit comments