"use strict";
/**
 * penguins-eggs
 * lib: utils.ts
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = void 0;
const child_process_1 = require("child_process");
/**
 *
 * @param command
 * @param param1
 * @returns
 */
async function exec(command, { echo = false, ignore = false, capture = false } = {}) {
    /**
     * You could wrap spawn in a promise,
     * listen to exit event,
     * and resolve when it happens.
     *
     * That should play nicely with oclif/core.
     */
    return new Promise((resolve, reject) => {
        if (echo) {
            console.log(command);
        }
        const child = (0, child_process_1.spawn)('bash', ['-c', command], {
            stdio: ignore ? 'ignore' : (capture ? 'pipe' : 'inherit'),
        });
        // const spawn = require('child_process').spawn
        // child.stdout.on('data', (data: string) => {
        let stdout = '';
        if (capture) {
            child.stdout?.on('data', (data) => {
                stdout += data;
            });
        }
        // 'error' event
        child.on('error', function (error) {
            reject({ code: 1, error: error });
        });
        // The 'exit' event is emitted after the child process ends. If the process exited, code is the final exit code of the process,
        // otherwise null. If the process terminated due to receipt of a signal, signal is the string name of the signal, otherwise null.
        // One of the two will always be non-null.
        child.on('exit', (code) => {
            resolve({ code: code, data: stdout });
        });
        // end promise
    });
}
exports.exec = exec;
/**
  * Il problema è in questa funzione: core/cli-ux/indesx.ts
  * qui riportata solo come esempio
  *
function timeout(p: Promise<any>, ms: number) {
  function wait(ms: number, unref = false) {
    return new Promise(resolve => {
      const t: any = setTimeout(() => resolve(null), ms)
      if (unref) t.unref()
    })
  }

  return Promise.race([p, wait(ms, true).then(() => ux.error('timed out'))])
}
*/
