diff options
Diffstat (limited to 'src/helper/get-documents.ts')
| -rw-r--r-- | src/helper/get-documents.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/helper/get-documents.ts b/src/helper/get-documents.ts new file mode 100644 index 0000000..ca9ce93 --- /dev/null +++ b/src/helper/get-documents.ts @@ -0,0 +1,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; + } +} |
