@nxextensions/firebase
Version:
An Nx plugin for firebase applications that would like to run the emulators in conjunction with their app
155 lines • 6.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNodesV2 = void 0;
exports.createNodesInternal = createNodesInternal;
const devkit_1 = require("@nx/devkit");
const fs_1 = require("fs");
const file_hasher_1 = require("nx/src/hasher/file-hasher");
const path_1 = require("path");
const cache_directory_1 = require("nx/src/utils/cache-directory");
const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
const lock_file_1 = require("nx/src/plugins/js/lock-file/lock-file");
function readTargetsCache(cachePath) {
return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {};
}
function writeTargetsToCache(cachePath, targets) {
(0, devkit_1.writeJsonFile)(cachePath, targets);
}
const firebaseConfigGlob = '**/firebase.json';
const pmc = (0, devkit_1.getPackageManagerCommand)();
exports.createNodesV2 = [
firebaseConfigGlob,
async (configFiles, options, context) => {
const optionsHash = (0, file_hasher_1.hashObject)(options);
const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `firebase-${optionsHash}.hash`);
const targetsCache = readTargetsCache(cachePath);
try {
return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache), configFiles, options, context);
}
finally {
writeTargetsToCache(cachePath, targetsCache);
}
},
];
async function createNodesInternal(configFile, options, context, targetsCache) {
options = normalizePluginOptions(options);
const projectRoot = (0, path_1.dirname)(configFile);
const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot));
if (!siblingFiles.includes('package.json') &&
!siblingFiles.includes('project.json')) {
return {};
}
// TODO: fail point
const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [(0, lock_file_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]);
targetsCache[hash] = await buildFirebaseTargets(configFile, projectRoot, options, context);
const { targets, metadata } = targetsCache[hash];
const project = {
projectType: 'application',
targets,
metadata,
};
return {
projects: {
[projectRoot]: project,
},
};
}
function normalizePluginOptions(options) {
var _a, _b, _c;
options !== null && options !== void 0 ? options : (options = {});
(_a = options.serveTargetName) !== null && _a !== void 0 ? _a : (options.serveTargetName = 'serve-firebase');
(_b = options.deployTargetName) !== null && _b !== void 0 ? _b : (options.deployTargetName = 'deploy');
(_c = options.includeHosting) !== null && _c !== void 0 ? _c : (options.includeHosting = false);
return options;
}
async function buildFirebaseTargets(configFile, projectRoot, options, context) {
const firebaseConfig = (0, devkit_1.readJsonFile)((0, path_1.join)(context.workspaceRoot, configFile));
const targets = {};
if (!firebaseConfig) {
return {};
}
const baseServeTarget = await getBaseServeTarget(configFile, context);
targets[options.deployTargetName] = {
executor: '@nxextensions/firebase:deploy',
dependsOn: ['build'],
options: {
cwd: projectRoot,
},
parallelism: false,
metadata: {
description: "Runs 'firebase deploy' on your application",
technologies: ['firebase'],
help: {
command: `${pmc.exec} firebase deploy`,
example: {
args: ['--only hosting'],
},
},
},
};
if (!firebaseConfig.emulators) {
return { targets };
}
const onlyEmulators = getEmulatorsFromConfig(firebaseConfig);
const fullTargetName = `${baseServeTarget.split(':')[0]}:${options.serveTargetName}`;
targets[options.serveTargetName] = {
executor: '@nxextensions/firebase:serve',
options: {
cwd: projectRoot,
baseServeTarget,
only: onlyEmulators,
includeHosting: options.includeHosting,
saveDataDir: options.saveDataDirectory
? options.saveDataDirectory
: undefined,
},
parallelism: false,
metadata: {
description: 'Runs the firebase emulators alongside your application',
technologies: ['firebase'],
help: {
command: `${pmc.exec} nx run ${fullTargetName} --help`,
example: {
args: ['--only auth,database', '--saveDataDir ./firebase/data'],
},
},
},
};
return { targets };
}
function getEmulatorsFromConfig(firebaseConfig) {
const emulatorConfig = firebaseConfig.emulators;
const keys = [];
Object.keys(emulatorConfig).forEach((key) => {
if (typeof emulatorConfig[key] === 'boolean' ||
!emulatorConfig[key].port ||
key === 'hosting') {
return;
}
keys.push(key);
});
return keys;
}
async function getBaseServeTarget(configFile, context) {
const rootDir = (0, path_1.dirname)(configFile);
if ((0, fs_1.existsSync)((0, path_1.join)(context.workspaceRoot, rootDir, 'nx-firebase.json'))) {
const config = (0, devkit_1.readJsonFile)((0, path_1.join)(context.workspaceRoot, rootDir, 'nx-firebase.json'));
if (config.baseServeTarget) {
return config.baseServeTarget;
}
}
const projectJson = (0, devkit_1.readJsonFile)((0, devkit_1.joinPathFragments)(context.workspaceRoot, rootDir, 'project.json'));
let serveTarget;
if (projectJson.targets['serve']) {
serveTarget = 'serve';
}
else {
Object.keys(projectJson.targets).forEach((key) => {
if (key.toLowerCase().includes('serve') && !serveTarget) {
serveTarget = key;
}
});
}
return `${projectJson.name}:${serveTarget}`;
}
//# sourceMappingURL=index.js.map