Skip to content

Commit

Permalink
feat(gas-report): add more logs to stdin piping (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Oct 3, 2023
1 parent 3d0b3ed commit ba17bda
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-avocados-poke.md
@@ -0,0 +1,5 @@
---
"@latticexyz/gas-report": patch
---

Pass through `stdin` logs in `gas-report`. Since the script piping in logs to `gas-report` can be long-running, it is useful to see its logs to know if it's stalling.
2 changes: 2 additions & 0 deletions packages/gas-report/package.json
Expand Up @@ -34,12 +34,14 @@
"chalk": "^5.3.0",
"dotenv": "^16.0.3",
"execa": "^7.0.0",
"stream-to-array": "^2.3.0",
"strip-ansi": "^7.1.0",
"table": "^6.8.1",
"yargs": "^17.7.1"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@types/stream-to-array": "^2.3.1",
"@types/yargs": "^17.0.10",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1",
Expand Down
22 changes: 6 additions & 16 deletions packages/gas-report/ts/index.ts
Expand Up @@ -4,6 +4,7 @@ import { execa } from "execa";
import chalk from "chalk";
import { table, getBorderCharacters } from "table";
import stripAnsi from "strip-ansi";
import toArray from "stream-to-array";

/**
* Print the gas report to the console, save it to a file and compare it to a previous gas report if provided.
Expand Down Expand Up @@ -106,8 +107,11 @@ async function runGasReport(options: Options): Promise<GasReport> {
let logs: string;
try {
if (options.stdin) {
// Read the logs from stdin
logs = await readStdIn();
// Read the logs from stdin and pipe them to stdout for visibility
console.log("Waiting for stdin...");
process.stdin.pipe(process.stdout);
logs = (await toArray(process.stdin)).map((chunk) => chunk.toString()).join("\n");
console.log("Done reading stdin");
} else {
// Run the default test command to capture the logs
const child = execa("forge", ["test", "-vvv"], {
Expand Down Expand Up @@ -202,17 +206,3 @@ 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<string> {
return new Promise((resolve) => {
let data = "";

process.stdin.on("data", (chunk) => {
data += chunk;
});

process.stdin.on("end", () => {
resolve(data);
});
});
}
19 changes: 18 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit ba17bda

@vercel
Copy link

@vercel vercel bot commented on ba17bda Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.