Reading a Whole File
Dev’s port has the log’s name from the command line, and now it needs to read the log. Dev’s
Python tool loops over the log, but the port reads the whole text first, which in Python is text =
f.read() inside with open(path, encoding="utf-8") as f:.
Python’s open() is built in, but in JavaScript the port imports readFile() from Node’s built-in
file module, node:fs/promises, and the path line ports as before. One call takes the path and "utf8", like
Python’s encoding, with no file object and no with block. Here’s the line-by-line port. What does
it log when it runs?
digest.js
import { readFile } from "node:fs/promises";
const [path] = process.argv.slice(2);
const text = readFile(path, "utf8");
console.log(text);
const lines = text.split("\n");
Run as node digest.js logs/week-44.log, it logs a pending promise, not the text, and then the
last line throws (your terminal also shows Node’s source line and stack trace, left out here):