lib/docker.js
function promisedSpawn(args, options) {

    options = options || {};

    let promise = new P((resolve, reject) => {
        const argOpts = options.capture ? undefined : { stdio: 'inherit' };
        let ret = '';
        let err = '';
        if (opts.verbose) {
            console.log(`# RUNNING: ${args.join(' ')}\n  (in ${process.cwd()})`);
        }
        child = spawn('/usr/bin/env', args, argOpts);
        if (options.capture) {
            child.stdout.on('data', (data) => {
                ret += data.toString();
            });
            child.stderr.on('data', (data) => {
                err += data.toString();
            });
        }
        child.on('close', (code) => {
            child = undefined;
            ret = ret.trim();
            if (ret === '') {
                ret = undefined;
            }
            if (code) {
                if (options.ignoreErr) {
                    resolve(ret);
                } else {
                    reject(new SpawnError(code, err.trim()));
                }
            } else {
                resolve(ret);
            }
        });
    });

    if (options.useErrHandler || options.errMessage) {
        promise = promise.catch((err) => {
            if (options.errMessage) {
                console.error(`ERROR: ${options.errMessage.split('\n').join('\nERROR: ')}`);
            }
            let msg = `ERROR: ${args.slice(0, 2).join(' ')} exited with code ${err.code}`;
            if (err.message) {
                msg += ` and message ${err.message}`;
            }
            console.error(msg);
            process.exit(err.code);
        });
    }

    return promise;

}
function promisedSpawn(args, options) {

    options = options || {};

    let promise = new P((resolve, reject) => {
        const argOpts = options.capture ? undefined : { stdio: 'inherit' };
        let ret = '';
        let err = '';
        if (opts.verbose) {
            console.log(`# RUNNING: ${args.join(' ')}\n  (in ${process.cwd()})`);
        }
        child = spawn('/usr/bin/env', args, argOpts);
        if (options.capture) {
            child.stdout.on('data', (data) => {
                ret += data.toString();
            });
            child.stderr.on('data', (data) => {
                err += data.toString();
            });
        }
        child.on('close', (code) => {
            child = undefined;
            ret = ret.trim();
            if (ret === '') {
                ret = undefined;
            }
            if (code) {
                if (options.ignoreErr) {
                    resolve(ret);
                } else {
                    reject(new SpawnError(code, err.trim()));
                }
            } else {
                resolve(ret);
            }
        });
    });

    if (options.useErrHandler || options.errMessage) {
        promise = promise.catch((err) => {
            if (options.errMessage) {
                console.error(`ERROR: ${options.errMessage.split('\n').join('\nERROR: ')}`);
            }
            let msg = `ERROR: ${args.slice(0, 2).join(' ')} exited with code ${err.code}`;
            if (err.message) {
                msg += ` and message ${err.message}`;
            }
            console.error(msg);
            process.exit(err.code);
        });
    }

    return promise;

}
function promisedSpawn ( args , options ) { options = options || { } ; let promise = new P ( ( resolve , reject ) => { const argOpts = options . capture ? undefined : { stdio : 'inherit' } ; let ret = '' ; let err = '' ; if ( opts . verbose ) { console . log ( ` ${ args . join ( ' ' ) } \n ${ process . cwd ( ) } ` ) ; } child = spawn ( '/usr/bin/env' , args , argOpts ) ; if ( options . capture ) { child . stdout . on ( 'data' , ( data ) => { ret += data . toString ( ) ; } ) ; child . stderr . on ( 'data' , ( data ) => { err += data . toString ( ) ; } ) ; } child . on ( 'close' , ( code ) => { child = undefined ; ret = ret . trim ( ) ; if ( ret === '' ) { ret = undefined ; } if ( code ) { if ( options . ignoreErr ) { resolve ( ret ) ; } else { reject ( new SpawnError ( code , err . trim ( ) ) ) ; } } else { resolve ( ret ) ; } } ) ; } ) ; if ( options . useErrHandler || options . errMessage ) { promise = promise . catch ( ( err ) => { if ( options . errMessage ) { console . error ( ` ${ options . errMessage . split ( '\n' ) . join ( '\nERROR: ' ) } ` ) ; } let msg = ` ${ args . slice ( 0 , 2 ) . join ( ' ' ) } ${ err . code } ` ; if ( err . message ) { msg += ` ${ err . message } ` ; } console . error ( msg ) ; process . exit ( err . code ) ; } ) ; } return promise ; }
Wraps a child process spawn in a promise which resolves
when the child process exists.
@param {Array}   args     the command and its arguments to run (uses /usr/bin/env)
@param {Object}  options  various execution options; attributes:
- {Boolean} capture        whether to capture stdout and return its contents
- {Boolean} useErrHandler  whether to use a generic error handler on failure
- {string}  errMessage     additional error message to emit
- {Boolean} ignoreErr      whether to ignore the error code entirely
@return {Promise} the promise which is fulfilled once the child exists
Wraps a child process spawn in a promise which resolves when the child process exists .