Skip to content

Commit 30170c8

Browse files
committed
feat: implement custom ls with -1 and -a support
1 parent 7e7ebfd commit 30170c8

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

implement-shell-tools/cat/customCat.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ program.parse();
1515
const argv = program.args;
1616
const options = program.opts();
1717

18-
let lineNumber = 1;
19-
2018
for (const filePath of argv) {
2119
try {
2220
const content = await fs.readFile(filePath, "utf-8");
@@ -27,8 +25,9 @@ for (const filePath of argv) {
2725
lines.forEach((line, index) => {
2826
if (options.nonBlank) {
2927
if (line.trim() !== "") {
30-
process.stdout.write(`${(index + 1).toString().padStart(6)} ${line}\n`);
31-
lineNumber++;
28+
process.stdout.write(
29+
`${(index + 1).toString().padStart(6)} ${line}\n`,
30+
);
3231
} else process.stdout.write(`${line}\n`);
3332
} else if (options.number) {
3433
process.stdout.write(`${(index + 1).toString().padStart(6)} ${line}\n`);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
3+
import process from "node:process";
4+
import { program } from "commander";
5+
import { promises as fs } from "node:fs";
6+
7+
program
8+
.name("customLs")
9+
.description("List contents of a directory")
10+
.option("-1", "Force output to be one entry per line")
11+
.option("-a", "Include hidden files")
12+
.argument("[path...]", "directories to list");
13+
14+
program.parse();
15+
16+
const options = program.opts();
17+
const targetPaths = program.args.length > 0 ? program.args : ["."];
18+
19+
async function listDir(dirPath, showHeader) {
20+
try {
21+
let contents = await fs.readdir(dirPath);
22+
23+
if (options.a) {
24+
contents.push(".", "..");
25+
} else {
26+
contents = contents.filter((name) => !name.startsWith("."));
27+
}
28+
29+
contents.sort();
30+
31+
if (showHeader) {
32+
process.stdout.write(`${dirPath}:\n`);
33+
}
34+
35+
if (options["1"]) {
36+
contents.forEach((item) => process.stdout.write(`${item}\n`));
37+
} else {
38+
process.stdout.write(`${contents.join(" ")}\n`);
39+
}
40+
} catch (error) {
41+
console.error(`customLs: ${dirPath}: ${error.message}`);
42+
process.exit(1);
43+
}
44+
}
45+
46+
for (let i = 0; i < targetPaths.length; i++) {
47+
const path = targetPaths[i];
48+
const isMultiplePath = targetPaths.length > 1;
49+
50+
await listDir(path, isMultiplePath);
51+
52+
if (isMultiplePath && i < targetPaths.length - 1) process.stdout.write(`\n`);
53+
}

implement-shell-tools/package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

implement-shell-tools/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"type": "module"
2+
"type": "module",
3+
"dependencies": {
4+
"commander": "^14.0.3"
5+
}
36
}

0 commit comments

Comments
 (0)