2.8.23 • Published 4 years ago

@o/electron-sudo v2.8.23

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Electron subprocess with administrative privileges

Run a subprocess with administrative privileges, prompting the user with a graphical OS dialog if necessary. Useful for background subprocesse which run native Electron apps that need sudo.

If you don't trust binaries bundled in npm package you can manually build tools and use them instead.

Features

  • Supports spawn and exec subprocess behavior
  • Supports applications packaged as asar archive
  • Separate password prompt for each call (use bash or bat script for single prompt)
  • No external dependencies, does not depend on OS versions

Installation

npm install electron-sudo

Usage

Note: Your command should not start with the sudo prefix.

Version 4.0.*

import Sudoer from 'electron-sudo';

let options = {name: 'electron sudo application'},
    sudoer = new Sudoer(options);

/* Spawn subprocess behavior */
let cp = await sudoer.spawn(
  'echo', ['$PARAM'], {env: {PARAM: 'VALUE'}}
);
cp.on('close', () => {
  /*
    cp.output.stdout (Buffer)
    cp.output.stderr (Buffer)
  */
});

/* Exec subprocess behavior */
let result = await sudoer.exec(
  'echo $PARAM', {env: {PARAM: 'VALUE'}}
);
/* result is Buffer with mixed (both stdout and stderr) output */


/* Usage with Vanila JS */

var Sudoer = require('electron-sudo').default;
var sudoer = new Sudoer(options);
sudoer.spawn('echo', ['$PARAM'], {env: {PARAM: 'VALUE'}}).then(function (cp) {
  /*
    cp.output.stdout (Buffer)
    cp.output.stderr (Buffer)
  */
});

Version 3.0.* (deprecated)

var sudo = require('electron-sudo');
var options = {
  name: 'Your application name',
  icns: '/path/to/icns/file' // (optional, only for MacOS),
  process: {
    options: {
      // Can use custom environment variables for your privileged subprocess
      env: {'VAR': 'VALUE'}
      // ... and all other subprocess options described here
      // https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
    },
    on: function(ps) {
      ps.stdout.on('data', function(data) {});
      setTimeout(function() {
        ps.kill()
      }.bind(ps), 50000);
    }
  }
};
sudo.exec('echo hello', options, function(error) {});

Tests

npm i && npm test

Usage with Webpack

Webpack config should contain __dirname equals true for work properly

let nodeModules = fs.readdirSync('./node_modules')
    .filter((module) => {
        return module !== '.bin';
    })
    .reduce((prev, module) => {
        return Object.assign(prev, {[module]: 'commonjs ' + module});
    }, {});

export default {
    ...
    target: 'electron',
    node: {
        /* http://webpack.github.io/docs/configuration.html#node */
        __dirname: true
    },
    externals: nodeModules
};
2.8.19

4 years ago

2.8.18

4 years ago

2.8.17

4 years ago

2.8.16

4 years ago

2.8.15

4 years ago

2.8.23

4 years ago

2.8.22

4 years ago

2.8.21

4 years ago

2.7.21

4 years ago

2.7.20

4 years ago

2.7.19

4 years ago

2.7.18

4 years ago

2.7.17

4 years ago

2.7.16

4 years ago

2.7.15

4 years ago

2.7.14

4 years ago

2.7.13

4 years ago

2.7.12

4 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.5.2

6 years ago

2.5.0

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago