Skip to content

Commit

Permalink
replace fsExtra in artifacts with hh-utils + remove sync functions th…
Browse files Browse the repository at this point in the history
…at use fsExtra
  • Loading branch information
ChristopherDedominici committed May 13, 2024
1 parent 5f6bec8 commit 2b46254
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 125 deletions.
10 changes: 0 additions & 10 deletions v-next/hardhat-build-system/src/internal/types/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export interface Artifacts {
*/
readArtifact(contractNameOrFullyQualifiedName: string): Promise<Artifact>;

/**
* Synchronous version of readArtifact.
*/
readArtifactSync(contractNameOrFullyQualifiedName: string): Artifact;

/**
* Returns true if an artifact exists.
*
Expand All @@ -41,11 +36,6 @@ export interface Artifacts {
*/
getBuildInfo(fullyQualifiedName: string): Promise<BuildInfo | undefined>;

/**
* Synchronous version of getBuildInfo.
*/
getBuildInfoSync(fullyQualifiedName: string): BuildInfo | undefined;

/**
* Returns an array with the absolute paths of all the existing artifacts.
*
Expand Down
168 changes: 53 additions & 115 deletions v-next/hardhat-build-system/src/internal/utils/artifacts.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,7 +39,6 @@ import {
getAllFilesMatching,
getAllFilesMatchingSync,
getFileTrueCase,
getFileTrueCaseSync,
} from "./fs-utils.js";

const log = debug("hardhat:core:artifacts");
Expand Down Expand Up @@ -72,12 +77,7 @@ export class Artifacts implements IArtifacts {

public async readArtifact(name: string): Promise<Artifact> {
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<boolean> {
Expand All @@ -93,7 +93,7 @@ export class Artifacts implements IArtifacts {
throw e;
}

return fsExtra.pathExists(artifactPath);
return exists(artifactPath);
}

public async getAllFullyQualifiedNames(): Promise<string[]> {
Expand Down Expand Up @@ -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<string[]> {
Expand Down Expand Up @@ -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;
Expand All @@ -242,9 +216,7 @@ export class Artifacts implements IArtifacts {
pathToBuildInfo,
);

await fsExtra.writeJSON(debugFilePath, debugFile, {
spaces: 2,
});
await writeJsonFile(debugFilePath, debugFile);
})(),
]);
} finally {
Expand All @@ -260,7 +232,7 @@ export class Artifacts implements IArtifacts {
): Promise<string> {
try {
const buildInfoDir = path.join(this.#artifactsPath, BUILD_INFO_DIR_NAME);
await fsExtra.ensureDir(buildInfoDir);
await ensureDir(buildInfoDir);

const buildInfoName = await this.#getBuildInfoName(
solcVersion,
Expand Down Expand Up @@ -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);
}),
);
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -888,20 +838,8 @@ Please replace "${contractName}" for the correct contract name wherever you are
async #getBuildInfoFromDebugFile(
debugFilePath: string,
): Promise<string | undefined> {
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<any>(debugFilePath);
return path.resolve(path.dirname(debugFilePath), buildInfo);
}

Expand Down

0 comments on commit 2b46254

Please sign in to comment.