-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
229 lines (204 loc) · 6.43 KB
/
main.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {app, ipcMain, ipcRenderer, BrowserWindow, screen, dialog, Menu} from "electron";
import * as path from "path";
import * as url from "url";
import {execFile} from "child_process";
import {autoUpdater} from "electron-updater";
import {join} from "path";
import openAboutWindow from "about-window";
import * as isDev from "electron-is-dev";
import * as ProgressBar from "electron-progressbar/source";
class ElectronMain {
appTitle = "Jenkins Button";
args: any;
mainWindow: BrowserWindow;
secondWindow: BrowserWindow;
progressBar: ProgressBar;
constructor() {
this.initApp();
this.initAppEvents();
this.initIpc();
this.initAutoUpdaterEvents();
}
initApp() {
this.enableHotReload(this.checkElectronArgs());
this.disableSecurityWarnings();
}
initAppEvents() {
app.on("ready", () => this.createMainWindow());
app.on("window-all-closed", () => this.quitAppOnNonDarwin());
app.on("activate", () => this.createDefaultWindows());
}
initAutoUpdaterEvents() {
autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.setFeedURL({
provider: "github",
repo: "jenkins-button",
owner: "skyquartam",
private: false
});
const self = this;
autoUpdater.checkForUpdates();
autoUpdater.addListener("error", function (error) {
// dialog.showMessageBox({title: "Error Happened", message: error.toString(), buttons: ["OK"]});
self.mainWindow.webContents.send("update-error", error);
});
autoUpdater.addListener("checking-for-update", function (event) {
dialog.showMessageBox({title: "Checking for update", message: `Checking for updates...`, buttons: ["OK"]});
});
autoUpdater.on("error", (error) => {
dialog.showErrorBox("Error: ", error == null ? "unknown" : (error.stack || error).toString());
});
autoUpdater.signals.updateDownloaded(updateInfo => {
const buttonIndex = dialog.showMessageBox(
{
title: "Update downloaded",
message: `Version ${updateInfo.version} is downloaded and will be automatically installed on Quit`,
buttons: [
"Installa ora e riavvia",
"Installa al prossimo riavvio"
]
});
if (buttonIndex === 0) {
autoUpdater.quitAndInstall();
}
});
this.initProgressEvent();
}
initProgressEvent() {
const self = this;
autoUpdater.addListener("update-available", function (event) {
dialog.showMessageBox({title: "A new update is ready to install", message: `A new shiny version (${event.version}) is available, click "Ok" to download `, buttons: ["OK"]});
self.mainWindow.webContents.send("update-available", event);
});
autoUpdater.signals.progress(progressInfo => {
let log_message = "Download speed: " + this.formatBytes(progressInfo.bytesPerSecond);
log_message = log_message + " - Downloaded " + progressInfo.percent.toFixed(2) + "%";
log_message = log_message + " (" + this.formatBytes(progressInfo.transferred) + "/" + this.formatBytes(progressInfo.total) + ")";
console.log(log_message);
this.mainWindow.setProgressBar(progressInfo.percent / 100);
this.mainWindow.webContents.send("update-progress", progressInfo);
});
}
formatBytes(bytes: number, decimals?: number) {
if (bytes === 0) {
return "0 Bytes";
}
const k = 1024,
dm = decimals <= 0 ? 0 : decimals || 2,
sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
initIpc() {
ipcMain.on("event", (e, data) => this.ipcEventHandler(e, data));
}
checkElectronArgs(): boolean {
this.args = process.argv.slice(1);
return this.args.some(val => val === "--serve");
}
enableHotReload(serve: boolean) {
if (serve) {
require("electron-reload")(__dirname, {
electron: require(`${__dirname}/node_modules/electron`),
});
}
}
createMainWindow() {
this.mainWindow = this.createBrowserWindow();
this.loadFromFile(this.mainWindow);
this.onWindowClosed(this.mainWindow);
if (!isDev) {
this.createMenu();
} else {
this.enableDevTools(this.mainWindow);
}
}
createBrowserWindow(): BrowserWindow {
const isMac = process.platform === "darwin";
const titleBarStyle = isMac ? "hiddenInset" : "default";
return new BrowserWindow({
title: this.appTitle,
width: 570,
height: 850,
autoHideMenuBar: true,
center: true,
titleBarStyle,
// backgroundColor: "#d38312"
});
}
loadFromFile(window: BrowserWindow, routePath: string = "/") {
window.loadURL(
url.format({
pathname: path.join(__dirname, "/dist/electron-angular/index.html"),
protocol: "file:",
slashes: true,
hash: routePath,
})
);
}
enableDevTools(window: BrowserWindow) {
window.webContents.openDevTools();
}
onWindowClosed(window: BrowserWindow) {
window.on("closed", () => {
this.mainWindow = null;
this.secondWindow = null;
app.quit();
app.exit();
});
}
createDefaultWindows() {
if (null === this.mainWindow) {
this.createMainWindow();
}
if (this.secondWindow === null) {
// this.createSecondWindow();
}
}
quitAppOnNonDarwin() {
if (process.platform !== "darwin") {
app.quit();
}
}
ipcEventHandler(e: any, data: any) {
console.log("[EVENT]:", "recieved from main process");
}
disableSecurityWarnings() {
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
}
execFile(pathUrl?: string) {
if (pathUrl) {
return new Promise((resolve, reject) => {
execFile(pathUrl, (err, data) => {
if (err) {
reject(err);
}
if (data) {
resolve(data);
}
});
});
}
return Promise.reject("No path provided!");
}
private createMenu() {
const menu = Menu.buildFromTemplate([
{
label: "Example",
submenu: [
{
label: `About ${this.appTitle}`,
click: () =>
openAboutWindow({
icon_path: join(__dirname, "buildResources", "icon.png"),
package_json_dir: __dirname
}),
},
],
},
]);
Menu.setApplicationMenu(menu);
}
}
export default new ElectronMain();