"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkIsMetroRunning = exports.startMetro = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const path_1 = tslib_1.__importDefault(require("path"));
const utils_1 = require("./utils");
const startServerInNewWindow_1 = require("@react-native-community/cli-platform-android/build/commands/buildAndroid/startServerInNewWindow");
const cli_tools_1 = require("@react-native-community/cli-tools");
async function startMetro(resetCache = false, port = '8081') {
    (0, startServerInNewWindow_1.startServerInNewWindow)(Number(port), (0, cli_tools_1.getDefaultUserTerminal)(), path_1.default.join((0, utils_1.getProjectRootDir)(), 'node_modules/react-native'));
    while (!(await checkIsMetroRunning(port))) {
        await (0, utils_1.sleep)(100);
    }
}
exports.startMetro = startMetro;
async function checkIsMetroRunning(port = '8081') {
    try {
        await axios_1.default.get(`http://localhost:${port}`);
        return true;
    }
    catch (ex) {
        if (ex === null || ex === void 0 ? void 0 : ex.message.includes('ECONNREFUSED')) {
            return false;
        }
        throw ex;
    }
}
exports.checkIsMetroRunning = checkIsMetroRunning;
