Last active
December 12, 2024 11:08
-
-
Save bodia-uz/112dc9fde3d13f9d008a23c866d71c96 to your computer and use it in GitHub Desktop.
gen REP ApiKey map
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
import * as ts from "typescript"; | |
import * as fs from "fs"; | |
import * as path from "path"; | |
// Directory containing TypeScript files | |
const dirPath = "./"; // Change this to the path of your repository to REP packages (keep as is if `extractApiKeyMap.js` script is in `packages`) | |
const map = {}; | |
function extractKeysFromFile(filePath) { | |
const content = fs.readFileSync(filePath, "utf8"); | |
const sourceFile = ts.createSourceFile( | |
filePath, | |
content, | |
ts.ScriptTarget.Latest, | |
true | |
); | |
ts.forEachChild(sourceFile, (node) => { | |
if (ts.isVariableStatement(node)) { | |
node.declarationList.declarations.forEach((declaration) => { | |
if ( | |
ts.isIdentifier(declaration.name) && | |
declaration.initializer && | |
ts.isObjectLiteralExpression(declaration.initializer) | |
) { | |
const key = declaration.name.text; | |
const nameProperty = declaration.initializer.properties.find( | |
(prop) => | |
ts.isPropertyAssignment(prop) && | |
ts.isIdentifier(prop.name) && | |
prop.name.text === "name" && | |
ts.isStringLiteral(prop.initializer) | |
); | |
if (nameProperty) { | |
const value = nameProperty.initializer.text; | |
map[key] = value; | |
} | |
} | |
}); | |
} | |
}); | |
} | |
function processDirectory(dir) { | |
const files = fs.readdirSync(dir); | |
files.forEach((file) => { | |
const fullPath = path.join(dir, file); | |
if (fs.statSync(fullPath).isDirectory()) { | |
processDirectory(fullPath); | |
} else if (file.endsWith(".ts") || file.endsWith(".tsx")) { | |
extractKeysFromFile(fullPath); | |
} | |
}); | |
} | |
processDirectory(path.resolve(dirPath)); | |
fs.writeFileSync("map.json", JSON.stringify(map, null, 2)); | |
console.log("Generated Map:"); | |
console.log(JSON.stringify(map, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment