#!/usr/bin/env node
"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 });
const commander_1 = require("commander");
const scheduler_1 = require("./utils/scheduler");
const cmd_1 = require("./cmd");
const logger_1 = require("./utils/logger");
const check_stage_1 = require("./cmd/check-stage");
const check_service_1 = require("./cmd/check-service");
const build_1 = require("./cmd/build");
const chalk_1 = __importDefault(require("chalk"));
const platform_1 = require("./utils/platform");
const package_1 = require("./cmd/package");
const test_1 = require("./cmd/test");
const deploy_1 = require("./cmd/deploy");
const logger = new logger_1.Logger();
const scheduler = new scheduler_1.RecompilationScheduler(logger);
const program = new commander_1.Command();
program.version('0.0.15-alpha');
program
    .command('start')
    .option('-i, --interactive', 'interactively choose microservices', false)
    .option('-d, --discrete', 'start the app in background without logging in console', false)
    .option('-p <port>, --port <port>', 'start mila server on given port', 4545)
    .option('-n, --no-recompile', 'avoid recompiling dependency graph before starting microservices')
    .option('-s <service>, --service <service>', 'the service for which you want to start', false)
    .description('start microlambda services')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const options = {
            recompile: cmd.recompile,
            service: cmd.S,
            defaultPort: cmd.P || 3001,
            interactive: cmd.interactive,
        };
        logger.log('cmd').debug(options);
        yield cmd_1.start(scheduler, options, logger);
    }
    catch (e) {
        console.error(`\n${chalk_1.default.bgRedBright('Fatal Error')}`, e);
        process.exit(1);
    }
}));
program
    .command('check-stage <stage>')
    .description('check if stage is allowed')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        yield check_stage_1.checkStage(cmd);
    }
    catch (e) {
        console.error(`\n${chalk_1.default.bgRedBright('Fatal Error')}`, e);
        process.exit(1);
    }
}));
program
    .command('check-service <service>')
    .description('check if service is valid')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        yield check_service_1.checkService(cmd);
    }
    catch (e) {
        console.error(`\n${chalk_1.default.bgRedBright('Fatal Error')}`, e);
        process.exit(1);
    }
}));
program
    .command('build')
    .option('--no-bootstrap', 'skip bootstrapping dependencies', false)
    .option('--only-self', 'skip compiling service dependencies', false)
    .option('-s <service>, --service <service>', 'the service you want to build', false)
    .description('compile packages and services')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        yield build_1.build(cmd, scheduler, logger);
    }
    catch (e) {
        console.error(`\n${chalk_1.default.bgRedBright('Fatal Error')}`, e);
        process.exit(1);
    }
}));
program
    .command('package')
    .option('--no-bootstrap', 'skip bootstrapping dependencies', false)
    .option('--no-recompile', 'skip package and service recompilation', false)
    .option('-c, --concurrency', 'defines how much threads can be used for parallel tasks', platform_1.getDefaultThreads())
    .option('-s <service>, --service <service>', 'the service you want to package', false)
    .description('package services source code')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    yield package_1.packagr(cmd, logger, scheduler);
}));
program
    .command('deploy')
    .option('--no-bootstrap', 'skip bootstrapping dependencies', false)
    .option('--no-recompile', 'skip package and service recompilation', false)
    .option('-c, --concurrency', 'defines how much threads can be used for parallel tasks', platform_1.getDefaultThreads())
    .option('--no-package', 'skip bundling service deployment package', false)
    .option('-s <service>, --service <service>', 'the service you want to deploy', null)
    .option('-e <stage>, --stage <stage>', 'target stage for deployment', null)
    .option('--no-prompt', 'skip asking user confirmation before deploying', false)
    .description('deploy services to AWS')
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    yield deploy_1.deploy(cmd, logger, scheduler);
}));
program
    .command('test')
    .description('test microlambda services')
    .option('--no-bootstrap', 'skip reinstalling dependencies before starting microservices', false)
    .option('--no-recompile', 'skip recompiling dependency graph before starting microservices', false)
    .option('--only-self', 'only recompile target services', false)
    .option('--unit', 'only run unit tests', false)
    .option('--functional', 'only run functional tests', false)
    .option('-c <jobs>, --concurrency <jobs>', 'set maximum concurrent services being tested', null)
    .option('-s <service>, --service <service>', 'the service for which you want to test', null)
    .action((cmd) => __awaiter(void 0, void 0, void 0, function* () {
    yield test_1.runTests(cmd, scheduler, logger);
}));
(() => __awaiter(void 0, void 0, void 0, function* () { return program.parseAsync(process.argv); }))();
//# sourceMappingURL=index.js.map