Skip to content

Commit 7e70d1c

Browse files
committed
Refactor file handling and error messages in cat, ls, and wc tools
1 parent daac5ec commit 7e70d1c

3 files changed

Lines changed: 32 additions & 33 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ function cat(files, options) {
77
let lineNumber = 1;
88

99
files.forEach((file) => {
10-
const filePath = path.resolve(file);
11-
1210
try {
13-
const data = fs.readFileSync(filePath, 'utf8');
11+
const data = fs.readFileSync(file, 'utf8');
1412
const lines = data.split('\n');
1513

1614
lines.forEach((line) => {
17-
if (options.numberNonEmpty && line.trim()) {
18-
console.log(`${lineNumber}\t${line}`);
19-
lineNumber++;
20-
} else if (options.numberLines) {
21-
console.log(`${lineNumber}\t${line}`);
22-
lineNumber++;
23-
} else {
24-
console.log(line);
25-
}
15+
const prefix = options.numberNonEmpty && line.trim() ? `${lineNumber}\t` : options.numberLines ? `${lineNumber}\t` : '';
16+
console.log(`${prefix}${line}`);
17+
if (prefix) lineNumber++;
2618
});
2719
} catch (err) {
28-
console.error(`cat: ${file}: No such file or directory`);
20+
if (err.code === 'ENOENT') {
21+
console.error(`cat: ${file}: No such file or directory`);
22+
} else if (err.code === 'EACCES') {
23+
console.error(`cat: ${file}: Permission denied`);
24+
} else {
25+
console.error(`cat: ${file}: An error occurred`);
26+
}
27+
process.exit(1);
2928
}
3029
});
3130
}

implement-shell-tools/ls/ls.js

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

33
const fs = require('fs');
4-
const path = require('path');
54

65
function listFiles(directory, options) {
76
try {
@@ -11,10 +10,11 @@ function listFiles(directory, options) {
1110
if (!options.all && file.name.startsWith('.')) {
1211
return; // Skip hidden files unless -a is specified
1312
}
14-
console.log(file.name);
13+
console.log(`${directory}/${file.name}`);
1514
});
1615
} catch (err) {
17-
console.error(`ls: cannot access '${directory}': No such file or directory`);
16+
console.error(`ls: cannot access '${directory}': ${err.code === 'ENOENT' ? 'No such file or directory' : 'An error occurred'}`);
17+
process.exit(1);
1818
}
1919
}
2020

@@ -24,18 +24,22 @@ function main() {
2424
all: false,
2525
};
2626

27-
let directories = ['.'];
27+
const directories = [];
2828

2929
args.forEach((arg) => {
3030
if (arg === '-1') {
3131
// -1 is the default behavior, so no action needed
3232
} else if (arg === '-a') {
3333
options.all = true;
3434
} else {
35-
directories = [arg];
35+
directories.push(arg);
3636
}
3737
});
3838

39+
if (directories.length === 0) {
40+
directories.push('.');
41+
}
42+
3943
directories.forEach((directory) => {
4044
listFiles(directory, options);
4145
});

implement-shell-tools/wc/wc.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
#!/usr/bin/env node
22

33
const fs = require('fs');
4-
const path = require('path');
54

6-
function countFile(filePath, options) {
5+
function countFile(file, options) {
76
try {
8-
const data = fs.readFileSync(filePath, 'utf8');
7+
const data = fs.readFileSync(file, 'utf8');
98

109
const lines = data.split('\n').length;
1110
const words = data.split(/\s+/).filter(Boolean).length;
1211
const bytes = Buffer.byteLength(data, 'utf8');
1312

14-
if (options.lines) {
15-
console.log(`${lines}\t${filePath}`);
16-
} else if (options.words) {
17-
console.log(`${words}\t${filePath}`);
18-
} else if (options.bytes) {
19-
console.log(`${bytes}\t${filePath}`);
20-
} else {
21-
console.log(`${lines}\t${words}\t${bytes}\t${filePath}`);
22-
}
13+
const results = [];
14+
if (options.lines) results.push(lines);
15+
if (options.words) results.push(words);
16+
if (options.bytes) results.push(bytes);
17+
18+
console.log(`${results.join('\t')}\t${file}`);
2319
} catch (err) {
24-
console.error(`wc: ${filePath}: No such file or directory`);
20+
console.error(`wc: ${file}: ${err.code === 'ENOENT' ? 'No such file or directory' : 'An error occurred'}`);
21+
process.exit(1);
2522
}
2623
}
2724

@@ -53,8 +50,7 @@ function main() {
5350
}
5451

5552
files.forEach((file) => {
56-
const filePath = path.resolve(file);
57-
countFile(filePath, options);
53+
countFile(file, options);
5854
});
5955
}
6056

0 commit comments

Comments
 (0)