generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathls.js
37 lines (30 loc) · 930 Bytes
/
ls.js
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
const fs = require('fs');
const { program } = require('commander');
program
.option('-a, --all', 'Show hidden files')
.option('-1', 'Show one file per line')
.argument('[dirPath]', 'Directory path', '.')
.parse(process.argv);
const { all, '1': onePerLine } = program.opts();
const dirPath = program.args[0] || '.';
if (typeof dirPath !== 'string') {
console.error('Error: Invalid directory path');
process.exit(1);
}
function listDirectoryContents(dirPath, showHidden = false, onePerLine = false) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err.message}`);
return;
}
if (!showHidden) {
files = files.filter(file => !file.startsWith('.'));
}
if (onePerLine) {
files.forEach(file => console.log(file));
} else {
console.log(files.join(' '));
}
});
}
listDirectoryContents(dirPath, all, onePerLine);