-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.mjs
More file actions
37 lines (30 loc) · 819 Bytes
/
ls.mjs
File metadata and controls
37 lines (30 loc) · 819 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
import { program } from "commander";
import { promises as fs } from "fs";
import process from "process";
program
.name("ls")
.description("Lists the files in a directory")
.option("-1, --one", "One per line")
.option("-a, --all", "Include files starting with dot")
.argument("<path>", "Directory to list");
program.parse(process.argv);
const args = program.args;
const opts = program.opts();
if (args.length !== 1) {
console.error("Expected 1 argument");
process.exit(1);
}
try {
let files = await fs.readdir(args[0]);
if (!opts.all) {
files = files.filter(f => !f.startsWith('.'));
}
files.sort();
if (opts.one) {
console.log(files.join('\n'));
} else {
console.log(files.join(' '));
}
} catch (err) {
console.error(`ls: cannot access '${args[0]}': ${err.message}`);
}