Skip to content

Commit fbef4af

Browse files
committed
Merge branch 'dev'
2 parents 801083b + 467565d commit fbef4af

File tree

6 files changed

+4265
-76
lines changed

6 files changed

+4265
-76
lines changed

src/index.js

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
'use strict';
22
const electron = require('electron');
3+
const { app, BrowserWindow, Menu, protocol, ipcMain, dialog } = require('electron');
34

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...');
511

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');
739

840
// adds debug features like hotkeys for triggering dev tools and reload
941
require('electron-debug')();
@@ -43,20 +75,67 @@ app.on('activate', () => {
4375

4476
app.on('ready', () => {
4577
mainWindow = createMainWindow();
78+
autoUpdater.checkForUpdatesAndNotify();
4679
});
4780

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');
4985

5086
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) {
5288
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;
54134
}
55-
event.returnValue = data;
56135
});
57136
});
58137

59138
ipc.on('save_snippets', (event, snippets) => {
60-
fs.writeFile('snippets.json', snippets, 'utf8', function(){});
139+
fs.writeFile(data_file_path, snippets, 'utf8', function () { });
61140
event.returnValue = 1; // Required for sendSync or it hangs forever! You can send back anything here.
62141
});

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const app = new Moon({
7878
hooks: {
7979
init: function() {
8080
this.callMethod('update_search_index')
81-
this.set('snippets', JSON.parse(ipc.sendSync('get_snippets')))
81+
this.set('snippets', JSON.parse(ipc.sendSync('get_snippets')) || this.get('snippets'))
8282
},
8383
mounted: function() {
8484
$('#search').val('')

0 commit comments

Comments
 (0)