Writing code inside sudo.exec in Electron App

2019-07-13 18:07发布

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?

0条回答
登录 后发表回答