All files / util linux-installer.js

14.29% Statements 2/14
0% Branches 0/4
0% Functions 0/5
14.29% Lines 2/14
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      1x               1x                                    
import { spawnSync } from 'child_process';
import Sudoer from 'electron-sudo';
 
const which = async (type, prog, promise) => {
  if (spawnSync('which', [prog]).status === 0) {
    await promise;
  } else {
    throw new Error(`${prog} is required to install ${type} packages`);
  }
};
 
export const sudo = (type, prog, args) =>
  new Promise((resolve, reject) => {
    const sudoer = new Sudoer({ name: 'Electron Forge' });
 
    which(type, prog, sudoer.spawn(`${prog} ${args}`)
      .then((child) => {
        child.on('exit', async (code) => {
          if (code !== 0) {
            console.error(child.output.stdout.toString('utf8').red);
            console.error(child.output.stderr.toString('utf8').red);
            return reject(new Error(`${prog} failed with status code ${code}`));
          }
          resolve();
        });
      }));
  });
 
export default which;