| 40 | * and build pipelines where persistence is not required. |
| 41 | */ |
| 42 | export class InMemoryFileSystem implements FileSystem { |
| 43 | private files: Map<string, string> = new Map(); |
| 44 | |
| 45 | /** |
| 46 | * @param files Optional initial file contents. Accepts either a plain object |
| 47 | * (keys are paths, values are file contents) or a `Map`. Defaults to an |
| 48 | * empty filesystem. |
| 49 | */ |
| 50 | constructor(files: Record<string, string> | Map<string, string> = new Map()) { |
| 51 | this.files = files instanceof Map ? files : new Map(Object.entries(files)); |
| 52 | } |
| 53 | |
| 54 | read(path: string): string | null { |
| 55 | return this.files.get(path) ?? null; |
| 56 | } |
| 57 | |
| 58 | write(path: string, content: string): void { |
| 59 | this.files.set(path, content); |
| 60 | } |
| 61 | |
| 62 | delete(path: string): void { |
| 63 | this.files.delete(path); |
| 64 | } |
| 65 | |
| 66 | list(prefix?: string): string[] { |
| 67 | return Array.from(this.files.keys()).filter( |
| 68 | (path) => prefix === undefined || path.startsWith(prefix) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | flush(): Promise<void> { |
| 73 | return Promise.resolve(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * A generic write-overlay on top of any `FileSystem`. Not re-exported from the |
nothing calls this directly
no outgoing calls
no test coverage detected