-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
167 lines (141 loc) · 4.21 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const { OBSPlayer } = require('./index');
const ChainEventEmitter = require('./src/ChainEventEmitter');
const EventEmitter = require('events');
require('update-electron-app')();
const { app, BrowserWindow, ipcMain, dialog, autoUpdater } = require('electron');
const path = require('path');
const ChildProcess = require('child_process');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const isDev = require('electron-is-dev');
function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}
// eslint-disable-next-line func-style
const spawn = function(command, args) {
// eslint-disable-next-line no-unused-vars
let error, spawnedProcess;
try {
spawnedProcess = ChildProcess.spawn(command, args, { detached: true });
// eslint-disable-next-line no-empty
} catch (error) {}
return spawnedProcess;
};
// eslint-disable-next-line func-style
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
spawnUpdate(['--createShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-obsolete':
app.quit();
return true;
}
}
if (handleSquirrelEvent()) {
return;
}
if(!isDev) {
const server = 'https://east-releases.vercel.app/';
const url = `${server}/update/${process.platform}/${app.getVersion()}`;
autoUpdater.setFeedURL({ url });
autoUpdater.checkForUpdates();
}
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
});
autoUpdater.on('error', message => {
console.error('There was a problem updating the application');
console.error(message);
});
setInterval(() => {
autoUpdater.checkForUpdates();
}, 60000);
const uiEventEmitter = (webContents) => {
return {
send: (channel, args) => {
if ((typeof webContents.send) === 'function') {
webContents.send(channel, args);
// console.log('event send ', channel, args)
} else {
console.log('can not send event');
}
},
on: (channel, callable) => {
ipcMain.on(channel, function (event, args) {
// console.log('event received ', channel)
callable(args, event);
});
}
};
};
const stdEventEmitter = () => {
const eventEmitter = new EventEmitter();
return {
send: eventEmitter.emit,
on: eventEmitter.on,
};
};
const start = (webContents) => {
const eventEmitter = new ChainEventEmitter();
eventEmitter.add(stdEventEmitter());
eventEmitter.add(uiEventEmitter(webContents));
const player = new OBSPlayer(
__dirname,
eventEmitter
);
player.start();
};
const createWindow = () => {
const mainWindow = new BrowserWindow({
minWidth:600,
minHeight: 500,
maxHeight:900,
maxWidth: 730,
width: 730,
height: 900,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'ui', 'preload.js')
}
});
mainWindow.loadFile('ui/index.html').then(() => {
isDev && mainWindow.webContents.openDevTools();
start(mainWindow.webContents);
})
.catch((err) => console.error(err));
};
app.whenReady().then(() => {
createWindow();
// to do config loader
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
stdEventEmitter().send('app.kill');
app.quit();
}
});