"use strict";
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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var os = require("os");
var packager_1 = require("../tools/packager");
var isWindows = process.platform === "win32";
var isMac = process.platform === "darwin";
module.exports = {
    description: "Checks your dev environment for dependencies.",
    run: function (toolbox) {
        var _a, _b, _c, _d;
        return __awaiter(this, void 0, void 0, function () {
            function packageInfo(packagerName) {
                return __awaiter(this, void 0, void 0, function () {
                    return __generator(this, function (_a) {
                        switch (_a.label) {
                            case 0: return [4 /*yield*/, packager_1.packager.list({ packagerName: packagerName, global: true })];
                            case 1: return [2 /*return*/, (_a.sent()).map(function (nameAndVersion) { return [
                                    column1("  " + nameAndVersion[0]),
                                    column2(nameAndVersion[1]),
                                    column3(""),
                                ]; })];
                        }
                    });
                });
            }
            function expoWorkflowInfo() {
                var iosFound = isFile("".concat(directory, "\\ios\\.xcodeproj"));
                var androidFound = isFile("".concat(directory, "\\android\\.gradle"));
                return iosFound || androidFound;
            }
            var _e, separator, isFile, _f, run, which, _g, colors, info, table, padEnd, meta, column1, column2, column3, platform, arch, cpus, firstCpu, cpu, cores, directory, nodePath, nodeVersion, npmPath, npmVersion, _h, yarnPath, yarnVersion, _j, pnpmPath, pnpmVersion, _k, nodeInfo, npmInfo, yarnInfo, pnpmInfo, npmPackages, _l, yarnPackages, _m, pnpmPackages, haveGlobalPackages, expoVersionCmd, expoVersion, expoWorkflow, _1, expoInfo, ignitePath, igniteSrcPath, igniteVersion, igniteTable, androidPath, javaPath, javaVersionCmd, javaVersion, _o, _2, xcodePath, xcodeVersion, _p, cocoaPodsPath, cocoaPodsVersion, _q, gitPath, gitVersion, _r, gitInfo;
            return __generator(this, function (_s) {
                switch (_s.label) {
                    case 0:
                        _e = toolbox.filesystem, separator = _e.separator, isFile = _e.isFile, _f = toolbox.system, run = _f.run, which = _f.which, _g = toolbox.print, colors = _g.colors, info = _g.info, table = _g.table, padEnd = toolbox.strings.padEnd, meta = toolbox.meta;
                        column1 = function (label, length) {
                            if (length === void 0) { length = 16; }
                            return padEnd(label || "", length);
                        };
                        column2 = function (label) { return colors.yellow(padEnd(label || "-", 10)); };
                        column3 = function (label) { return colors.muted(label); };
                        platform = process.platform;
                        arch = os.arch();
                        cpus = os.cpus() || [];
                        firstCpu = cpus[0] || { model: undefined };
                        cpu = "".concat(firstCpu.model);
                        cores = "".concat(cpus.length, " cores");
                        directory = "".concat(process.cwd());
                        info(colors.cyan("System"));
                        table([
                            [column1("platform"), column2(platform), column3("")],
                            [column1("arch"), column2(arch), column3("")],
                            [column1("cpu"), column2(cores), column3(cpu)],
                            [column1("directory"), column2(directory.split(separator).pop()), column3(directory)],
                        ]);
                        nodePath = which("node");
                        return [4 /*yield*/, run("node --version", { trim: true })];
                    case 1:
                        nodeVersion = (_s.sent()).replace("v", "");
                        npmPath = which("npm");
                        _h = npmPath;
                        if (!_h) return [3 /*break*/, 3];
                        return [4 /*yield*/, run("npm --version", { trim: true })];
                    case 2:
                        _h = (_s.sent());
                        _s.label = 3;
                    case 3:
                        npmVersion = _h;
                        yarnPath = which("yarn");
                        _j = yarnPath;
                        if (!_j) return [3 /*break*/, 5];
                        return [4 /*yield*/, run("yarn --version", { trim: true })];
                    case 4:
                        _j = (_s.sent());
                        _s.label = 5;
                    case 5:
                        yarnVersion = _j;
                        pnpmPath = which("pnpm");
                        _k = pnpmPath;
                        if (!_k) return [3 /*break*/, 7];
                        return [4 /*yield*/, run("pnpm --version", { trim: true })];
                    case 6:
                        _k = (_s.sent());
                        _s.label = 7;
                    case 7:
                        pnpmVersion = _k;
                        nodeInfo = [column1("node"), column2(nodeVersion), column3(nodePath)];
                        npmInfo = [column1("npm"), column2(npmVersion), column3(npmPath || "not installed")];
                        yarnInfo = [column1("yarn"), column2(yarnVersion), column3(yarnPath || "not installed")];
                        pnpmInfo = [column1("pnpm"), column2(pnpmVersion), column3(pnpmPath || "not installed")];
                        if (!npmPath) return [3 /*break*/, 9];
                        return [4 /*yield*/, packageInfo("npm")];
                    case 8:
                        _l = _s.sent();
                        return [3 /*break*/, 10];
                    case 9:
                        _l = [];
                        _s.label = 10;
                    case 10:
                        npmPackages = _l;
                        if (!yarnPath) return [3 /*break*/, 12];
                        return [4 /*yield*/, packageInfo("yarn")];
                    case 11:
                        _m = _s.sent();
                        return [3 /*break*/, 13];
                    case 12:
                        _m = [];
                        _s.label = 13;
                    case 13:
                        yarnPackages = _m;
                        pnpmPackages = pnpmPath
                            ? [[column1("  "), column2("<no pnpm global package info available>"), column3("")]]
                            : [];
                        haveGlobalPackages = npmPackages.length > 0 || yarnPackages.length > 0;
                        expoVersionCmd = "npm list --depth 0 expo 2>&1";
                        _s.label = 14;
                    case 14:
                        _s.trys.push([14, 16, , 17]);
                        return [4 /*yield*/, run(expoVersionCmd)];
                    case 15:
                        expoVersion = (_b = (_a = (_s.sent())) === null || _a === void 0 ? void 0 : _a.match(/expo@(.*)/)) === null || _b === void 0 ? void 0 : _b.slice(-1)[0];
                        expoWorkflow = expoWorkflowInfo() ? "bare" : "managed";
                        return [3 /*break*/, 17];
                    case 16:
                        _1 = _s.sent();
                        expoVersion = "-";
                        expoWorkflow = "not installed";
                        return [3 /*break*/, 17];
                    case 17:
                        expoInfo = [column1("expo"), column2(expoVersion), column3(expoWorkflow)];
                        info("");
                        info(colors.cyan("JavaScript".concat(haveGlobalPackages ? " (and globally-installed packages)" : "")));
                        table(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([
                            nodeInfo,
                            npmInfo
                        ], npmPackages, true), [
                            yarnInfo
                        ], false), yarnPackages, true), [
                            pnpmInfo
                        ], false), pnpmPackages, true), [
                            expoInfo,
                        ], false));
                        ignitePath = which("ignite");
                        igniteSrcPath = "".concat(meta.src);
                        igniteVersion = meta.version();
                        info("");
                        info(colors.cyan("Ignite"));
                        igniteTable = [];
                        igniteTable.push([column1("ignite-cli"), column2(igniteVersion), column3(ignitePath)]);
                        igniteTable.push([
                            column1("ignite src"),
                            column2(igniteSrcPath.split(separator).pop()),
                            column3(igniteSrcPath),
                        ]);
                        table(igniteTable);
                        androidPath = process.env.ANDROID_HOME;
                        javaPath = which("java");
                        javaVersionCmd = "java -version 2>&1";
                        _s.label = 18;
                    case 18:
                        _s.trys.push([18, 21, , 22]);
                        _o = javaPath;
                        if (!_o) return [3 /*break*/, 20];
                        return [4 /*yield*/, run(javaVersionCmd)];
                    case 19:
                        _o = ((_d = (_c = (_s.sent())) === null || _c === void 0 ? void 0 : _c.match(/"(.*)"/)) === null || _d === void 0 ? void 0 : _d.slice(-1)[0]);
                        _s.label = 20;
                    case 20:
                        javaVersion = _o;
                        return [3 /*break*/, 22];
                    case 21:
                        _2 = _s.sent();
                        javaVersion = "-";
                        javaPath = "not installed";
                        return [3 /*break*/, 22];
                    case 22:
                        info("");
                        info(colors.cyan("Android"));
                        table([
                            [column1("java"), column2(javaVersion), column3(javaPath)],
                            [column1("android home"), column2("-"), column3(androidPath)],
                        ]);
                        if (!isMac) return [3 /*break*/, 28];
                        xcodePath = which("xcodebuild");
                        _p = xcodePath;
                        if (!_p) return [3 /*break*/, 24];
                        return [4 /*yield*/, run("xcodebuild -version", { trim: true })];
                    case 23:
                        _p = (_s.sent()).split(/\s/)[1];
                        _s.label = 24;
                    case 24:
                        xcodeVersion = _p;
                        info("");
                        info(colors.cyan("iOS"));
                        table([[column1("xcode"), column2(xcodeVersion)]]);
                        cocoaPodsPath = which("pod") || "";
                        if (!cocoaPodsPath) return [3 /*break*/, 26];
                        return [4 /*yield*/, run("pod --version", { trim: true })];
                    case 25:
                        _q = _s.sent();
                        return [3 /*break*/, 27];
                    case 26:
                        _q = "Not installed";
                        _s.label = 27;
                    case 27:
                        cocoaPodsVersion = _q;
                        table([[column1("cocoapods"), column2(cocoaPodsVersion), column3(cocoaPodsPath)]]);
                        _s.label = 28;
                    case 28:
                        // -=-=-=- windows -=-=-=-
                        // TODO: what can we check on Windows?
                        if (isWindows) {
                            // info('')
                            // info(colors.cyan('Windows'))
                            // table([])
                        }
                        // -=-=-=- tools -=-=-=-
                        info("");
                        info(colors.cyan("Tools"));
                        gitPath = which("git");
                        _r = gitPath;
                        if (!_r) return [3 /*break*/, 30];
                        return [4 /*yield*/, run("git --version", { trim: true })];
                    case 29:
                        _r = (_s.sent());
                        _s.label = 30;
                    case 30:
                        gitVersion = _r;
                        gitInfo = [column1("git"), column2(gitVersion), column3(gitPath || "not installed")];
                        table([gitInfo]);
                        return [2 /*return*/];
                }
            });
        });
    },
};
//# sourceMappingURL=doctor.js.map