Skip to content

Commit 6267db3

Browse files
committed
:修复了无法执行sudo的bug
1 parent 6288899 commit 6267db3

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

main.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const { app, Menu, Tray, BrowserWindow, Notification } = require("electron");
22
const path = require("path");
3-
4-
if (app.getAppPath().slice(-8) == 'app.asar') {
3+
var sudo = require("sudo-prompt");
4+
var options = {
5+
name: "Electron",
6+
};
7+
if (app.getAppPath().slice(-8) == "app.asar") {
58
run_path = path.resolve(__dirname, "..");
69
} else {
710
run_path = path.resolve(__dirname, "");
@@ -71,31 +74,15 @@ app.whenReady().then(() => {
7174
tray.setTitle("hi");
7275
tray.setContextMenu(contextMenu);
7376

74-
const { spawn } = require("child_process");
75-
7677
function run(n, c) {
77-
const ls = spawn(c, {
78-
encoding: "utf8",
79-
cwd: run_path, // 执行命令路径
80-
shell: true, // 使用shell命令
81-
});
82-
83-
// 监听标准输出
84-
ls.stdout.on("data", (data) => {
85-
console.log(`${data}`);
86-
check(n, `${data}`);
87-
return `${data}`;
88-
});
89-
90-
// 监听标准错误
91-
ls.stderr.on("data", (data) => {
92-
show("失败");
93-
});
94-
95-
// 子进程关闭事件
96-
ls.on("close", (code) => {
97-
console.log(`子进程退出,退出码 ${code}`);
98-
});
78+
sudo.exec(
79+
`cd ${run_path}&&${c}`,
80+
options,
81+
function (error, stdout, stderr) {
82+
if (error) show("失败");
83+
check(n, `${stdout}`);
84+
}
85+
);
9986
}
10087

10188
function check(n, x) {

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linux-performance-control",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Linux性能管理",
55
"main": "main.js",
66
"scripts": {
@@ -31,5 +31,8 @@
3131
"./assets/**",
3232
"./run.sh"
3333
]
34+
},
35+
"dependencies": {
36+
"sudo-prompt": "^9.2.1"
3437
}
3538
}

0 commit comments

Comments
 (0)