-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathls.js
More file actions
44 lines (36 loc) · 929 Bytes
/
ls.js
File metadata and controls
44 lines (36 loc) · 929 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
41
42
43
44
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
function listFiles(directory, options) {
try {
const files = fs.readdirSync(directory, { withFileTypes: true });
files.forEach((file) => {
if (!options.all && file.name.startsWith('.')) {
return; // Skip hidden files unless -a is specified
}
console.log(file.name);
});
} catch (err) {
console.error(`ls: cannot access '${directory}': No such file or directory`);
}
}
function main() {
const args = process.argv.slice(2);
const options = {
all: false,
};
let directories = ['.'];
args.forEach((arg) => {
if (arg === '-1') {
// -1 is the default behavior, so no action needed
} else if (arg === '-a') {
options.all = true;
} else {
directories = [arg];
}
});
directories.forEach((directory) => {
listFiles(directory, options);
});
}
main();