From 4ea684196d94e2f311a6ff95e2ff235e7839b62a Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Wed, 18 Dec 2024 11:50:05 +0100 Subject: [PATCH] refac: optimize script for generating contract names --- scaffold_scripts/generateContractNames.mjs | 63 ++++++++++++++++++++++ scaffold_scripts/get-contract-names.js | 59 -------------------- 2 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 scaffold_scripts/generateContractNames.mjs delete mode 100644 scaffold_scripts/get-contract-names.js diff --git a/scaffold_scripts/generateContractNames.mjs b/scaffold_scripts/generateContractNames.mjs new file mode 100644 index 0000000..f47af5e --- /dev/null +++ b/scaffold_scripts/generateContractNames.mjs @@ -0,0 +1,63 @@ +import { readdirSync, readFileSync, writeFileSync } from "fs"; +import { resolve, join } from "path"; + +const configPath = resolve(process.cwd(), "configs/scaffold.config.json"); +const contractsDir = resolve(process.cwd(), "contracts/src"); + +/** + * Scans the contracts directory and returns an array of contract names. + * @returns {string[]} An array of contract names. + */ +function getContractNames() { + try { + // Read all files in the contracts directory with .cairo extension + const cairoFiles = readdirSync(contractsDir).filter((file) => + file.endsWith(".cairo") + ); + + // Extract contract names using regex + return cairoFiles.flatMap((file) => { + const filePath = join(contractsDir, file); + const content = readFileSync(filePath, "utf-8"); + const contractRegex = /#\[\s*starknet::contract\s*]\s*mod\s+(\w+)/g; + const matches = [...content.matchAll(contractRegex)]; + return matches.map((match) => match[1]); // Extract the contract name from each match + }); + } catch (err) { + throw new Error(`Failed to read contract files: ${err.message}`); + } +} + +/** + * Reads a JSON configuration file and updates the contract names array + * @param {string[]} contractNames - The array of contract names for updating. + */ +function updateConfigFileWithContractNames(contractNames) { + try { + const config = JSON.parse(readFileSync(configPath, "utf-8")); + + // Update and write the configuration file + config["contract-names"] = contractNames; + writeFileSync(configPath, JSON.stringify(config, null, 2)); + + console.log("Contract names added to config file successfully!"); + } catch (err) { + throw new Error(`Error updating config file: ${err.message}`); + } +} + +/** + * Main function to scan contracts and update the configuration file. + */ +function main() { + try { + const contractNames = getContractNames(); + console.log("Contracts found:", contractNames); + + updateConfigFileWithContractNames(contractNames); + } catch (err) { + console.error(err.message); + } +} + +main(); diff --git a/scaffold_scripts/get-contract-names.js b/scaffold_scripts/get-contract-names.js deleted file mode 100644 index 7851054..0000000 --- a/scaffold_scripts/get-contract-names.js +++ /dev/null @@ -1,59 +0,0 @@ -const fs = require("fs"); -const path = require("path"); - -const configPath = path.resolve(__dirname, "../configs/scaffold.config.json"); - -// Function to scan files in a directory -function getContractNames() { - const contractNames = []; - - // Read all files in the directory - const contractsDir = path.resolve(__dirname, "../contracts/src"); - const files = fs.readdirSync(contractsDir); - - // Filter files with .cairo extension - const cairoFiles = files.filter((file) => file.endsWith(".cairo")); - - cairoFiles.forEach((file) => { - const filePath = path.join(contractsDir, file); - const content = fs.readFileSync(filePath, "utf-8"); - - // Use regex to find contracts with #[starknet::contract] and module name - const contractRegex = /#\[\s*starknet::contract\s*]\s*mod\s+(\w+)/g; - let match; - - while ((match = contractRegex.exec(content)) !== null) { - const contractName = match[1]; // The module name after 'mod' - contractNames.push(contractName); - } - }); - - return contractNames; -} - -// Function to update the JSON file -function updateConfigFileWithContractNames(contractNames) { - try { - // Read the existing JSON file - const jsonData = fs.readFileSync(configPath, "utf-8"); - const config = JSON.parse(jsonData); - - // Update the "contract-names" array - config["contract-names"] = contractNames; - - // Write the updated JSON back to the file - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - console.log("Contract names added to config file successfully!"); - } catch (err) { - console.error("Error adding contract names to config file:", err.message); - } -} - -try { - const contractNames = getContractNames(); - console.log("Contracts found:", contractNames); - - updateConfigFileWithContractNames(contractNames); -} catch (err) { - console.error("Error scanning files:", err.message); -}