File size: 2,730 Bytes
90989cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { join } from '@std/path';

export function randomString(len: number) {
  return [...crypto.getRandomValues(new Uint8Array(len))].map((x, i) => (i = x / 255 * 61 | 0, String.fromCharCode(i + (i > 9 ? i > 35 ? 61 : 55 : 48)))).join('');
}

export async function getFilesInDirectory(dirPath: string): Promise<string[]> {
  const files: string[] = [];
  for await (const entry of Deno.readDir(dirPath)) {
    if (entry.isFile) {
      files.push(entry.name);
    }
  }
  return files;
}

export async function getDirectoryStructureString(
  dirPath: string,
  indent: string = '',
  maxDepth?: number,
  currentDepth: number = 0,
): Promise<string> {
  let structureString = ''; // Initialize the string to build

  // Stop if maxDepth is defined and reached
  if (maxDepth !== undefined && currentDepth > maxDepth) {
    return ''; // Return empty string if max depth exceeded
  }

  try {
    const entries = [];
    for await (const entry of Deno.readDir(dirPath)) {
      entries.push(entry);
    }

    // Sort entries alphabetically, directories first
    entries.sort((a, b) => {
      if (a.isDirectory && !b.isDirectory) return -1;
      if (!a.isDirectory && b.isDirectory) return 1;
      return a.name.localeCompare(b.name);
    });

    for (let i = 0; i < entries.length; i++) {
      const entry = entries[i];
      const isLast = i === entries.length - 1;
      const marker = isLast ? '└── ' : 'β”œβ”€β”€ ';
      const entryPath = join(dirPath, entry.name);

      // Append the current entry line to the string
      structureString += `${indent}${marker}${entry.name}${entry.isDirectory ? '/' : ''}\n`; // Add newline

      if (entry.isDirectory) {
        const nextIndent = indent + (isLast ? '    ' : 'β”‚   ');
        // Await the recursive call and append its result string
        const subStructure = await getDirectoryStructureString(entryPath, nextIndent, maxDepth, currentDepth + 1);
        structureString += subStructure;
      }
    }
  } catch (error) {
    // Append error messages to the string instead of logging them directly
    if (error instanceof Deno.errors.PermissionDenied) {
      structureString += `${indent}└── [Permission Denied for ${dirPath}]\n`;
    } else if (error instanceof Deno.errors.NotFound) {
      structureString += `${indent}└── [Directory Not Found: ${dirPath}]\n`;
    } else {
      // Append a generic error message; you might still want to log the full error separately
      structureString += `${indent}└── [Error reading ${dirPath}]\n`;
      console.error(`Error details during structure generation for ${dirPath}:`, error); // Optionally log full error
    }
  }
  return structureString; // Return the accumulated string
}