/* lib/terminal/commands/library/typescript - Executes TypeScript type checks. */ import error from "../../utilities/error.js";
import log from "../../utilities/log.js";
import node from "../../utilities/node.js";
import vars from "../../utilities/vars.js";
const typescript = function terminal_commands_library_typescript(typePath, callback) {
    const errorOut = function terminal_commands_library_typescript_stat_errorOut(message, errorObject) {
        error([
            message
        ], errorObject);
        process.exit(1);
    };
    if (typeof typePath !== "string" || typePath.length < 1) {
        typePath = vars.path.project;
    }
    node.fs.stat(typePath, function terminal_commands_library_typescript_stat(statError, stat) {
        if (statError === null) {
            const dir = stat.isDirectory();
            if (dir === true) {
                const command = `npx tsc --pretty --noEmit --project ${vars.path.project}tsconfig.json`, args = {
                    cwd: typePath
                };
                node.child_process.exec(command, args, function terminal_commands_library_typescript_stat_exec(err, stdout) {
                    const control = "\u001b[91m";
                    if (stdout !== "") {
                        let compileErrors = stdout.slice(stdout.indexOf("Found"));
                        if (stdout.indexOf(` ${control}error${vars.text.none} `) > -1) {
                            errorOut([
                                "TypeScript reported warnings.",
                                stdout
                            ].join(node.os.EOL), null);
                            return;
                        }
                        compileErrors = compileErrors.slice(0, compileErrors.indexOf("error") - 1).replace(/\D+/g, "");
                        if (vars.environment.command === "build") {
                            log([
                                stdout
                            ]);
                            callback("TypeScript", [
                                compileErrors
                            ], false);
                        } else {
                            callback("TypeScript", [
                                stdout
                            ], true);
                        }
                    } else {
                        callback("TypeScript", [
                            `${vars.text.green + vars.text.bold}TypeScript type validation completed without warnings.${vars.text.none}`
                        ], false);
                    }
                });
            } else {
                errorOut("The TypeScript command is only supporting directory type file system paths.", null);
            }
        } else {
            errorOut(`Error accessing path: ${typePath}`, statError);
        }
    });
};
export default typescript;
