generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathls.js
44 lines (36 loc) · 2.01 KB
/
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
38
39
40
41
42
43
44
//note to myself:
// path.resolve() converts a relative path into an absolute path.
//The last argument (dir) is resolved relative to the first argument.
//syntax: path.resolve(...paths);
//process.cwd() Returns the current working directory of the process (i.e., where the user ran the command from).It does NOT return the script’s directory.
//targetDir is created to store this resolved absolute path for reliable use in fs.readdirSync().
import fs from "fs/promises";
import { Command } from "commander";
import path from "path";
const program = new Command();
program
.argument('[directory]', 'Directory to list', '.')//This specifies the directory argument. If no directory is provided, it defaults to the current directory (.)
.option("-l, --format", "List files in a single column") // Add -1 flag
.option("-a, --hidden", "Show hidden files (like ls -a)") // Add -a flag
.action(async (directory) => {
const resolvedPath = path.resolve(directory);
const options = program.opts();
try {
const files = await fs.readdir(resolvedPath, { withFileTypes: true }); // files is an array of files inside the directory
//{ withFileTypes: true } returns file objects, allowing you to filter out hidden files if needed.
// If `-a` is NOT set, filter out hidden files (those starting with ".")
let filteredFiles = files.map(file => file.name);
if(!options.hidden){
filteredFiles = filteredFiles.filter(file => !file.startsWith("."));
}
if(options.format){
console.log(filteredFiles.join("\n"))// Print files in a single column (default `ls -1` behavior)
}else{
console.log(filteredFiles.join(" "))// Print files space-separated (default `ls` behavior)
}
} catch (error) {
console.error(`Error reading directory: ${error.message}`);
process.exit(1);
}
})
program.parse(process.argv);