Skip to content

Commit 1e95feb

Browse files
committed
fix: implement ls to not exit when an error is encountered
1 parent bd10e7c commit 1e95feb

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

implement-shell-tools/cat/customCat.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env node
22

3-
4-
53
import { program } from "commander";
64
import { promises as fs } from "node:fs";
75
import process from "node:process";
@@ -31,12 +29,12 @@ for (const filePath of argv) {
3129
if (options.nonBlank) {
3230
if (line !== "") {
3331
count++;
34-
process.stdout.write(
35-
`${count.toString().padStart(6)} ${line}\n`,
36-
);
32+
process.stdout.write(`${count.toString().padStart(6)} ${line}\n`);
3733
} else process.stdout.write(`${line}\n`);
3834
} else if (options.number) {
39-
process.stdout.write(`${(count++ + 1).toString().padStart(6)} ${line}\t`);
35+
process.stdout.write(
36+
`${(count++ + 1).toString().padStart(6)} ${line}\t`,
37+
);
4038
} else {
4139
process.stdout.write(`${line}\n`);
4240
}

implement-shell-tools/ls/customLs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ program.parse();
1616
const options = program.opts();
1717
const targetPaths = program.args.length > 0 ? program.args : ["."];
1818

19+
let hadError = false;
20+
1921
async function listDir(dirPath, showHeader) {
2022
try {
2123
let contents = await fs.readdir(dirPath);
@@ -39,7 +41,7 @@ async function listDir(dirPath, showHeader) {
3941
}
4042
} catch (error) {
4143
console.error(`cls: ${dirPath}: ${error.message}`);
42-
process.exit(1);
44+
hadError = true;
4345
}
4446
}
4547

@@ -51,3 +53,5 @@ for (let i = 0; i < targetPaths.length; i++) {
5153

5254
if (isMultiplePath && i < targetPaths.length - 1) process.stdout.write(`\n`);
5355
}
56+
57+
if (hadError) process.exit(1);

0 commit comments

Comments
 (0)