Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds --output to flatten #5947

Merged
98 changes: 58 additions & 40 deletions packages/hardhat-core/src/builtin-tasks/flatten.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
", "
)}`
)
);
}
}
});
);
17 changes: 16 additions & 1 deletion packages/hardhat-core/test/builtin-tasks/flatten.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
s-di-cola marked this conversation as resolved.
Show resolved Hide resolved
});

describe("No contracts to flatten", () => {
useFixtureProject("flatten-task/no-contracts");

Expand Down