优化electron-forge发布的应用安装时两次打开应用

发布时间:

更新时间:

在官方推荐 electron-forge 之后便用了,但之前一直是图方便用 package 打包绿色版,后来用自动更新要用打包版本,发现了这个很迷的问题。想到的方案就是让它安装时不创建窗口,等安装结束后打开时再创建窗口。
在 main.js 里添加方法,将其他代码放到 else 里,如果不处在安装过程中再创建。
在下方的事件里还可以创建快捷方式等,如果有其他方式实现创建快捷方式等内容,用 CheckIsSetup 也行。

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
// 放在最上面
if (handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
} else {
// createWindow...
}

function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}

const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case "--squirrel-install":
case "--squirrel-updated":
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus

setTimeout(app.exit, 1000);
return true;

case "--squirrel-uninstall":
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers

setTimeout(app.exit, 1000);
return true;

case "--squirrel-obsolete":
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated

app.exit();
return true;
}
}

function checkIsSetup() {
if (process.argv.length === 1) {
return false;
} else {
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case "--squirrel-install":
case "--squirrel-updated":
case "--squirrel-uninstall":
case "--squirrel-obsolete":
app.exit();
return true;
}
}
}
请吃小笼包
支付宝 | Alipay
微信 | Wechat