-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathscript-ls.js
More file actions
40 lines (34 loc) · 928 Bytes
/
script-ls.js
File metadata and controls
40 lines (34 loc) · 928 Bytes
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
#!/usr/bin/env node
import { program } from "commander";
import fs from "fs";
import path from "path";
// Define CLI
program
.name("ls-clone")
.description("A simple implementation of ls")
.option("-1", "list one file per line")
.option("-a", "include hidden files")
.argument("[dirs...]", "directories to list", "."); // default is current dir
program.parse();
const options = program.opts();
const dirs = program.args.length ? program.args : ["."];
const onePerLine = options["1"];
const showAll = options.a;
for (const dir of dirs) {
let files;
try {
files = fs.readdirSync(dir);
} catch (err) {
console.error(`ls-clone: cannot access '${dir}': No such file or directory`);
continue;
}
if (!showAll) {
files = files.filter(name => !name.startsWith("."));
}
// Output
if (onePerLine) {
files.forEach(f => console.log(f));
} else {
console.log(files.join(" "));
}
}