-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.js
More file actions
48 lines (39 loc) · 1.03 KB
/
ls.js
File metadata and controls
48 lines (39 loc) · 1.03 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
#!/usr/bin/env node
const fs = require('fs');
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(`${directory}/${file.name}`);
});
} catch (err) {
console.error(`ls: cannot access '${directory}': ${err.code === 'ENOENT' ? 'No such file or directory' : 'An error occurred'}`);
process.exit(1);
}
}
function main() {
const args = process.argv.slice(2);
const options = {
all: false,
};
const 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.push(arg);
}
});
if (directories.length === 0) {
directories.push('.');
}
directories.forEach((directory) => {
listFiles(directory, options);
});
}
main();