firebase-tools
Version:
Command-Line Interface for Firebase
58 lines (57 loc) • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEmulatorEnvs = exports.start = void 0;
const net_1 = require("net");
const portUtils_1 = require("../portUtils");
const developmentServer_1 = require("./developmentServer");
const constants_1 = require("../constants");
const spawn_1 = require("../../init/spawn");
const developmentServer_2 = require("./developmentServer");
const types_1 = require("../types");
const config_1 = require("./config");
const projectPath_1 = require("../../projectPath");
const registry_1 = require("../registry");
const env_1 = require("../env");
async function start(options) {
var _a;
const hostname = constants_1.DEFAULT_HOST;
let port = (_a = options === null || options === void 0 ? void 0 : options.port) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_PORTS.apphosting;
while (!(await availablePort(hostname, port))) {
port += 1;
}
serve(port, options === null || options === void 0 ? void 0 : options.startCommand, options === null || options === void 0 ? void 0 : options.rootDirectory);
return { hostname, port };
}
exports.start = start;
async function serve(port, startCommand, backendRelativeDir) {
backendRelativeDir = backendRelativeDir !== null && backendRelativeDir !== void 0 ? backendRelativeDir : "./";
const backendRoot = (0, projectPath_1.resolveProjectPath)({}, backendRelativeDir);
const apphostingLocalConfig = await (0, config_1.getLocalAppHostingConfiguration)(backendRoot);
const environmentVariablesAsRecord = {};
for (const env of apphostingLocalConfig.environmentVariables) {
environmentVariablesAsRecord[env.variable] = env.value;
}
const environmentVariablesToInject = Object.assign(Object.assign(Object.assign({}, getEmulatorEnvs()), environmentVariablesAsRecord), { PORT: port.toString() });
if (startCommand) {
developmentServer_2.logger.logLabeled("BULLET", types_1.Emulators.APPHOSTING, `running custom start command: '${startCommand}'`);
await (0, spawn_1.spawnWithCommandString)(startCommand, backendRoot, environmentVariablesToInject);
return;
}
const detectedStartCommand = await (0, developmentServer_1.detectStartCommand)(backendRoot);
developmentServer_2.logger.logLabeled("BULLET", types_1.Emulators.APPHOSTING, `starting app with: '${detectedStartCommand}'`);
await (0, spawn_1.spawnWithCommandString)(detectedStartCommand, backendRoot, environmentVariablesToInject);
}
function availablePort(host, port) {
return (0, portUtils_1.checkListenable)({
address: host,
port,
family: (0, net_1.isIPv4)(host) ? "IPv4" : "IPv6",
});
}
function getEmulatorEnvs() {
const envs = {};
const emulatorInfos = registry_1.EmulatorRegistry.listRunningWithInfo().filter((emulator) => emulator.name !== types_1.Emulators.APPHOSTING);
(0, env_1.setEnvVarsForEmulators)(envs, emulatorInfos);
return envs;
}
exports.getEmulatorEnvs = getEmulatorEnvs;