From 5b3e9fa2621f38e7d853dd7bfe7f594cb4ba95de Mon Sep 17 00:00:00 2001 From: s-di-cola Date: Fri, 15 Nov 2024 15:32:15 +0000 Subject: [PATCH 1/2] feat: adds --output to flatten --- .../hardhat-core/src/builtin-tasks/flatten.ts | 98 +++++++++++-------- .../test/builtin-tasks/flatten.ts | 17 +++- 2 files changed, 74 insertions(+), 41 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/flatten.ts b/packages/hardhat-core/src/builtin-tasks/flatten.ts index 28e6a65c88..61c72924c7 100644 --- a/packages/hardhat-core/src/builtin-tasks/flatten.ts +++ b/packages/hardhat-core/src/builtin-tasks/flatten.ts @@ -1,4 +1,5 @@ import picocolors from "picocolors"; +import { writeFileSync } from "node:fs"; import { subtask, task, types } from "../internal/core/config/config-env"; import { HardhatError } from "../internal/core/errors"; import { ERRORS } from "../internal/core/errors-list"; @@ -303,46 +304,63 @@ task( undefined, types.inputFile ) - .setAction(async ({ files }: { files: string[] | undefined }, { run }) => { - const [flattenedFile, metadata]: [string, FlattenMetadata | null] = - await run(TASK_FLATTEN_GET_FLATTENED_SOURCE_AND_METADATA, { files }); - - console.log(flattenedFile); - - if (metadata === null) return; - - if (metadata.filesWithoutLicenses.length > 0) { - console.warn( - picocolors.yellow( - `\nThe following file(s) do NOT specify SPDX licenses: ${metadata.filesWithoutLicenses.join( - ", " - )}` - ) - ); - } + .addOptionalParam( + "output", + "The output file containing the flattened contracts", + undefined, + types.string + ) + .setAction( + async ( + { + files, + output, + }: { files: string[] | undefined; output: string | undefined }, + { run } + ) => { + const [flattenedFile, metadata]: [string, FlattenMetadata | null] = + await run(TASK_FLATTEN_GET_FLATTENED_SOURCE_AND_METADATA, { files }); + + if (output !== undefined) { + writeFileSync(output, flattenedFile, { encoding: "utf-8" }); + } else { + console.log(flattenedFile); + } + if (metadata === null) return; + + if (metadata.filesWithoutLicenses.length > 0) { + console.warn( + picocolors.yellow( + `\nThe following file(s) do NOT specify SPDX licenses: ${metadata.filesWithoutLicenses.join( + ", " + )}` + ) + ); + } - if ( - metadata.pragmaDirective !== "" && - metadata.filesWithoutPragmaDirectives.length > 0 - ) { - console.warn( - picocolors.yellow( - `\nPragma abicoder directives are defined in some files, but they are not defined in the following ones: ${metadata.filesWithoutPragmaDirectives.join( - ", " - )}` - ) - ); - } + if ( + metadata.pragmaDirective !== "" && + metadata.filesWithoutPragmaDirectives.length > 0 + ) { + console.warn( + picocolors.yellow( + `\nPragma abicoder directives are defined in some files, but they are not defined in the following ones: ${metadata.filesWithoutPragmaDirectives.join( + ", " + )}` + ) + ); + } - if (metadata.filesWithDifferentPragmaDirectives.length > 0) { - console.warn( - picocolors.yellow( - `\nThe flattened file is using the pragma abicoder directive '${ - metadata.pragmaDirective - }' but these files have a different pragma abicoder directive: ${metadata.filesWithDifferentPragmaDirectives.join( - ", " - )}` - ) - ); + if (metadata.filesWithDifferentPragmaDirectives.length > 0) { + console.warn( + picocolors.yellow( + `\nThe flattened file is using the pragma abicoder directive '${ + metadata.pragmaDirective + }' but these files have a different pragma abicoder directive: ${metadata.filesWithDifferentPragmaDirectives.join( + ", " + )}` + ) + ); + } } - }); + ); diff --git a/packages/hardhat-core/test/builtin-tasks/flatten.ts b/packages/hardhat-core/test/builtin-tasks/flatten.ts index 64844b5645..68cb3c0a82 100644 --- a/packages/hardhat-core/test/builtin-tasks/flatten.ts +++ b/packages/hardhat-core/test/builtin-tasks/flatten.ts @@ -1,8 +1,11 @@ import { assert } from "chai"; -import fs from "fs"; +import fs, { readFileSync } from "fs"; import sinon, { SinonSpy } from "sinon"; import picocolors from "picocolors"; +import { removeSync } from "fs-extra"; +import { readSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import { TASK_FLATTEN, TASK_FLATTEN_GET_FLATTENED_SOURCE, @@ -412,6 +415,18 @@ describe("Flatten task", () => { assert(!spyFunctionConsoleWarn.called); }); + it("should write to an output file when the parameter output is specified", async function () { + const outputFile = "flatten.sol"; + await this.env.run(TASK_FLATTEN, { + files: ["contracts/A.sol", "contracts/D.sol"], + output: outputFile, + }); + const expected = await getExpectedSol(); + const actual = readFileSync(outputFile, "utf8"); + assert.equal(actual, expected); + removeSync(outputFile); + }); + describe("No contracts to flatten", () => { useFixtureProject("flatten-task/no-contracts"); From c7756d535142c1ab22ef1efc95402bbc37d680f2 Mon Sep 17 00:00:00 2001 From: s-di-cola Date: Sat, 16 Nov 2024 10:01:38 +0000 Subject: [PATCH 2/2] fix: adds temporary file --- packages/hardhat-core/test/builtin-tasks/flatten.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/hardhat-core/test/builtin-tasks/flatten.ts b/packages/hardhat-core/test/builtin-tasks/flatten.ts index 68cb3c0a82..cc5c508816 100644 --- a/packages/hardhat-core/test/builtin-tasks/flatten.ts +++ b/packages/hardhat-core/test/builtin-tasks/flatten.ts @@ -4,8 +4,7 @@ import fs, { readFileSync } from "fs"; import sinon, { SinonSpy } from "sinon"; import picocolors from "picocolors"; import { removeSync } from "fs-extra"; -import { readSync } from "node:fs"; -import { readFile } from "node:fs/promises"; +import { tmpdir } from "os"; import { TASK_FLATTEN, TASK_FLATTEN_GET_FLATTENED_SOURCE, @@ -416,7 +415,7 @@ describe("Flatten task", () => { }); it("should write to an output file when the parameter output is specified", async function () { - const outputFile = "flatten.sol"; + const outputFile = `${tmpdir()}/flatten.sol`; await this.env.run(TASK_FLATTEN, { files: ["contracts/A.sol", "contracts/D.sol"], output: outputFile,