"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const ascii_1 = require("./utils/ascii");
const get_project_root_1 = require("./utils/get-project-root");
const load_config_1 = require("./config/load-config");
const get_lerna_graph_1 = require("./utils/get-lerna-graph");
const logger_1 = require("./utils/logger");
const interactive_1 = require("./utils/interactive");
const logs_1 = require("./utils/logs");
const external_binaries_1 = require("./utils/external-binaries");
exports.start = (scheduler, options) => __awaiter(void 0, void 0, void 0, function* () {
    ascii_1.showOff();
    logger_1.log.info('Starting up the app');
    const projectRoot = get_project_root_1.getProjectRoot();
    logger_1.log.debug('Loading config');
    const config = load_config_1.loadConfig();
    yield external_binaries_1.verifyBinaries(config.compilationMode, projectRoot);
    scheduler.setMode(config.compilationMode);
    logger_1.log.debug(config);
    logger_1.log.info('Parsing lerna dependency graph', projectRoot);
    const graph = yield get_lerna_graph_1.getLernaGraph(projectRoot, config, options.defaultPort);
    yield graph.bootstrap().catch((e) => {
        logger_1.log.error(e);
        logger_1.log.error('Error installing microservices dependencies. Run in verbose mode (export MILA_DEBUG=*) for more infos.');
        process.exit(1);
    });
    logger_1.log.debug('Services excluded by config', config.noStart);
    const enabledServices = graph.getServices().filter((s) => !config.noStart.includes(s.getName()));
    logger_1.log.debug('Enabled services', enabledServices.map((s) => s.getName()));
    let chosenServices = [];
    if (options.interactive) {
        yield interactive_1.interactive(enabledServices, 'Please select the microservices you wants to start').then((s) => (chosenServices = s));
    }
    else {
        chosenServices = enabledServices;
    }
    logger_1.log.debug('Chosen services', chosenServices.map((s) => s.getName()));
    chosenServices.forEach((s) => s.enable());
    graph.enableNodes();
    logger_1.log.debug('Enabled nodes', graph
        .getNodes()
        .filter((n) => n.isEnabled())
        .map((n) => n.getName()));
    logs_1.recreateLogDirectory(projectRoot);
    logger_1.log.info(`Found ${chosenServices.length} services`);
    logger_1.log.info('Starting services');
    logger_1.log.debug(chosenServices);
    yield scheduler.startProject(graph, options.recompile);
    graph
        .getNodes()
        .filter((n) => n.isEnabled())
        .forEach((n) => n.watch(scheduler));
    process.on('SIGINT', () => __awaiter(void 0, void 0, void 0, function* () {
        logger_1.log.warn('SIGINT signal received');
        yield scheduler.stopProject(graph);
        process.exit();
    }));
});
//# sourceMappingURL=start.js.map