summaryrefslogtreecommitdiff
path: root/src/helper/get-documents.ts
blob: ca9ce939b8552704c3248c22768ee750357ceb47 (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
35
/*** EXPORT ------------------------------------------- ***/

export default async(directory: string) => {
  const posts: string[] = [];

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

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

    /*** Deno is weird in that if you do NOT call `.reverse()`
         it will NOT load everything in the directory…WTF?! ***/

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

    for (const file of files) {
      if (file.name.startsWith("."))
        return;

      if (file.name.endsWith(".txt"))
        posts.push(file.name);
    }
  } catch(error) {
    console.error(`Error reading directory for posts: ${String(error)}`);
  } finally {
    // deno-lint-ignore no-unsafe-finally
    return posts;
  }
}