electron-forge创建快捷方式

发布时间:

更新时间:

这个官方推荐插件用得我好迷啊,一个打包工具常见的快捷方式还得自己实现,不知是我没找到还是咋回事,在官方 issue 下面找到的下面的方法,个人认为没 electron-builder 好用,虽然官方说 6.0 怎么重构怎么好的。

改造 main.js

在 main.js 里添加方法 handleSquirrelEvent,然后将其他代码放到 else 里

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
if (handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
}else {
// 之前的所有代码都放这里
}

function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require("child_process");
const path = require("path");
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 spawn = function (command, args) {
let spawnedProcess, error;

try {
spawnedProcess = ChildProcess.spawn(command, args, { detached: true });
} catch (error) {}

return spawnedProcess;
};

const spawnUpdate = function (args) {
return spawn(updateDotExe, args);
};

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

// Install desktop and start menu shortcuts
spawnUpdate(["--createShortcut", exeName]);

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

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

// Remove desktop and start menu shortcuts
spawnUpdate(["--removeShortcut", exeName]);

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;
}
}
请吃小笼包
支付宝 | Alipay
微信 | Wechat