* This reads a directory and returns a map of the files and folders in it * It is quite tolerant of errors and reentrancy, so if multiple callers are trying to get the same results * it will only do the work once per directory * * @param fullPath the full path of the folder to read * @para
(fullPath: string, executableExtensions: Set<string> = process.platform === 'win32' ? new Set(['.exe'/* ,'.cmd','.bat' */]) : new Set())
| 32 | * @returns a map of the files and folders in the directory, or undefined if the directory doesn't exist or is inaccessible. |
| 33 | */ |
| 34 | async function readDirectory(fullPath: string, executableExtensions: Set<string> = process.platform === 'win32' ? new Set(['.exe'/* ,'.cmd','.bat' */]) : new Set()): Promise<Map<string, File | FolderWithChildren> | undefined> { |
| 35 | // have we already read this directory? |
| 36 | let folder = cache.get(fullPath) as FolderWithChildren | undefined; |
| 37 | let promise: ManualPromise<FolderWithChildren | undefined> | undefined; |
| 38 | |
| 39 | if (!folder) { |
| 40 | // create a promise and insert it into the cache, so if something else comes looking before we do any async, they can await that |
| 41 | promise = new ManualPromise<FolderWithChildren | undefined>(); |
| 42 | cache.set(fullPath, promise); |
| 43 | |
| 44 | const stats = await stat(fullPath).catch(returns.undefined); |
| 45 | |
| 46 | if (!stats?.isDirectory()) { |
| 47 | // no results, return undefined. |
| 48 | promise.resolve(folder); |
| 49 | return undefined; |
| 50 | } |
| 51 | |
| 52 | folder = { |
| 53 | name: basename(fullPath), |
| 54 | fullPath, |
| 55 | isFolder: true, |
| 56 | isFile: false, |
| 57 | isLink: stats.isSymbolicLink() |
| 58 | } as FolderWithChildren; |
| 59 | } |
| 60 | |
| 61 | // if we are waiting on a promise |
| 62 | if (is.promise(folder)) { |
| 63 | folder = await folder; |
| 64 | } |
| 65 | |
| 66 | // if the target isn't a folder, it can't have children |
| 67 | if (!folder?.isFolder) { |
| 68 | return undefined; |
| 69 | } |
| 70 | |
| 71 | // if we haven't scanned this folder yet, do so now. |
| 72 | if (!folder.children) { |
| 73 | folder.children = new Map(); |
| 74 | |
| 75 | if (!is.promise(promise)) { |
| 76 | // if we didn't already have a promise, create one now. |
| 77 | // this can happen when the parent has scanned and added in the child but nobody has asked for the children yet. |
| 78 | promise = new ManualPromise<FolderWithChildren | undefined>(); |
| 79 | cache.set(fullPath, promise); |
| 80 | } |
| 81 | |
| 82 | // this doesn't use the path.info function because in this case, the direntry is already available, and on Windows we can skip a call to stat (which is expensive) |
| 83 | // process all the entries, and add them to the cache and the children map |
| 84 | await foreach(readdir(fullPath, { withFileTypes: true }).catch(returns.none), async (direntry: Dirent) => { |
| 85 | const name = direntry.name; |
| 86 | const fp = `${fullPath}${sep}${name}`; |
| 87 | if (cache.has(fp)) { |
| 88 | return; |
| 89 | } |
| 90 | // create the entry |
| 91 | const entry = { |