summaryrefslogtreecommitdiff
path: root/src/helper/get-directory-contents.ts
blob: 80fb20cef9a793f9ad61887a45f3f6ab236dd2ae (plain) (blame)
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
/*** EXPORT ------------------------------------------- ***/

export default async(directory: string): Promise<{ filename: string; }[]> => {
  const documentArray: { filename: string; }[] = [];

  try {
    const documents: Deno.DirEntry[] = [];

    for await (const dirEntry of Deno.readDir(directory)) {
      if (dirEntry.isFile)
        documents.push(dirEntry);
    }

    documents.sort((a, b) => a.name.localeCompare(b.name)).reverse();

    for (const document of documents) {
      if (document.name.startsWith("."))
        return [];

      if (document.name.endsWith(".txt")) {
        const data = { filename: document.name };
        documentArray.push(data);
      }
    }
  } catch(error) {
    console.error(`Error reading directory contents: ${String(error)}`);
  } finally {
    // deno-lint-ignore no-unsafe-finally
    return documentArray;
  }
}