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/4] 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/4] 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, From f7c461a506c0499a737d457d09f1c08da8d8a0d0 Mon Sep 17 00:00:00 2001 From: galargh Date: Thu, 28 Nov 2024 11:30:41 +0100 Subject: [PATCH 3/4] chore: clean up after failed flatten test run --- .../test/builtin-tasks/flatten.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/hardhat-core/test/builtin-tasks/flatten.ts b/packages/hardhat-core/test/builtin-tasks/flatten.ts index cc5c508816..6249b3cf1c 100644 --- a/packages/hardhat-core/test/builtin-tasks/flatten.ts +++ b/packages/hardhat-core/test/builtin-tasks/flatten.ts @@ -416,14 +416,17 @@ describe("Flatten task", () => { it("should write to an output file when the parameter output is specified", async function () { const outputFile = `${tmpdir()}/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); + try { + 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); + } finally { + removeSync(outputFile); + } }); describe("No contracts to flatten", () => { From 180f96e23f4d69b04a2b12cec025cb4a58c2d1bd Mon Sep 17 00:00:00 2001 From: galargh Date: Thu, 28 Nov 2024 11:47:02 +0100 Subject: [PATCH 4/4] docs: add a changeset for the new --output param --- .changeset/thick-lizards-yell.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thick-lizards-yell.md diff --git a/.changeset/thick-lizards-yell.md b/.changeset/thick-lizards-yell.md new file mode 100644 index 0000000000..e42260073c --- /dev/null +++ b/.changeset/thick-lizards-yell.md @@ -0,0 +1,5 @@ +--- +"hardhat": patch +--- + +Added an optional `--output` param to the flatten task