-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirmap
executable file
·105 lines (90 loc) · 3.08 KB
/
dirmap
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Helper function to display help message
function displayHelp() {
console.log(`
Usage: tree [options] [directory]
Options:
-h, --help Show this help message and exit
-a, --all Include hidden files in the output
[number] Specify the maximum depth of the directory tree
[directory] Specify the root directory (default is current directory)
-[folder] Ignore the specified folder (prefix with '-' for each folder to ignore)
Examples:
dirmap Display directory tree with default settings
dirmap -a Display directory tree including hidden files
dirmap 2 Display directory tree up to 2 levels deep
dirmap /path/to/dir Display directory tree for the specified directory
dirmap -node_modules Ignore the 'node_modules' folder
`);
}
// Helper function to parse command-line arguments
function parseArgs() {
const args = process.argv.slice(2);
const options = {
maxLevel: 1,
rootDir: '.',
showHidden: false,
ignoreFolders: [],
currentLevel: 0,
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '-h' || args[i] === '--help') {
displayHelp();
process.exit(0);
} else if (args[i] === '-a' || args[i] === '--all') {
options.showHidden = true;
} else if (args[i].startsWith('-')) {
options.ignoreFolders.push(args[i].substring(1)); // Remove the '-' prefix
} else if (!isNaN(parseInt(args[i]))) {
options.maxLevel = parseInt(args[i]);
} else {
options.rootDir = args[i];
}
}
return options;
}
function printDirectoryContents(options) {
const { rootDir, currentLevel, maxLevel, showHidden, ignoreFolders } = options;
const files = fs.readdirSync(rootDir);
const indent = '│ '.repeat(currentLevel);
const dirName = path.basename(rootDir);
//console.log(`${indent}├── ${dirName}`);
for (let i = 0; i < files.length; i++) {
const file = files[i];
// Ignore dot files if showHidden is false
if (!showHidden && file.startsWith('.')) {
continue;
}
// Ignore specified folders
if (ignoreFolders.includes(file)) {
continue;
}
const filePath = path.join(rootDir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory() && currentLevel < maxLevel) {
if (i === files.length - 1) {
console.log(`${indent}└── ${file}`);
printDirectoryContents({ ...options, rootDir: filePath, currentLevel: currentLevel + 1 });
} else {
console.log(`${indent}├── ${file}`);
printDirectoryContents({ ...options, rootDir: filePath, currentLevel: currentLevel + 1 });
}
} else {
if (i === files.length - 1) {
console.log(`${indent}└── ${file}`);
} else {
console.log(`${indent}├── ${file}`);
}
}
}
}
try {
const options = parseArgs();
printDirectoryContents(options);
} catch (err) {
console.error(`Error parsing arguments: ${err.message}`);
displayHelp();
process.exit(1);
}