Skip to content

Commit cff3fda

Browse files
committed
refactor main-app process
clean unnecessary codes
1 parent 108e83a commit cff3fda

File tree

5 files changed

+119
-124
lines changed

5 files changed

+119
-124
lines changed

Diff for: index.js

+76-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
1+
const { app } = require('electron')
2+
const ChildProcess = require('child_process')
3+
const path = require('path')
4+
5+
var error = null
6+
17
function isFinderCalled () {
28
var argv = process.argv.slice(1)
39
return argv.some((arg) => arg.match(/--finder/))
410
}
511

12+
function execMainApp () {
13+
const appRootPath = path.join(process.execPath, '../..')
14+
const updateDotExePath = path.join(appRootPath, 'Update.exe')
15+
const exeName = path.basename(process.execPath)
16+
17+
function spawnUpdate (args, cb) {
18+
var stdout = ''
19+
var updateProcess = null
20+
try {
21+
updateProcess = ChildProcess.spawn(updateDotExePath, args)
22+
} catch (e) {
23+
process.nextTick(function () {
24+
cb(e)
25+
})
26+
}
27+
28+
updateProcess.stdout.on('data', function (data) {
29+
stdout += data
30+
})
31+
32+
updateProcess.on('error', function (_error) {
33+
error = _error
34+
})
35+
updateProcess.on('close', function (code, signal) {
36+
if (code !== 0) {
37+
error = new Error('Command failed: #{signal ? code}')
38+
error.code = code
39+
error.stdout = stdout
40+
}
41+
42+
cb(error, stdout)
43+
})
44+
}
45+
46+
var handleStartupEvent = function () {
47+
if (process.platform !== 'win32') {
48+
return false
49+
}
50+
51+
var squirrelCommand = process.argv[1]
52+
switch (squirrelCommand) {
53+
case '--squirrel-install':
54+
spawnUpdate(['--createShortcut', exeName], function (err) {
55+
if (err) console.error(err)
56+
app.quit()
57+
})
58+
return true
59+
case '--squirrel-updated':
60+
app.quit()
61+
return true
62+
case '--squirrel-uninstall':
63+
spawnUpdate(['--removeShortcut', exeName], function (err) {
64+
if (err) console.error(err)
65+
app.quit()
66+
})
67+
return true
68+
case '--squirrel-obsolete':
69+
app.quit()
70+
return true
71+
}
72+
}
73+
74+
if (handleStartupEvent()) {
75+
return
76+
}
77+
78+
require('./lib/main-app')
79+
}
80+
681
if (isFinderCalled()) {
782
require('./lib/finder-app')
883
} else {
9-
require('./lib/main-app')
84+
execMainApp()
1085
}

Diff for: lib/finder-window.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const electron = require('electron')
2+
const { app } = electron
23
const BrowserWindow = electron.BrowserWindow
34
const Menu = electron.Menu
45
const MenuItem = electron.MenuItem
@@ -44,7 +45,7 @@ finderWindow.on('close', function (e) {
4445
})
4546

4647
var appIcon = new Tray(path.join(__dirname, '../resources/tray-icon.png'))
47-
appIcon.setToolTip('Boost')
48+
appIcon.setToolTip('Boostnote')
4849

4950
var trayMenu = new Menu()
5051
trayMenu.append(new MenuItem({
@@ -84,4 +85,9 @@ function hideFinder () {
8485
}
8586
finderWindow.hide()
8687
}
88+
89+
app.on('before-quit', function (e) {
90+
finderWindow.removeAllListeners()
91+
})
92+
8793
module.exports = finderWindow

Diff for: lib/main-app.js

+30-117
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,11 @@ const ChildProcess = require('child_process')
88
const _ = require('lodash')
99
const GhReleases = require('electron-gh-releases')
1010
// electron.crashReporter.start()
11+
require('./ipc')
1112

1213
var mainWindow = null
13-
var finderProcess = null
1414
var finderWindow = null
1515

16-
const appRootPath = path.join(process.execPath, '../..')
17-
const updateDotExePath = path.join(appRootPath, 'Update.exe')
18-
const exeName = path.basename(process.execPath)
19-
20-
// For windows app
21-
function spawnUpdate (args, cb) {
22-
var stdout = ''
23-
var updateProcess = null
24-
try {
25-
updateProcess = ChildProcess.spawn(updateDotExePath, args)
26-
} catch (e) {
27-
process.nextTick(function () {
28-
cb(e)
29-
})
30-
}
31-
32-
updateProcess.stdout.on('data', function (data) {
33-
stdout += data
34-
})
35-
36-
error = null
37-
updateProcess.on('error', function (_error) {
38-
error = _error
39-
})
40-
updateProcess.on('close', function (code, signal) {
41-
if (code !== 0) {
42-
error = new Error("Command failed: #{signal ? code}")
43-
error.code = code
44-
error.stdout = stdout
45-
}
46-
47-
cb(error, stdout)
48-
})
49-
}
50-
51-
var handleStartupEvent = function () {
52-
if (process.platform !== 'win32') {
53-
return false
54-
}
55-
56-
var squirrelCommand = process.argv[1]
57-
switch (squirrelCommand) {
58-
case '--squirrel-install':
59-
spawnUpdate(['--createShortcut', exeName], function (err) {
60-
app.quit()
61-
})
62-
return true
63-
case '--squirrel-updated':
64-
app.quit()
65-
return true
66-
case '--squirrel-uninstall':
67-
spawnUpdate(['--removeShortcut', exeName], function (err) {
68-
app.quit()
69-
})
70-
return true
71-
case '--squirrel-obsolete':
72-
app.quit()
73-
return true
74-
}
75-
}
76-
77-
if (handleStartupEvent()) {
78-
return
79-
}
80-
8116
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
8217
if (mainWindow) {
8318
if (process.platform === 'win32') {
@@ -90,7 +25,6 @@ var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory)
9025
})
9126

9227
if (shouldQuit) {
93-
if (mainWindow != null) mainWindow.removeAllListeners()
9428
app.quit()
9529
return
9630
}
@@ -117,15 +51,10 @@ function checkUpdate () {
11751
if (err) {
11852
var isLatest = err.message === 'There is no newer version.'
11953
if (!isLatest) console.error('Updater error! %s', err.message)
54+
return
12055
}
121-
if (!err) {
122-
if (status) {
123-
// Download start
124-
mainWindow.webContents.send('update-found', 'Update found!')
125-
updater.download()
126-
} else {
127-
// Latest version
128-
}
56+
if (status) {
57+
updater.download()
12958
}
13059
})
13160
}
@@ -137,69 +66,53 @@ updater.on('update-downloaded', (info) => {
13766
}
13867
})
13968

140-
141-
function spawnFinder() {
142-
if (process.platform === 'darwin') {
143-
var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
144-
if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
145-
finderProcess = ChildProcess
146-
.execFile(process.execPath, finderArgv)
69+
ipc.on('update-app-confirm', function (event, msg) {
70+
if (isUpdateReady) {
71+
updater.install()
14772
}
148-
}
149-
150-
app.on('ready', function () {
151-
var template = require('./main-menu')
152-
var menu = Menu.buildFromTemplate(template)
153-
if (process.platform === 'darwin' || process.platform === 'linux') {
154-
Menu.setApplicationMenu(menu)
155-
}
156-
157-
// Check update every 24 hours
158-
setInterval(function () {
159-
checkUpdate()
160-
}, 1000 * 60 * 60 * 24)
161-
162-
ipc.on('check-update', function (event, msg) {
163-
checkUpdate()
164-
})
73+
})
16574

166-
ipc.on('update-app-confirm', function (event, msg) {
167-
if (isUpdateReady) {
168-
mainWindow.removeAllListeners()
169-
updater.install()
170-
}
171-
})
75+
function spawnFinder () {
76+
var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
77+
if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
78+
var finderProcess = ChildProcess
79+
.execFile(process.execPath, finderArgv)
17280

173-
ipc.on('quit-app-confirm', function () {
174-
mainWindow.removeAllListeners()
175-
app.quit()
81+
app.on('before-quit', function () {
82+
finderProcess.kill()
17683
})
84+
}
17785

178-
checkUpdate()
179-
86+
app.on('ready', function () {
18087
mainWindow = require('./main-window')
181-
if (process.platform === 'win32' || process.platform === 'linux') {
182-
mainWindow.setMenu(menu)
183-
}
18488

89+
var template = require('./main-menu')
90+
var menu = Menu.buildFromTemplate(template)
18591
switch (process.platform) {
18692
case 'darwin':
18793
spawnFinder()
94+
Menu.setApplicationMenu(menu)
18895
break
18996
case 'win32':
19097
finderWindow = require('./finder-window')
191-
finderWindow.on('close', function (e) {
192-
e.preventDefault()
193-
finderWindow.hide()
194-
})
98+
mainWindow.setMenu(menu)
19599
break
196100
case 'linux':
197101
// Finder is available on cinnamon only.
198102
if (process.env.DESKTOP_SESSION === 'cinnamon') {
199103
finderWindow = require('./finder-window')
200104
}
105+
Menu.setApplicationMenu(menu)
106+
mainWindow.setMenu(menu)
201107
}
202108

109+
// Check update every hour
110+
setInterval(function () {
111+
checkUpdate()
112+
}, 1000 * 60 * 60)
113+
114+
checkUpdate()
115+
203116
require('./hotkey')
204117
})
205118

Diff for: lib/main-menu.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ var boost = OSX
3636
type: 'separator'
3737
},
3838
{
39-
label: 'Quit',
40-
accelerator: 'Command+Q',
41-
click: function () {
42-
mainWindow.webContents.send('quit-app', {})
43-
}
39+
role: 'quit'
4440
}
4541
]
4642
}

Diff for: lib/main-window.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const electron = require('electron')
22
const app = electron.app
33
const BrowserWindow = electron.BrowserWindow
44
const path = require('path')
5+
const ipc = require('./ipc')
56

67
var mainWindow = new BrowserWindow({
78
width: 1080,
@@ -46,4 +47,8 @@ app.on('activate', function () {
4647
mainWindow.show()
4748
})
4849

50+
app.on('before-quit', function (e) {
51+
mainWindow.removeAllListeners()
52+
})
53+
4954
module.exports = mainWindow

0 commit comments

Comments
 (0)