diff --git a/.changeset/dull-spiders-hear.md b/.changeset/dull-spiders-hear.md new file mode 100644 index 0000000000..a93a161f6f --- /dev/null +++ b/.changeset/dull-spiders-hear.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/cli": patch +--- + +Refactored findLibraries to accept an array of paths. diff --git a/packages/cli/src/deploy/findLibraries.ts b/packages/cli/src/deploy/findLibraries.ts index 40609a0f98..af8c53312f 100644 --- a/packages/cli/src/deploy/findLibraries.ts +++ b/packages/cli/src/deploy/findLibraries.ts @@ -2,16 +2,24 @@ import { readFileSync } from "fs"; import { globSync } from "glob"; import { orderByDependencies } from "./orderByDependencies"; import { LinkReferences } from "../utils/findPlaceholders"; +import { Library } from "./common"; +import { getContractData } from "../utils/getContractData"; +import { createPrepareDeploy } from "./createPrepareDeploy"; +import path from "path"; -export function findLibraries(forgeOutDir: string): readonly { - readonly path: string; - readonly name: string; -}[] { - const artifacts = globSync(`${forgeOutDir}/**/*.json`, { ignore: "**/*.abi.json" }) - .sort() - .map((path) => JSON.parse(readFileSync(path, "utf8"))); +export function findLibraries(forgeOutDirs: string | string[]): Library[] { + const dirs = Array.isArray(forgeOutDirs) ? [...new Set(forgeOutDirs)] : [forgeOutDirs]; - const libraries = artifacts.flatMap((artifact) => { + // Deduplicate output directories and get all the artifacts + const artifactsWithDirs = dirs.flatMap((dir) => { + const files = globSync(`${dir}/**/*.json`, { ignore: "/**/*.abi.json" }).sort(); + return files.map((filePath) => ({ + forgeOutDir: dir, + artifact: JSON.parse(readFileSync(filePath, "utf8")), + })); + }); + + const libraries = artifactsWithDirs.flatMap(({ artifact, forgeOutDir }) => { if (!artifact.metadata) return []; const contractPath = Object.keys(artifact.metadata.settings.compilationTarget)[0]; @@ -24,13 +32,25 @@ export function findLibraries(forgeOutDir: string): readonly { name: libraryName, dependentPath: contractPath, dependentName: contractName, + forgeOutDir, })), ); }); - return orderByDependencies( + const orderedByDeps = orderByDependencies( libraries, (lib) => `${lib.path}:${lib.name}`, (lib) => [`${lib.dependentPath}:${lib.dependentName}`], ); + + return orderedByDeps.map((library) => { + const contractData = getContractData(path.basename(library.path), library.name, library.forgeOutDir); + return { + path: library.path, + name: library.name, + abi: contractData.abi, + prepareDeploy: createPrepareDeploy(contractData.bytecode, contractData.placeholders), + deployedBytecodeSize: contractData.deployedBytecodeSize, + }; + }); } diff --git a/packages/cli/src/deploy/resolveConfig.ts b/packages/cli/src/deploy/resolveConfig.ts index dee1d554ef..2025863ae3 100644 --- a/packages/cli/src/deploy/resolveConfig.ts +++ b/packages/cli/src/deploy/resolveConfig.ts @@ -38,19 +38,7 @@ export async function resolveConfig({ return [moduleOutDir]; }); - const libraries = [forgeOutDir, ...moduleOutDirs].flatMap((outDir) => - findLibraries(outDir).map((library): Library => { - // foundry/solc flattens artifacts, so we just use the path basename - const contractData = getContractData(path.basename(library.path), library.name, outDir); - return { - path: library.path, - name: library.name, - abi: contractData.abi, - prepareDeploy: createPrepareDeploy(contractData.bytecode, contractData.placeholders), - deployedBytecodeSize: contractData.deployedBytecodeSize, - }; - }), - ); + const libraries = findLibraries([forgeOutDir, ...moduleOutDirs]); const baseSystemContractData = getContractData("System.sol", "System", forgeOutDir); const baseSystemFunctions = baseSystemContractData.abi