diff --git a/v-next/hardhat-build-system/src/internal/types/artifacts.ts b/v-next/hardhat-build-system/src/internal/types/artifacts.ts index 1b03ca82d3..719adce9e3 100644 --- a/v-next/hardhat-build-system/src/internal/types/artifacts.ts +++ b/v-next/hardhat-build-system/src/internal/types/artifacts.ts @@ -12,11 +12,6 @@ export interface Artifacts { */ readArtifact(contractNameOrFullyQualifiedName: string): Promise; - /** - * Synchronous version of readArtifact. - */ - readArtifactSync(contractNameOrFullyQualifiedName: string): Artifact; - /** * Returns true if an artifact exists. * @@ -41,11 +36,6 @@ export interface Artifacts { */ getBuildInfo(fullyQualifiedName: string): Promise; - /** - * Synchronous version of getBuildInfo. - */ - getBuildInfoSync(fullyQualifiedName: string): BuildInfo | undefined; - /** * Returns an array with the absolute paths of all the existing artifacts. * diff --git a/v-next/hardhat-build-system/src/internal/utils/artifacts.ts b/v-next/hardhat-build-system/src/internal/utils/artifacts.ts index 78f93bd261..898d57498c 100644 --- a/v-next/hardhat-build-system/src/internal/utils/artifacts.ts +++ b/v-next/hardhat-build-system/src/internal/utils/artifacts.ts @@ -1,8 +1,14 @@ import debug from "debug"; -import fsExtra from "fs-extra"; import * as os from "node:os"; import * as path from "node:path"; import fsPromises from "node:fs/promises"; +import { + ensureDir, + exists, + readJsonFile, + remove, + writeJsonFile, +} from "@nomicfoundation/hardhat-utils/fs"; import { HardhatError, assertHardhatInvariant } from "../errors/errors.js"; import { Artifact, @@ -33,7 +39,6 @@ import { getAllFilesMatching, getAllFilesMatchingSync, getFileTrueCase, - getFileTrueCaseSync, } from "./fs-utils.js"; const log = debug("hardhat:core:artifacts"); @@ -72,12 +77,7 @@ export class Artifacts implements IArtifacts { public async readArtifact(name: string): Promise { const artifactPath = await this.#getArtifactPath(name); - return fsExtra.readJson(artifactPath); - } - - public readArtifactSync(name: string): Artifact { - const artifactPath = this.#getArtifactPathSync(name); - return fsExtra.readJsonSync(artifactPath); + return readJsonFile(artifactPath); } public async artifactExists(name: string): Promise { @@ -93,7 +93,7 @@ export class Artifacts implements IArtifacts { throw e; } - return fsExtra.pathExists(artifactPath); + return exists(artifactPath); } public async getAllFullyQualifiedNames(): Promise { @@ -124,31 +124,7 @@ export class Artifacts implements IArtifacts { ); } - return fsExtra.readJSON(buildInfoPath); - } - - public getBuildInfoSync(fullyQualifiedName: string): BuildInfo | undefined { - let buildInfoPath = - this.#cache?.artifactFQNToBuildInfoPathCache.get(fullyQualifiedName); - - if (buildInfoPath === undefined) { - const artifactPath = - this.formArtifactPathFromFullyQualifiedName(fullyQualifiedName); - - const debugFilePath = this.#getDebugFilePath(artifactPath); - buildInfoPath = this.#getBuildInfoFromDebugFileSync(debugFilePath); - - if (buildInfoPath === undefined) { - return undefined; - } - - this.#cache?.artifactFQNToBuildInfoPathCache.set( - fullyQualifiedName, - buildInfoPath, - ); - } - - return fsExtra.readJSONSync(buildInfoPath); + return readJsonFile(buildInfoPath); } public async getArtifactPaths(): Promise { @@ -224,12 +200,10 @@ export class Artifacts implements IArtifacts { const artifactPath = this.formArtifactPathFromFullyQualifiedName(fullyQualifiedName); - await fsExtra.ensureDir(path.dirname(artifactPath)); + await ensureDir(path.dirname(artifactPath)); await Promise.all([ - fsExtra.writeJSON(artifactPath, artifact, { - spaces: 2, - }), + writeJsonFile(artifactPath, artifact), (async () => { if (pathToBuildInfo === undefined) { return; @@ -242,9 +216,7 @@ export class Artifacts implements IArtifacts { pathToBuildInfo, ); - await fsExtra.writeJSON(debugFilePath, debugFile, { - spaces: 2, - }); + await writeJsonFile(debugFilePath, debugFile); })(), ]); } finally { @@ -260,7 +232,7 @@ export class Artifacts implements IArtifacts { ): Promise { try { const buildInfoDir = path.join(this.#artifactsPath, BUILD_INFO_DIR_NAME); - await fsExtra.ensureDir(buildInfoDir); + await ensureDir(buildInfoDir); const buildInfoName = await this.#getBuildInfoName( solcVersion, @@ -470,7 +442,7 @@ export class Artifacts implements IArtifacts { .filter((buildInfoFile) => !validBuildInfos.has(buildInfoFile)) .map(async (buildInfoFile) => { log(`Removing buildInfo '${buildInfoFile}'`); - await fsExtra.unlink(buildInfoFile); + await remove(buildInfoFile); }), ); } @@ -573,28 +545,6 @@ export class Artifacts implements IArtifacts { return result; } - /** - * Sync version of _getArtifactPath - */ - #getArtifactPathSync(name: string): string { - const cached = this.#cache?.artifactNameToArtifactPathCache.get(name); - if (cached !== undefined) { - return cached; - } - - let result: string; - - if (isFullyQualifiedName(name)) { - result = this.#getValidArtifactPathFromFullyQualifiedNameSync(name); - } else { - const files = this.#getArtifactPathsSync(); - result = this.#getArtifactPathFromFiles(name, files); - } - - this.#cache?.artifactNameToArtifactPathCache.set(name, result); - return result; - } - /** * Returns the absolute path to the artifact that corresponds to the given * fully qualified name. @@ -780,40 +730,40 @@ Please replace "${contractName}" for the correct contract name wherever you are return mostSimilarNames; } - #getValidArtifactPathFromFullyQualifiedNameSync( - fullyQualifiedName: string, - ): string { - const artifactPath = - this.formArtifactPathFromFullyQualifiedName(fullyQualifiedName); - - try { - const trueCasePath = path.join( - this.#artifactsPath, - getFileTrueCaseSync( - this.#artifactsPath, - path.relative(this.#artifactsPath, artifactPath), - ), - ); - - if (artifactPath !== trueCasePath) { - throw new HardhatError(ERRORS.ARTIFACTS.WRONG_CASING, { - correct: this.#getFullyQualifiedNameFromPath(trueCasePath), - incorrect: fullyQualifiedName, - }); - } - - return trueCasePath; - } catch (e) { - if (e instanceof FileNotFoundError) { - return this.#handleWrongArtifactForFullyQualifiedName( - fullyQualifiedName, - ); - } - - // eslint-disable-next-line @nomicfoundation/hardhat-internal-rules/only-hardhat-error - throw e; - } - } + // #getValidArtifactPathFromFullyQualifiedNameSync( + // fullyQualifiedName: string, + // ): string { + // const artifactPath = + // this.formArtifactPathFromFullyQualifiedName(fullyQualifiedName); + + // try { + // const trueCasePath = path.join( + // this.#artifactsPath, + // getFileTrueCaseSync( + // this.#artifactsPath, + // path.relative(this.#artifactsPath, artifactPath), + // ), + // ); + + // if (artifactPath !== trueCasePath) { + // throw new HardhatError(ERRORS.ARTIFACTS.WRONG_CASING, { + // correct: this.#getFullyQualifiedNameFromPath(trueCasePath), + // incorrect: fullyQualifiedName, + // }); + // } + + // return trueCasePath; + // } catch (e) { + // if (e instanceof FileNotFoundError) { + // return this.#handleWrongArtifactForFullyQualifiedName( + // fullyQualifiedName, + // ); + // } + + // // eslint-disable-next-line @nomicfoundation/hardhat-internal-rules/only-hardhat-error + // throw e; + // } + // } #getDebugFilePath(artifactPath: string): string { return artifactPath.replace(/\.json$/, ".dbg.json"); @@ -874,11 +824,11 @@ Please replace "${contractName}" for the correct contract name wherever you are * Remove the artifact file and its debug file. */ async #removeArtifactFiles(artifactPath: string) { - await fsExtra.remove(artifactPath); + await remove(artifactPath); const debugFilePath = this.#getDebugFilePath(artifactPath); - await fsExtra.remove(debugFilePath); + await remove(debugFilePath); } /** @@ -888,20 +838,8 @@ Please replace "${contractName}" for the correct contract name wherever you are async #getBuildInfoFromDebugFile( debugFilePath: string, ): Promise { - if (await fsExtra.pathExists(debugFilePath)) { - const { buildInfo } = await fsExtra.readJson(debugFilePath); - return path.resolve(path.dirname(debugFilePath), buildInfo); - } - - return undefined; - } - - /** - * Sync version of _getBuildInfoFromDebugFile - */ - #getBuildInfoFromDebugFileSync(debugFilePath: string): string | undefined { - if (fsExtra.pathExistsSync(debugFilePath)) { - const { buildInfo } = fsExtra.readJsonSync(debugFilePath); + if (await exists(debugFilePath)) { + const { buildInfo } = await readJsonFile(debugFilePath); return path.resolve(path.dirname(debugFilePath), buildInfo); }