diff options
| author | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
| commit | 7aba3deae3ff17d032c37e07d2929d1426b34278 (patch) | |
| tree | 9c482f9acda1599bd2f9bed95a6a46b75e634e80 /plugins/code-modernization/hooks/reader/fs.ts | |
| parent | 5472f5f56e0070feeeb464318b316714fe3e9105 (diff) | |
| parent | b8331dd69ab3e132bbf8cd6e89b5d8c1747e21b1 (diff) | |
Merge remote-tracking branch 'origin/main' into add-plugin/xcodeadd-plugin/xcode
Diffstat (limited to 'plugins/code-modernization/hooks/reader/fs.ts')
| -rw-r--r-- | plugins/code-modernization/hooks/reader/fs.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/reader/fs.ts b/plugins/code-modernization/hooks/reader/fs.ts new file mode 100644 index 00000000..1d8e9f6d --- /dev/null +++ b/plugins/code-modernization/hooks/reader/fs.ts @@ -0,0 +1,79 @@ +/** + * The file access the reader needs, narrow enough that `$.fs` and a test's + * in-memory tree both satisfy it. Paths are relative to the session's + * working directory or absolute, as `$.fs` takes them. + */ +export type ReaderFs = { + read: (path: string) => Promise<string> + /** An entry as it stands: a symbolic link is `other` (with `isLink`), whatever it leads to. `stat` says what that is. */ + list: (path: string) => Promise<{ name: string; kind: 'file' | 'dir' | 'other'; size: number; isLink?: boolean }[]> + exists: (path: string) => Promise<boolean> + stat: (path: string) => Promise<{ kind: 'file' | 'dir' | 'other'; size: number; mtimeMs: number }> +} + +/** `read`, or null when the file is missing, unreadable or over the size cap. */ +export async function readOrNull(fs: ReaderFs, path: string): Promise<string | null> { + try { + // `exists` never rejects; asking first keeps a missing optional file out of the error log. + if (!(await fs.exists(path))) { + return null + } + + return await fs.read(path) + } catch { + return null + } +} + +/** `list`, or an empty list when the directory is missing. */ +export async function listOrEmpty( + fs: ReaderFs, + path: string, +): Promise<{ name: string; kind: 'file' | 'dir' | 'other'; size: number; isLink?: boolean }[]> { + try { + if (!(await fs.exists(path))) { + return [] + } + + return await fs.list(path) + } catch { + return [] + } +} + +/** `stat`'s mtime, or null when the path is missing. */ +export async function mtimeOrNull(fs: ReaderFs, path: string): Promise<number | null> { + try { + if (!(await fs.exists(path))) { + return null + } + + return (await fs.stat(path)).mtimeMs + } catch { + return null + } +} + +/** + * The directories under `path`, by name. A symbolic link to a directory counts: the engine lists a link as `other`, + * and a legacy tree is often a link to where the code really lives. + */ +export async function dirNamesOf(fs: ReaderFs, path: string): Promise<string[]> { + const names: string[] = [] + + for (const entry of await listOrEmpty(fs, path)) { + if (entry.kind === 'dir') { + names.push(entry.name) + } else if (entry.kind === 'other') { + try { + if ((await fs.stat(`${path}/${entry.name}`)).kind === 'dir') { + names.push(entry.name) + } + } catch { + // a link that leads nowhere is not a system + } + } + } + + return names +} |
