Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Last active August 4, 2023 16:36
Show Gist options
  • Save emersonbroga/19d35a21bb0822b3af716ed13b28522a to your computer and use it in GitHub Desktop.
Save emersonbroga/19d35a21bb0822b3af716ed13b28522a to your computer and use it in GitHub Desktop.
[QuickNode] Update TabOrder on Chain JSON files.
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const [, , folderPath] = process.argv;
if (!folderPath) {
console.error(
"Please provide the chains json folder path [metadata/chains/]"
);
process.exit(1);
}
const getJsonFiles = (folderPath) => {
return fs
.readdirSync(folderPath)
.map((f) => path.join(folderPath, f))
.filter((file) => {
return path.extname(file) === ".json";
});
};
function readJsonFile(filePath) {
try {
const data = fs.readFileSync(filePath, "utf8");
const jsonData = JSON.parse(data);
return jsonData;
} catch (error) {
throw new Error(`Error reading JSON file: ${filePath} - ${error}`);
}
}
const updateFiles = (sorted) => {
for (const item of sorted) {
const data = readJsonFile(item.filePath);
data.options.api.tab_order = item.tab_order;
const jsonStringWithNewLine = JSON.stringify(data, null, 2) + "\n";
fs.writeFileSync(item.filePath, jsonStringWithNewLine);
}
return sorted;
};
const run = async (folderPath) => {
const files = getJsonFiles(folderPath);
const fileData = [];
for (const filePath of files) {
const data = readJsonFile(filePath);
if (data.options && data.options.api) {
fileData.push({
filePath,
name: data.name,
coin: data.coin,
tab_order: data.options.api.tab_order,
});
}
}
const mainOnes = fileData.filter((item) => firstChains.includes(item.name));
const otherOnes = fileData.filter((item) => !firstChains.includes(item.name));
const sortedMainOnes = mainOnes.map((item) => {
const index = firstChains.indexOf(item.name);
const tab_order = (index + 1) * 10;
return { ...item, tab_order };
});
const sortedOtherOnes = otherOnes
.sort((a, b) => a.name.localeCompare(b.name))
.map((item, index) => {
const tab_order = (index + 1) * 10 + mainOnes.length * 10;
return { ...item, tab_order };
});
const sorted = [...sortedMainOnes, ...sortedOtherOnes].sort((a, b) => {
return a.tab_order - b.tab_order;
});
return sorted;
};
const firstChains = ["Ethereum", "Solana", "BNB Smart Chain", "Polygon"];
run(folderPath).then(updateFiles).then(console.log);
{
"name": "update-chains-tab-order",
"description": "This is a internal tool to generate update the tab_order on the json files for each chain.",
"author": "Emerson Carvalho(Broga)",
"version": "1.0.0",
"bin": "./index.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment