Last active
May 24, 2020 08:49
-
-
Save IchordeDionysos/46f8679f2cc9bee26d41611a0e7af625 to your computer and use it in GitHub Desktop.
Rules import resolver Medium Article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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