index.js
function executeCommand(cmd, option) {
    option = option || {};
    return new Promise((ac, rj)=>{
        let okMsg = '';
        let erMsg='';
        log('Exec:'+cmd.join(' '));
        let child;
        if (option.sudo) {
            child = sudo(cmd, {password: option.password});
        } else {
            child = spawn(cmd[0], cmd.slice(1));
        }
        child.stdout.on('data', (dat)=>{
            okMsg += dat.toString();
        });
        child.stderr.on('data', (dat)=>{
            erMsg += dat.toString();
        });
        child.on('close', (code)=>{
            if (code == 0) {
                ac(okMsg);
            } else {
                rj('Error in executing\n'
                   +'$ '+cmd.join(' ')+'\n\n'
                   + erMsg);
            }
        });
        child.on('error', (err)=>{
            rj(err);
        });
    });
}
function executeCommand(cmd, option) {
    option = option || {};
    return new Promise((ac, rj)=>{
        let okMsg = '';
        let erMsg='';
        log('Exec:'+cmd.join(' '));
        let child;
        if (option.sudo) {
            child = sudo(cmd, {password: option.password});
        } else {
            child = spawn(cmd[0], cmd.slice(1));
        }
        child.stdout.on('data', (dat)=>{
            okMsg += dat.toString();
        });
        child.stderr.on('data', (dat)=>{
            erMsg += dat.toString();
        });
        child.on('close', (code)=>{
            if (code == 0) {
                ac(okMsg);
            } else {
                rj('Error in executing\n'
                   +'$ '+cmd.join(' ')+'\n\n'
                   + erMsg);
            }
        });
        child.on('error', (err)=>{
            rj(err);
        });
    });
}
function executeCommand ( cmd , option ) { option = option || { } ; return new Promise ( ( ac , rj ) => { let okMsg = '' ; let erMsg = '' ; log ( 'Exec:' + cmd . join ( ' ' ) ) ; let child ; if ( option . sudo ) { child = sudo ( cmd , { password : option . password } ) ; } else { child = spawn ( cmd [ 0 ] , cmd . slice ( 1 ) ) ; } child . stdout . on ( 'data' , ( dat ) => { okMsg += dat . toString ( ) ; } ) ; child . stderr . on ( 'data' , ( dat ) => { erMsg += dat . toString ( ) ; } ) ; child . on ( 'close' , ( code ) => { if ( code == 0 ) { ac ( okMsg ) ; } else { rj ( 'Error in executing\n' + '$ ' + cmd . join ( ' ' ) + '\n\n' + erMsg ) ; } } ) ; child . on ( 'error' , ( err ) => { rj ( err ) ; } ) ; } ) ; }
Execute command with sudo
@param {Array.<string>} cmd : Array of command
@param {object} [option] : option parameter
@return {Promise} Return stdout strings
Execute command with sudo