"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeriallyRunningTasks = exports.ParallelRunningTasks = void 0;
const child_process_1 = require("child_process");
const run_commands_impl_1 = require("./run-commands.impl");
const path_1 = require("path");
const chalk = require("chalk");
const npm_run_path_1 = require("npm-run-path");
const task_env_1 = require("../../tasks-runner/task-env");
const treeKill = require("tree-kill");
class ParallelRunningTasks {
    constructor(options, context) {
        this.exitCallbacks = [];
        this.childProcesses = options.commands.map((commandConfig) => new RunningNodeProcess(commandConfig, options.color, calculateCwd(options.cwd, context), options.env ?? {}, options.readyWhenStatus, options.streamOutput, options.envFile));
        this.readyWhenStatus = options.readyWhenStatus;
        this.streamOutput = options.streamOutput;
        this.run();
    }
    async getResults() {
        return new Promise((res) => {
            this.onExit((code, terminalOutput) => {
                res({ code, terminalOutput });
            });
        });
    }
    onExit(cb) {
        this.exitCallbacks.push(cb);
    }
    send(message) {
        for (const childProcess of this.childProcesses) {
            childProcess.send(message);
        }
    }
    async kill(signal) {
        await Promise.all(this.childProcesses.map(async (p) => {
            try {
                return p.kill();
            }
            catch (e) {
                console.error(`Unable to terminate "${p.command}"\nError:`, e);
            }
        }));
    }
    async run() {
        if (this.readyWhenStatus.length) {
            let { childProcess, result: { code, terminalOutput }, } = await Promise.race(this.childProcesses.map((childProcess) => new Promise((res) => {
                childProcess.onExit((code, terminalOutput) => {
                    res({
                        childProcess,
                        result: { code, terminalOutput },
                    });
                });
            })));
            if (code !== 0) {
                const output = `Warning: command "${childProcess.command}" exited with non-zero status code`;
                terminalOutput += output;
                if (this.streamOutput) {
                    process.stderr.write(output);
                }
            }
            for (const cb of this.exitCallbacks) {
                cb(code, terminalOutput);
            }
        }
        else {
            const results = await Promise.all(this.childProcesses.map((childProcess) => childProcess.getResults().then((result) => ({
                childProcess,
                result,
            }))));
            let terminalOutput = results
                .map((r) => r.result.terminalOutput)
                .join('\r\n');
            const failed = results.filter((result) => result.result.code !== 0);
            if (failed.length > 0) {
                const output = failed
                    .map((failedResult) => `Warning: command "${failedResult.childProcess.command}" exited with non-zero status code`)
                    .join('\r\n');
                terminalOutput += output;
                if (this.streamOutput) {
                    process.stderr.write(output);
                }
                for (const cb of this.exitCallbacks) {
                    cb(1, terminalOutput);
                }
            }
            else {
                for (const cb of this.exitCallbacks) {
                    cb(0, terminalOutput);
                }
            }
        }
    }
}
exports.ParallelRunningTasks = ParallelRunningTasks;
class SeriallyRunningTasks {
    constructor(options, context, pseudoTerminal) {
        this.pseudoTerminal = pseudoTerminal;
        this.terminalOutput = '';
        this.currentProcess = null;
        this.exitCallbacks = [];
        this.code = 0;
        this.run(options, context)
            .catch((e) => {
            this.error = e;
        })
            .finally(() => {
            for (const cb of this.exitCallbacks) {
                cb(this.code, this.terminalOutput);
            }
        });
    }
    getResults() {
        return new Promise((res, rej) => {
            this.onExit((code) => {
                if (this.error) {
                    rej(this.error);
                }
                else {
                    res({ code, terminalOutput: this.terminalOutput });
                }
            });
        });
    }
    onExit(cb) {
        this.exitCallbacks.push(cb);
    }
    send(message) {
        throw new Error('Not implemented');
    }
    kill(signal) {
        return this.currentProcess.kill(signal);
    }
    async run(options, context) {
        for (const c of options.commands) {
            const childProcess = await this.createProcess(c, [], options.color, calculateCwd(options.cwd, context), options.processEnv ?? options.env ?? {}, false, options.usePty, options.streamOutput, options.tty, options.envFile);
            this.currentProcess = childProcess;
            let { code, terminalOutput } = await childProcess.getResults();
            this.terminalOutput += terminalOutput;
            this.code = code;
            if (code !== 0) {
                const output = `Warning: command "${c.command}" exited with non-zero status code`;
                terminalOutput += output;
                if (options.streamOutput) {
                    process.stderr.write(output);
                }
                this.terminalOutput += terminalOutput;
                // Stop running commands
                break;
            }
        }
    }
    async createProcess(commandConfig, readyWhenStatus = [], color, cwd, env, isParallel, usePty = true, streamOutput = true, tty, envFile) {
        // The rust runCommand is always a tty, so it will not look nice in parallel and if we need prefixes
        // currently does not work properly in windows
        if (this.pseudoTerminal &&
            process.env.NX_NATIVE_COMMAND_RUNNER !== 'false' &&
            !commandConfig.prefix &&
            readyWhenStatus.length === 0 &&
            !isParallel &&
            usePty) {
            return createProcessWithPseudoTty(this.pseudoTerminal, commandConfig, color, cwd, env, streamOutput, tty, envFile);
        }
        return new RunningNodeProcess(commandConfig, color, cwd, env, readyWhenStatus, streamOutput, envFile);
    }
}
exports.SeriallyRunningTasks = SeriallyRunningTasks;
class RunningNodeProcess {
    constructor(commandConfig, color, cwd, env, readyWhenStatus, streamOutput = true, envFile) {
        this.readyWhenStatus = readyWhenStatus;
        this.terminalOutput = '';
        this.exitCallbacks = [];
        env = processEnv(color, cwd, env, envFile);
        this.command = commandConfig.command;
        this.terminalOutput = chalk.dim('> ') + commandConfig.command + '\r\n\r\n';
        if (streamOutput) {
            process.stdout.write(this.terminalOutput);
        }
        this.childProcess = (0, child_process_1.exec)(commandConfig.command, {
            maxBuffer: run_commands_impl_1.LARGE_BUFFER,
            env,
            cwd,
            windowsHide: false,
        });
        this.addListeners(commandConfig, streamOutput);
    }
    getResults() {
        return new Promise((res) => {
            this.onExit((code, terminalOutput) => {
                res({ code, terminalOutput });
            });
        });
    }
    onExit(cb) {
        this.exitCallbacks.push(cb);
    }
    send(message) {
        this.childProcess.send(message);
    }
    kill(signal) {
        return new Promise((res, rej) => {
            treeKill(this.childProcess.pid, signal, (err) => {
                if (err) {
                    rej(err);
                }
                else {
                    res();
                }
            });
        });
    }
    addListeners(commandConfig, streamOutput) {
        this.childProcess.stdout.on('data', (data) => {
            const output = addColorAndPrefix(data, commandConfig);
            this.terminalOutput += output;
            if (streamOutput) {
                process.stdout.write(output);
            }
            if (this.readyWhenStatus.length &&
                isReady(this.readyWhenStatus, data.toString())) {
                for (const cb of this.exitCallbacks) {
                    cb(0, this.terminalOutput);
                }
            }
        });
        this.childProcess.stderr.on('data', (err) => {
            const output = addColorAndPrefix(err, commandConfig);
            this.terminalOutput += output;
            if (streamOutput) {
                process.stderr.write(output);
            }
            if (this.readyWhenStatus.length &&
                isReady(this.readyWhenStatus, err.toString())) {
                for (const cb of this.exitCallbacks) {
                    cb(1, this.terminalOutput);
                }
            }
        });
        this.childProcess.on('error', (err) => {
            const output = addColorAndPrefix(err.toString(), commandConfig);
            this.terminalOutput += output;
            if (streamOutput) {
                process.stderr.write(output);
            }
            for (const cb of this.exitCallbacks) {
                cb(1, this.terminalOutput);
            }
        });
        this.childProcess.on('exit', (code) => {
            if (!this.readyWhenStatus.length || isReady(this.readyWhenStatus)) {
                for (const cb of this.exitCallbacks) {
                    cb(code, this.terminalOutput);
                }
            }
        });
    }
}
async function createProcessWithPseudoTty(pseudoTerminal, commandConfig, color, cwd, env, streamOutput = true, tty, envFile) {
    let terminalOutput = chalk.dim('> ') + commandConfig.command + '\r\n\r\n';
    if (streamOutput) {
        process.stdout.write(terminalOutput);
    }
    env = processEnv(color, cwd, env, envFile);
    const childProcess = pseudoTerminal.runCommand(commandConfig.command, {
        cwd,
        jsEnv: env,
        quiet: !streamOutput,
        tty,
    });
    childProcess.onOutput((output) => {
        terminalOutput += output;
    });
    return childProcess;
}
function addColorAndPrefix(out, config) {
    if (config.prefix) {
        out = out
            .split('\n')
            .map((l) => {
            let prefixText = config.prefix;
            if (config.prefixColor && chalk[config.prefixColor]) {
                prefixText = chalk[config.prefixColor](prefixText);
            }
            prefixText = chalk.bold(prefixText);
            return l.trim().length > 0 ? `${prefixText} ${l}` : l;
        })
            .join('\n');
    }
    if (config.color && chalk[config.color]) {
        out = chalk[config.color](out);
    }
    if (config.bgColor && chalk[config.bgColor]) {
        out = chalk[config.bgColor](out);
    }
    return out;
}
function calculateCwd(cwd, context) {
    if (!cwd)
        return context.root;
    if ((0, path_1.isAbsolute)(cwd))
        return cwd;
    return (0, path_1.join)(context.root, cwd);
}
/**
 * Env variables are processed in the following order:
 * - env option from executor options
 * - env file from envFile option if provided
 * - local env variables
 */
function processEnv(color, cwd, envOptionFromExecutor, envFile) {
    let localEnv = (0, npm_run_path_1.env)({ cwd: cwd ?? process.cwd() });
    localEnv = {
        ...process.env,
        ...localEnv,
    };
    if (process.env.NX_LOAD_DOT_ENV_FILES !== 'false' && envFile) {
        loadEnvVarsFile(envFile, localEnv);
    }
    let res = {
        ...localEnv,
        ...envOptionFromExecutor,
    };
    // need to override PATH to make sure we are using the local node_modules
    if (localEnv.PATH)
        res.PATH = localEnv.PATH; // UNIX-like
    if (localEnv.Path)
        res.Path = localEnv.Path; // Windows
    if (color) {
        res.FORCE_COLOR = `${color}`;
    }
    return res;
}
function isReady(readyWhenStatus = [], data) {
    if (data) {
        for (const readyWhenElement of readyWhenStatus) {
            if (data.toString().indexOf(readyWhenElement.stringToMatch) > -1) {
                readyWhenElement.found = true;
                break;
            }
        }
    }
    return readyWhenStatus.every((readyWhenElement) => readyWhenElement.found);
}
function loadEnvVarsFile(path, env = {}) {
    (0, task_env_1.unloadDotEnvFile)(path, env);
    const result = (0, task_env_1.loadAndExpandDotEnvFile)(path, env);
    if (result.error) {
        throw result.error;
    }
}
