| 13 | * If path is a directory, return an object with directory's contents. |
| 14 | */ |
| 15 | export async function readFileSystem( |
| 16 | path: string, |
| 17 | ): Promise<FileSystemEntity | undefined> { |
| 18 | if (!fs.existsSync(path)) return undefined; |
| 19 | if (fs.statSync(path).isDirectory()) { |
| 20 | const directory: FileSystemDirectory = {}; |
| 21 | for (const file of fs.readdirSync(path)) { |
| 22 | const content = await readFileSystem(join(path, file)); |
| 23 | if (content) directory[file] = content; |
| 24 | } |
| 25 | return directory; |
| 26 | } else { |
| 27 | const file = await open(path, "r"); |
| 28 | const content = await file.readFile("utf-8"); |
| 29 | file.close(); |
| 30 | return content; |
| 31 | } |
| 32 | } |