-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathls.js
More file actions
48 lines (34 loc) · 1.04 KB
/
Copy pathls.js
File metadata and controls
48 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import process from "node:process";
import { promises as fs } from "node:fs";
import { program } from "commander";
program
.name("list-files-in-directory")
.description("List all files and directories in a directory")
.argument("<path>", "The file path to process")
.option("-1, --one", "Output one entry per line")
.option("-a", "List all files & directories, including hidden ones");
program.parse();
const path = program.args[0];
const options = program.opts();
if (!path) {
console.error("Error: No directory path was provided.");
process.exit(1);
}
let directoryContent;
try {
directoryContent = await fs.readdir(path);
} catch (err) {
console.error(`Error reading directory: ${err.message}`);
process.exit(1);
}
let allContent = directoryContent;
if (!options.a) {
allContent = directoryContent.filter(name => !name.startsWith("."));
}
for (const item of allContent) {
if (options.one) {
process.stdout.write(item + "\n");
} else {
process.stdout.write(item + " ");
}
}