diff --git a/configs/scaffold.config.json b/configs/scaffold.config.json index 70615fe..ce6d894 100644 --- a/configs/scaffold.config.json +++ b/configs/scaffold.config.json @@ -5,5 +5,8 @@ "account": { "name": "scaffold", "profile": "scaffold" - } -} + }, + "contract-names": [ + "HelloStarknet" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 45d66b9..66ad34e 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,15 @@ "description": "An open-source starknet development stack", "bin": "./bin/cli.mjs", "scripts": { + "prepare-account": "node scaffold_scripts/prepare-account.js", + "deploy-account": "node scaffold_scripts/deploy-account.js", + "get-contract-names": "node scaffold_scripts/get-contract-names.js", "build-contracts": "cd contracts && scarb build", "test-contracts": "cd contracts && snforge test", "format-contracts": "cd contracts && scarb fmt", "verify-contracts": "cd contracts && sncast verify --contract-address ${npm_config_contract_address} --contract-name ${npm_config_contract_name} --verifier walnut --network ${npm_config_network}", "contract-scripts": "cd contracts/scripts && sncast script run ${npm_config_script} --url ${npm_config_url}", "generate-interface": "cd contracts && src5_rs parse", - "prepare-account": "node scaffold_scripts/prepare-account.js", - "deploy-account": "node scaffold_scripts/deploy-account.js", "delete-account": "cd contracts && sncast --profile ${npm_config_profile} --accounts-file ${npm_config_accounts_file} account delete --name ${npm_config_name} --network ${npm_config_network}", "declare-contract": "cd contracts && sncast --profile ${npm_config_profile} declare --contract-name ${npm_config_contract_name} --fee-token ${npm_config_fee_token}", "deploy-contract": "cd contracts && sncast --profile ${npm_config_profile} deploy --fee-token ${npm_config_fee_token} --class-hash ${npm_config_class_hash}", diff --git a/scaffold_scripts/get-contract-names.js b/scaffold_scripts/get-contract-names.js new file mode 100644 index 0000000..7851054 --- /dev/null +++ b/scaffold_scripts/get-contract-names.js @@ -0,0 +1,59 @@ +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); +}