UNPKG

@nxextensions/firebase

Version:

An Nx plugin for firebase applications that would like to run the emulators in conjunction with their app

76 lines 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const firebase_1 = require("../../utils/firebase"); const server_1 = require("../../utils/server"); const rln = require("readline"); const runExecutor = async (options, context) => { options = validateAndNormalizeOptions(options); let killEmulators; try { const firebaseCommand = getFirebaseCommand(options.only, options.includeHosting, options.saveDataDir); killEmulators = await (0, firebase_1.startEmulators)(firebaseCommand, context, options.saveDataDir); initProcessListeners(killEmulators); await (0, server_1.startServer)(options.baseServeTarget, context); return { success: true, }; } finally { killEmulators && (await killEmulators()); } }; function validateAndNormalizeOptions(options) { validateOptions(options); return normalizeOptions(options); } function validateOptions(options) { var _a; if (!options.baseServeTarget) { throw new Error('A base server target is required.'); } if (((_a = options.only) === null || _a === void 0 ? void 0 : _a.length) && options.includeHosting) { console.warn('Using --includeHosting causes the command to ignore --only.'); } } function normalizeOptions(options) { var _a, _b, _c; options !== null && options !== void 0 ? options : (options = {}); (_a = options.only) !== null && _a !== void 0 ? _a : (options.only = options.includeHosting ? undefined : []); (_b = options.includeHosting) !== null && _b !== void 0 ? _b : (options.includeHosting = false); (_c = options.saveDataDir) !== null && _c !== void 0 ? _c : (options.saveDataDir = undefined); return options; } function getFirebaseCommand(emulators, includeHosting, saveDataDir) { let command = 'firebase emulators:start'; if (!includeHosting) { let onlyPart = ' --only '; emulators.forEach((emulator) => { if (emulator !== 'hosting') { onlyPart += `${emulator},`; } }); onlyPart = onlyPart.substring(0, onlyPart.length - 1); command += onlyPart; } if (saveDataDir) { command += ` --import ${saveDataDir} --export-on-exit ${saveDataDir}`; } return command; } function initProcessListeners(cb) { if (process.platform === 'win32') { const rl = rln.createInterface({ input: process.stdin, output: process.stdout, }); rl.on('SIGINT', () => { process.emit('SIGINT'); }); } process.on('SIGINT', async () => { await cb(); process.exit(); }); } exports.default = runExecutor; //# sourceMappingURL=executor.js.map