canary-log-viewer.ts 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import readline from "readline";
  2. import fs from "fs";
  3. import yargs from "yargs";
  4. import packageJson from "./package.json";
  5. import * as Rx from "rxjs";
  6. import { hideBin } from "yargs/helpers";
  7. import { map, pluck, takeUntil } from "rxjs/operators";
  8. const args = yargs(hideBin(process.argv))
  9. .scriptName(Object.keys(packageJson.bin)[0])
  10. .usage("Usage: $0 logfile")
  11. .option("files", {
  12. boolean: true,
  13. describe: "Strip out and save the embedded files from the log file"
  14. })
  15. .argv;
  16. type LogLine = {
  17. log: string;
  18. stream: string;
  19. time: string;
  20. };
  21. const rl = readline.createInterface({
  22. input: fs.createReadStream(args._[0]?.toString())
  23. });
  24. Rx.fromEvent<string>(rl, "line")
  25. .pipe(
  26. takeUntil(Rx.fromEvent(rl, "close")),
  27. map<string, LogLine>(line => JSON.parse(line)),
  28. pluck("log"),
  29. map(log => log.replace(/\n$/, ""))
  30. )
  31. .subscribe({
  32. next: console.log,
  33. error: console.error,
  34. complete: () => rl.close()
  35. });