Skip to content

Commit 41a871e

Browse files
committed
added catch to handle non exist files, and trim on every line to handle nonBlank
1 parent 06aacde commit 41a871e

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

implement-shell-tools/cat/customCat.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,28 @@ const pathsArray = argumentArray;
2929
let count = 1;
3030

3131
for (let path of pathsArray) {
32-
const context = await fs.readFile(path, "utf-8");
33-
const lines = context.trimEnd().split("\n");
34-
if (options.nonBlank) {
35-
lines.forEach((line) => {
36-
if (line.length != 0) {
32+
try {
33+
const context = await fs.readFile(path, "utf-8");
34+
const lines = context.trimEnd().split("\n");
35+
if (options.nonBlank) {
36+
lines.forEach((line) => {
37+
if (line.trim().length != 0) {
38+
console.log(` ${count} ${line}`);
39+
count++;
40+
} else {
41+
console.log(line);
42+
}
43+
});
44+
} else if (options.line) {
45+
lines.forEach((line) => {
3746
console.log(` ${count} ${line}`);
3847
count++;
39-
} else {
40-
console.log(line);
41-
}
42-
});
43-
} else if (options.line) {
44-
lines.forEach((line) => {
45-
console.log(` ${count} ${line}`);
46-
count++;
47-
});
48-
} else {
49-
console.log(context.trimEnd());
48+
});
49+
} else {
50+
console.log(context.trimEnd());
51+
}
52+
} catch (error) {
53+
console.error(error.message);
54+
process.exitCode = 1;
5055
}
5156
}

0 commit comments

Comments
 (0)