"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.build = exports.typeCheck = exports.beforeBuild = void 0;
const scheduler_1 = require("../utils/scheduler");
const start_1 = require("./start");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const lerna_1 = require("../lerna");
exports.beforeBuild = (cmd, scheduler, logger, acceptPackages = false) => __awaiter(void 0, void 0, void 0, function* () {
    const { graph, projectRoot } = yield start_1.init(logger, scheduler);
    graph.enableAll();
    const nodes = acceptPackages ? graph.getNodes() : graph.getServices();
    const service = nodes.find((s) => s.getName() === cmd.S);
    if (cmd.S && !service) {
        console.error(chalk_1.default.red('Unknown service', cmd.S));
        process.exit(1);
    }
    if (cmd.bootstrap) {
        yield start_1.lernaBootstrap(graph, logger);
    }
    return { projectRoot, graph, service };
});
exports.typeCheck = (scheduler, target, onlySelf, force) => __awaiter(void 0, void 0, void 0, function* () {
    return new Promise((resolve, reject) => {
        const spinners = new Map();
        const onNext = (evt) => {
            if (evt.type === scheduler_1.RecompilationEventType.TYPE_CHECK_IN_PROGRESS) {
                const spinner = ora_1.default(`Compiling ${evt.node.getName()}`);
                spinner.start();
                spinners.set(evt.node.getName(), spinner);
            }
            else if (evt.type === scheduler_1.RecompilationEventType.TYPE_CHECK_SUCCESS) {
                const spinner = spinners.get(evt.node.getName());
                spinner.text = `${evt.node.getName()} compiled ${chalk_1.default.gray(evt.took + 'ms')}`;
                spinner.succeed();
            }
            else if (evt.type === scheduler_1.RecompilationEventType.TYPE_CHECK_FAILURE) {
                const spinner = spinners.get(evt.node.getName());
                spinner.fail(`Error compiling ${evt.node.getName()}`);
                evt.node.tscLogs.forEach((l) => console.error(l));
                return reject();
            }
        };
        const onError = (evt) => {
            const spinner = spinners.get(evt.node.getName());
            spinner.fail(`Error compiling ${evt.node.getName()}`);
            evt.logs.forEach((l) => console.error(l));
            return reject();
        };
        const onComplete = () => {
            return resolve();
        };
        if (target instanceof lerna_1.LernaNode) {
            scheduler.buildOne(target, onlySelf, force).subscribe(onNext, onError, onComplete);
        }
        else {
            scheduler.buildAll(target, onlySelf, force).subscribe(onNext, onError, onComplete);
        }
    });
});
exports.build = (cmd, scheduler, logger) => __awaiter(void 0, void 0, void 0, function* () {
    const { graph, service } = yield exports.beforeBuild(cmd, scheduler, logger, true);
    try {
        yield exports.typeCheck(scheduler, cmd.S ? service : graph, cmd.onlySelf, true);
        console.info('\nSuccessfully built ✨');
        process.exit(0);
    }
    catch (e) {
        process.exit(1);
    }
});
//# sourceMappingURL=build.js.map