Skip to content

Instantly share code, notes, and snippets.

@IchordeDionysos
Last active May 24, 2020 08:49
Show Gist options
  • Save IchordeDionysos/46f8679f2cc9bee26d41611a0e7af625 to your computer and use it in GitHub Desktop.
Save IchordeDionysos/46f8679f2cc9bee26d41611a0e7af625 to your computer and use it in GitHub Desktop.
Rules import resolver Medium Article
const fs = require("fs");
const path = require("path");
// TODO: Add your exact paths
const srcFile = path.join(__dirname, "rules", "index.rules");
const destFile = path.join(__dirname, "firestore.rules");
fs.writeFileSync(destFile, resolveImports(srcFile));
function resolveImports(filePath) {
const rulesFile = fs.readFileSync(filePath).toString();
// Every second element in the array will have the file name to the imported file
const fileElements = rulesFile.split(/include "([A-Za-z0-9\/.]+\.rules)";/);
// Resolve imports
let resolvedRulesFile = "";
let isImport = false;
for (const elem of fileElements) {
if (isImport) {
const importPath = path.join(filePath, "..", elem);
resolvedRulesFile += resolveImports(importPath);
} else {
resolvedRulesFile += elem;
}
isImport = !isImport;
}
return resolvedRulesFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment