|
| 1 | +import * as fs from "fs-extra"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +// Define the paths for the directories |
| 5 | +const artifactsForgeDir = path.join(__dirname, "..", "artifacts_forge"); |
| 6 | +const contractsDir = path.join(__dirname, "..", "contracts"); |
| 7 | +const contractArtifactsDir = path.join(__dirname, "..", "contract_artifacts"); |
| 8 | + |
| 9 | +async function getAllSolidityFiles(dir: string): Promise<string[]> { |
| 10 | + const dirents = await fs.readdir(dir, { withFileTypes: true }); |
| 11 | + const files = await Promise.all( |
| 12 | + dirents.map(dirent => { |
| 13 | + const res = path.join(dir, dirent.name); |
| 14 | + return dirent.isDirectory() ? getAllSolidityFiles(res) : res; |
| 15 | + }), |
| 16 | + ); |
| 17 | + // Flatten the array and filter for .sol files |
| 18 | + return files |
| 19 | + .flat() |
| 20 | + .filter(file => file.endsWith(".sol")) |
| 21 | + .map(file => path.basename(file)); |
| 22 | +} |
| 23 | + |
| 24 | +async function main() { |
| 25 | + // Create the contract_artifacts directory |
| 26 | + await fs.ensureDir(contractArtifactsDir); |
| 27 | + |
| 28 | + // Get all directories within artifacts_forge that match *.sol |
| 29 | + const artifactDirs = await fs.readdir(artifactsForgeDir); |
| 30 | + const validArtifactDirs = artifactDirs.filter(dir => dir.endsWith(".sol")); |
| 31 | + |
| 32 | + // Get all .sol filenames within contracts (recursively) |
| 33 | + const validContractFiles = await getAllSolidityFiles(contractsDir); |
| 34 | + |
| 35 | + // Check if directory-name matches any Solidity file name from contracts |
| 36 | + for (const artifactDir of validArtifactDirs) { |
| 37 | + // Removing the .sol extension from the directory name to match with file names |
| 38 | + const artifactName = path.basename(artifactDir, ".sol"); |
| 39 | + |
| 40 | + if (validContractFiles.includes(artifactName + ".sol")) { |
| 41 | + const sourcePath = path.join(artifactsForgeDir, artifactDir); |
| 42 | + const destinationPath = path.join(contractArtifactsDir, artifactDir); |
| 43 | + await fs.copy(sourcePath, destinationPath); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + console.log("Done copying matching directories."); |
| 48 | +} |
| 49 | + |
| 50 | +main().catch(error => { |
| 51 | + console.error("An error occurred:", error); |
| 52 | +}); |
0 commit comments