Writing code inside sudo.exec in Electron App

杀马特。学长 韩版系。学妹 提交于 2019-12-11 03:03:22

问题


I need to execute code with admin rights at many places. I find sudo.exe and successfully prompt user for permission and password. I still could not figure out how exactly to use sudo.exe. As I am getting same error of permission denied while deleting a file that need admin permission. That is how my code looks like:

const fs = require('fs')
var sudo = require('sudo-prompt');

var options = {
    name: 'Electron',
};

sudo.exec('echo hello', options,
    function(error, stdout, stderr) {
    if (error) throw error;

    // Code that I want to run with admin rights

    fs.unlinkSync("/private/var/log/fsck_hfs.log", (err) => {
        alert("File succesfully deleted");
    });
}
);

I think this method can only be used to run command, like echo hello in this case. What if I actually want to execute a chunk of code instead of a command? Does this method works or these is any other approach available?

Is there a better method available in Electron to get privileges?

来源:https://stackoverflow.com/questions/54052974/writing-code-inside-sudo-exec-in-electron-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!