"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.packagr = exports.packageService = exports.beforePackage = void 0;
const scheduler_1 = require("../utils/scheduler");
const build_1 = require("./build");
const chalk_1 = __importDefault(require("chalk"));
const spinnies_1 = __importDefault(require("spinnies"));
const platform_1 = require("../utils/platform");
const lerna_1 = require("../lerna");
exports.beforePackage = (cmd, scheduler, logger) => __awaiter(void 0, void 0, void 0, function* () {
    const concurrency = cmd.C ? platform_1.getThreads(Number(cmd.C)) : platform_1.getDefaultThreads();
    const { projectRoot, graph, service } = yield build_1.beforeBuild(cmd, scheduler, logger);
    const target = cmd.S ? service : graph;
    if (cmd.recompile) {
        try {
            console.info('\nBuilding dependency graph\n');
            yield build_1.typeCheck(scheduler, target, cmd.onlySelf, false);
        }
        catch (e) {
            process.exit(1);
        }
    }
    return { projectRoot, concurrency, graph, service };
});
exports.packageService = (scheduler, concurrency, target) => {
    return new Promise((resolve, reject) => {
        const spinnies = new spinnies_1.default({
            failColor: 'white',
            succeedColor: 'white',
            spinnerColor: 'cyan',
        });
        const onNext = (evt) => {
            switch (evt.type) {
                case scheduler_1.RecompilationEventType.PACKAGE_IN_PROGRESS: {
                    spinnies.add(evt.node.getName(), { text: `Packaging ${evt.node.getName()}` });
                    break;
                }
                case scheduler_1.RecompilationEventType.PACKAGE_SUCCESS: {
                    spinnies.succeed(evt.node.getName(), {
                        text: `${evt.node.getName()} packaged ${chalk_1.default.cyan(evt.megabytes + 'MB')} ${chalk_1.default.gray(evt.took + 'ms')}`,
                    });
                    break;
                }
            }
        };
        const onError = (evt) => __awaiter(void 0, void 0, void 0, function* () {
            spinnies.fail(evt.node.getName(), { text: `Error packaging ${evt.node.getName()}` });
            spinnies.stopAll();
            console.log('\n');
            evt.logs.forEach((l) => {
                console.info(chalk_1.default.bold(evt.node.getName()));
                console.log(chalk_1.default.red(l));
            });
            return reject(evt);
        });
        const onComplete = () => {
            return resolve();
        };
        scheduler.setConcurrency(concurrency);
        if (target instanceof lerna_1.Service) {
            scheduler.packageOne(target).subscribe(onNext, onError, onComplete);
        }
        else {
            scheduler.packageAll(target).subscribe(onNext, onError, onComplete);
        }
    });
};
exports.packagr = (cmd, logger, scheduler) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const { concurrency, graph, service } = yield exports.beforePackage(cmd, scheduler, logger);
        console.info('\nPackaging services\n');
        yield exports.packageService(scheduler, concurrency, cmd.S ? service : graph);
        console.info('\nSuccessfully packaged 📦');
        process.exit(0);
    }
    catch (e) {
        console.error(e);
        process.exit(1);
    }
});
//# sourceMappingURL=package.js.map