-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-reader.ts
More file actions
46 lines (41 loc) · 1.05 KB
/
Copy pathfile-reader.ts
File metadata and controls
46 lines (41 loc) · 1.05 KB
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
36
37
38
39
40
41
42
43
44
45
46
/**
* file-reader.ts — read source file contents from disk.
*
* File discovery lives in scan/file-scanner.ts.
*/
import { readFile, stat } from 'node:fs/promises'
import type { Result } from '../model/result.js'
import { err, ok } from '../model/result.js'
export class FileReaderError extends Error {
constructor(
message: string,
public override readonly cause: unknown,
) {
super(message)
this.name = 'FileReaderError'
}
}
/**
* Read the content of a single file.
*/
export async function readFileContent(
absolutePath: string,
): Promise<Result<string, FileReaderError>> {
try {
const content = await readFile(absolutePath, 'utf-8')
return ok(content)
} catch (cause) {
return err(new FileReaderError(`Could not read file: ${absolutePath}`, cause))
}
}
/**
* Get file size in bytes. Returns undefined if stat fails.
*/
export async function getFileSize(absolutePath: string): Promise<number | undefined> {
try {
const info = await stat(absolutePath)
return info.size
} catch {
return undefined
}
}