Skip to content

Commit de0bfda

Browse files
committed
feat(cat): implement multi-file reading with error handling
- Refactor to async readMultipleFiles() function - Support reading multiple files via Promise.all - Output concatenated file contents to stdout - Add error handling with exit code 1 on failure - Remove debug logging (argv, path) - Fix markdown list formatting in README (bullets)
1 parent bc140a3 commit de0bfda

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

implement-shell-tools/cat/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Your task is to implement your own version of `cat`.
66

77
It must act the same as `cat` would, if run from the directory containing this README.md file, for the following command lines:
88

9-
* `cat sample-files/1.txt`
10-
* `cat -n sample-files/1.txt`
11-
* `cat sample-files/*.txt`
12-
* `cat -n sample-files/*.txt`
13-
* `cat -b sample-files/3.txt`
9+
- `cat sample-files/1.txt`
10+
- `cat -n sample-files/1.txt`
11+
- `cat sample-files/*.txt`
12+
- `cat -n sample-files/*.txt`
13+
- `cat -b sample-files/3.txt`
1414

1515
Matching any additional behaviours or flags are optional stretch goals.
1616

implement-shell-tools/cat/cat.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import process from "node:process";
2-
import { promises as fs } from "node:fs";
2+
import { promises as fs, readFile } from "node:fs";
33

4-
const argv = process.argv.slice(2);
5-
console.log(argv);
4+
const filesToRead = process.argv.slice(2);
5+
console.log(filesToRead);
66

7-
const path = argv[0];
7+
async function readMultipleFiles() {
8+
try {
9+
const results = await Promise.all(
10+
filesToRead.map((file) => fs.readFile(file, "utf-8")),
11+
);
12+
process.stdout.write(results.join(""));
13+
} catch (err) {
14+
console.error("Error reading multiple files:", err);
15+
process.exitCode = 1;
16+
}
17+
}
818

9-
console.log(path);
10-
11-
const content = await fs.readFile(path, "utf-8");
12-
console.log(content.trim());
19+
readMultipleFiles();

0 commit comments

Comments
 (0)