"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* istanbul ignore file */
const chalk_1 = __importDefault(require("chalk"));
const bundle_esbuild_1 = require("../deploy/bundle-esbuild");
const ports_1 = require("../utils/ports");
const runLocalApp_1 = __importDefault(require("./runLocalApp"));
const log_1 = __importDefault(require("../log"));
const fs_1 = require("fs");
const paths_1 = require("../deploy/paths");
async function createDevServer(options) {
    var _a;
    // Clean up .edgio folder after previous command run
    (0, fs_1.rmSync)(paths_1.EDGIO_DIR, {
        recursive: true,
        force: true,
    });
    const onRebuild = async () => {
        console.log('> Recompile of router, config and edge functions... done.');
    };
    const routerReady = (0, bundle_esbuild_1.bundleRouterAndConfig)({ watch: { onRebuild } }).then(onRebuild);
    const appReady = ((_a = options === null || options === void 0 ? void 0 : options.command) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.run) ? (0, runLocalApp_1.default)(ports_1.jsPort, options) : Promise.resolve();
    return Promise.all([routerReady, appReady]).catch((e) => {
        log_1.default.error(chalk_1.default.red('Edgio encountered a fatal error while running your application.', e));
        process.exit(1);
    });
}
exports.default = createDevServer;
