Repository navigation
Expand file tree
/
Copy pathprint.ts
More file actions
71 lines (59 loc) · 2.18 KB
/
Copy pathprint.ts
File metadata and controls
71 lines (59 loc) · 2.18 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
import { readdirSync, statSync } from "node:fs";
import { join } from "node:path";
// Exclude these directories
const excludedDirs = [".git", "node_modules"];
// Function to check if a directory contains `.html` files or subdirectories with `.html` files
function containsHtmlFiles(dir: string): boolean {
const items = readdirSync(dir);
for (let item of items) {
const fullPath = join(dir, item);
const isDirectory = statSync(fullPath).isDirectory();
if (isDirectory) {
if (excludedDirs.includes(item)) continue; // Skip excluded directories
if (containsHtmlFiles(fullPath)) return true; // Recursively check subdirectories
} else if (item.endsWith(".html")) {
return true; // Found an `.html` file
}
}
return false; // No `.html` files or valid subdirectories found
}
// Function to print the folder structure with directories first
function printFolderStructure(dir: string, level = 0) {
const indent = " ".repeat(level); // Indentation for hierarchy
const items = readdirSync(dir);
// Separate folders and files
const directories = [];
const files = [];
for (let item of items) {
const fullPath = join(dir, item);
const isDirectory = statSync(fullPath).isDirectory();
if (excludedDirs.includes(item)) continue; // Skip excluded directories
if (isDirectory) {
if (containsHtmlFiles(fullPath)) {
directories.push(item); // Only include directories that have `.html` files or subdirectories containing `.html`
}
} else if (item.endsWith(".html")) {
files.push(item); // Only include `.html` files
}
}
// Sort directories and files alphabetically
directories.sort();
files.sort();
// Print directories first
directories.forEach((directory) => {
console.log(`${indent}├── /${directory}/`);
printFolderStructure(join(dir, directory), level + 1);
});
// Print files next
files.forEach((file) => {
console.log(`${indent}├── /${file}`);
});
}
// Entry Point of CLI Tool
export function Print(targetDir = process.cwd()) {
console.log(`\nDocmach site structure\n`);
console.log("┬");
printFolderStructure(targetDir);
console.log("\nComplete!");
}