"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(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 HotClientServer_1 = require("./HotClientServer");
const HotNodeServer_1 = require("./HotNodeServer");
const cli_core_1 = require("@bluebase/cli-core");
const chokidar_1 = __importDefault(require("chokidar"));
exports.default = (opts) => __awaiter(this, void 0, void 0, function* () {
    let hotClient = new HotClientServer_1.HotClientServer(opts);
    let hotServer = new HotNodeServer_1.HotNodeServer(opts);
    hotClient.start();
    hotServer.start();
    // Any changes to our webpack bundleConfigs should restart the development devServer.
    const watcher = chokidar_1.default.watch([
        cli_core_1.Utils.fromProjectRoot('bluebase'),
    ]);
    watcher.on('ready', () => {
        watcher.on('change', () => __awaiter(this, void 0, void 0, function* () {
            cli_core_1.Utils.logger.log({
                label: '@bluebase/cli/web',
                level: 'warn',
                message: 'Project build configuration has changed. Restarting the development devServer...',
            });
            yield hotClient.dispose();
            yield hotServer.dispose();
            Object.keys(require.cache).forEach((modulePath) => {
                if (modulePath.indexOf('bluebase') !== -1) {
                    delete require.cache[modulePath];
                }
            });
            hotClient = new HotClientServer_1.HotClientServer(opts);
            hotServer = new HotNodeServer_1.HotNodeServer(opts);
            hotClient.start();
            hotServer.start();
        }));
    });
    // If we receive a kill cmd then we will first try to dispose our listeners.
    process.on('SIGTERM', () => {
        if (hotClient) {
            hotClient.dispose().then(() => {
                if (hotServer) {
                    hotClient.dispose().then(() => {
                        process.exit(0);
                    });
                }
            });
        }
    });
});
//# sourceMappingURL=index.js.map