From 882397f87eb0d8f0843011f3d6c151bed5d9161b Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 3 Oct 2023 15:01:43 +0100 Subject: [PATCH 1/4] allow gas-reporter to parse stdin --- packages/gas-report/ts/index.ts | 46 ++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/gas-report/ts/index.ts b/packages/gas-report/ts/index.ts index 9fea453b2d..c14c988f0d 100644 --- a/packages/gas-report/ts/index.ts +++ b/packages/gas-report/ts/index.ts @@ -31,6 +31,7 @@ type Options = { path: string[]; save?: string; compare?: string; + stdin?: boolean; }; type GasReportEntry = { @@ -52,13 +53,14 @@ const commandModule: CommandModule = { return yargs.options({ save: { type: "string", desc: "Save the gas report to a file" }, compare: { type: "string", desc: "Compare to an existing gas report" }, + stdin: { type: "boolean", desc: "Parse the gas report from stdin instead of running an internal test command" }, }); }, - async handler({ save, compare }) { + async handler(options) { let gasReport: GasReport; try { - gasReport = await runGasReport(); + gasReport = await runGasReport(options); } catch (error) { console.error(error); setTimeout(() => process.exit()); @@ -66,6 +68,7 @@ const commandModule: CommandModule = { } // If this gas report should be compared to an existing one, load the existing one + let { compare } = options; if (compare) { try { const compareGasReport: GasReport = JSON.parse(readFileSync(compare, "utf8")); @@ -84,7 +87,7 @@ const commandModule: CommandModule = { printGasReport(gasReport, compare); // Save gas report to file if requested - if (save) saveGasReport(gasReport, save); + if (options.save) saveGasReport(gasReport, options.save); process.exit(0); }, @@ -92,19 +95,24 @@ const commandModule: CommandModule = { export default commandModule; -async function runGasReport(): Promise { +async function runGasReport(options: Options): Promise { console.log("Running gas report"); const gasReport: GasReport = []; // Extract the logs from the child process - let stdout: string; + let logs: string; try { - // Run the generated file using forge - const child = execa("forge", ["test", "-vvv"], { - stdio: ["inherit", "pipe", "inherit"], - env: { GAS_REPORTER_ENABLED: "true" }, - }); - stdout = (await child).stdout; + if (options.stdin) { + // Read the logs from stdin + logs = await readStdIn(); + } else { + // Run the default test command to capture the logs + const child = execa("forge", ["test", "-vvv"], { + stdio: ["inherit", "pipe", "inherit"], + env: { GAS_REPORTER_ENABLED: "true" }, + }); + logs = (await child).stdout; + } } catch (error: any) { console.log(error.stdout ?? error); console.log(chalk.red("\n-----------\nError while running the gas report (see above)")); @@ -112,7 +120,7 @@ async function runGasReport(): Promise { } // Extract the gas reports from the logs - const lines = stdout.split("\n").map(stripAnsi); + const lines = logs.split("\n").map(stripAnsi); const gasReportPattern = /^\s*GAS REPORT: (\d+) (.*)$/; const testFunctionPattern = /^\[(?:PASS|FAIL).*\] (\w+)\(\)/; const testFilePattern = /^Running \d+ tests? for (.*):(.*)$/; @@ -191,3 +199,17 @@ function saveGasReport(gasReport: GasReport, path: string) { console.log(chalk.bold(`Saving gas report to ${path}`)); writeFileSync(path, `${JSON.stringify(gasReport, null, 2)}\n`); } + +function readStdIn(): Promise { + return new Promise((resolve) => { + let data = ""; + + process.stdin.on("data", (chunk) => { + data += chunk; + }); + + process.stdin.on("end", () => { + resolve(data); + }); + }); +} From 29971eb00960cc7a6320ea3652e2e7818d70c3ef Mon Sep 17 00:00:00 2001 From: alvarius Date: Tue, 3 Oct 2023 15:03:13 +0100 Subject: [PATCH 2/4] Update packages/gas-report/ts/index.ts --- packages/gas-report/ts/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gas-report/ts/index.ts b/packages/gas-report/ts/index.ts index c14c988f0d..9c61aaa516 100644 --- a/packages/gas-report/ts/index.ts +++ b/packages/gas-report/ts/index.ts @@ -53,7 +53,7 @@ const commandModule: CommandModule = { return yargs.options({ save: { type: "string", desc: "Save the gas report to a file" }, compare: { type: "string", desc: "Compare to an existing gas report" }, - stdin: { type: "boolean", desc: "Parse the gas report from stdin instead of running an internal test command" }, + stdin: { type: "boolean", desc: "Parse the gas report logs from stdin instead of running an internal test command" }, }); }, From bbaec8c0ce48bfeea764e87709542f145dc30b6d Mon Sep 17 00:00:00 2001 From: alvarius Date: Tue, 3 Oct 2023 15:05:49 +0100 Subject: [PATCH 3/4] Create silver-dolls-shave.md --- .changeset/silver-dolls-shave.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .changeset/silver-dolls-shave.md diff --git a/.changeset/silver-dolls-shave.md b/.changeset/silver-dolls-shave.md new file mode 100644 index 0000000000..84f81df962 --- /dev/null +++ b/.changeset/silver-dolls-shave.md @@ -0,0 +1,11 @@ +--- +"@latticexyz/gas-report": minor +--- + +Allow the `gas-report` CLI to parse logs via `stdin`, so it can be used with custom test commands (e.g. `mud test`). + +Usage: +```sh +# replace `forge test -vvv` with the custom test command +GAS_REPORTER_ENABLED=true forge test -vvv | pnpm gas-report --stdin +``` From 7baf988b15c68402429c2354df7cf579b2e1e815 Mon Sep 17 00:00:00 2001 From: alvrs Date: Tue, 3 Oct 2023 15:18:37 +0100 Subject: [PATCH 4/4] prettier --- .changeset/silver-dolls-shave.md | 1 + packages/gas-report/ts/index.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.changeset/silver-dolls-shave.md b/.changeset/silver-dolls-shave.md index 84f81df962..4cb44e330f 100644 --- a/.changeset/silver-dolls-shave.md +++ b/.changeset/silver-dolls-shave.md @@ -5,6 +5,7 @@ Allow the `gas-report` CLI to parse logs via `stdin`, so it can be used with custom test commands (e.g. `mud test`). Usage: + ```sh # replace `forge test -vvv` with the custom test command GAS_REPORTER_ENABLED=true forge test -vvv | pnpm gas-report --stdin diff --git a/packages/gas-report/ts/index.ts b/packages/gas-report/ts/index.ts index 9c61aaa516..35b644f66c 100644 --- a/packages/gas-report/ts/index.ts +++ b/packages/gas-report/ts/index.ts @@ -53,7 +53,10 @@ const commandModule: CommandModule = { return yargs.options({ save: { type: "string", desc: "Save the gas report to a file" }, compare: { type: "string", desc: "Compare to an existing gas report" }, - stdin: { type: "boolean", desc: "Parse the gas report logs from stdin instead of running an internal test command" }, + stdin: { + type: "boolean", + desc: "Parse the gas report logs from stdin instead of running an internal test command", + }, }); },