Skip to content

Commit

Permalink
feat: get contract names
Browse files Browse the repository at this point in the history
  • Loading branch information
EjembiEmmanuel committed Dec 18, 2024
1 parent 1e57adf commit ed64920
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
7 changes: 5 additions & 2 deletions configs/scaffold.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"account": {
"name": "scaffold",
"profile": "scaffold"
}
}
},
"contract-names": [
"HelloStarknet"
]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
59 changes: 59 additions & 0 deletions scaffold_scripts/get-contract-names.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ed64920

Please sign in to comment.