"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeVersionWarning = exports.renameAsync = exports.terminalPrompt = exports.isWin = exports.printDuration = exports.onlyUnix = exports.rimraf = exports.npm = exports.killChildren = exports.cleanup = exports.setTmpDirectory = void 0;
const fs = __importStar(require("fs"));
const path_1 = require("path");
const util_1 = require("util");
const utils_1 = require("../utils");
const execa_1 = __importDefault(require("execa"));
const childrenProcesses = [];
let tmpDirectory = null;
function setTmpDirectory(dir) {
    tmpDirectory = dir;
    if (dir) {
        rimraf(dir);
        process.once('uncaughtException', () => cleanup(true));
        process.once('exit', () => cleanup());
        process.once('SIGINT', () => cleanup());
        process.once('SIGTERM', () => cleanup());
    }
}
exports.setTmpDirectory = setTmpDirectory;
function cleanup(didError = false) {
    if (tmpDirectory) {
        killChildren();
    }
    setTimeout(() => {
        if (tmpDirectory) {
            rimraf(tmpDirectory);
            tmpDirectory = null;
        }
        process.exit(didError ? 1 : 0);
    }, 200);
}
exports.cleanup = cleanup;
function killChildren() {
    childrenProcesses.forEach(p => p.kill('SIGINT'));
}
exports.killChildren = killChildren;
function npm(command, projectPath, stdio = 'ignore') {
    return __awaiter(this, void 0, void 0, function* () {
        const { stdout, stderr } = yield (0, execa_1.default)('npm', [command], {
            cwd: projectPath
        });
    });
}
exports.npm = npm;
function rimraf(dirPath) {
    if (fs.existsSync(dirPath)) {
        fs.readdirSync(dirPath).forEach(entry => {
            const entryPath = (0, path_1.join)(dirPath, entry);
            if (fs.lstatSync(entryPath).isDirectory()) {
                rimraf(entryPath);
            }
            else {
                fs.unlinkSync(entryPath);
            }
        });
        fs.rmdirSync(dirPath);
    }
}
exports.rimraf = rimraf;
function onlyUnix(str) {
    return isWin() ? str : '';
}
exports.onlyUnix = onlyUnix;
function printDuration(duration) {
    if (duration > 1000) {
        return `in ${(duration / 1000).toFixed(2)} s`;
    }
    else {
        const ms = parseFloat(duration.toFixed(3));
        return `in ${ms} ms`;
    }
}
exports.printDuration = printDuration;
function isWin() {
    return process.platform === 'win32';
}
exports.isWin = isWin;
function terminalPrompt() {
    return isWin() ? '>' : '$';
}
exports.terminalPrompt = terminalPrompt;
exports.renameAsync = (0, util_1.promisify)(fs.rename);
function nodeVersionWarning() {
    try {
        const v = process.version.replace('v', '').split('.');
        const major = parseInt(v[0], 10);
        const minor = parseInt(v[1], 10);
        if (major < 10) {
            (0, utils_1.logWarning)(`Your current version of Node is ${process.version}, however the recommendation is a minimum of Node 10.x LTS.`);
        }
    }
    catch (e) {
        return;
    }
}
exports.nodeVersionWarning = nodeVersionWarning;
//# sourceMappingURL=utils.js.map