()
| 55 | }; |
| 56 | |
| 57 | export function makeDefaultFs(): Fs { |
| 58 | return { |
| 59 | async mkdir(path, options) { |
| 60 | await fs.promises.mkdir(path, options); |
| 61 | }, |
| 62 | pathExists, |
| 63 | readFile(path) { |
| 64 | return fs.promises.readFile(path, "utf-8"); |
| 65 | }, |
| 66 | async rm(path, options) { |
| 67 | await fs.promises.rm(path, options); |
| 68 | }, |
| 69 | stat(path) { |
| 70 | return fs.promises.stat(path); |
| 71 | }, |
| 72 | async unlink(path) { |
| 73 | await fs.promises.unlink(path); |
| 74 | }, |
| 75 | async writeFile(path, data, options) { |
| 76 | if (typeof data === "string") { |
| 77 | await fs.promises.writeFile(path, data, { |
| 78 | encoding: "utf8", |
| 79 | mode: options?.mode, |
| 80 | }); |
| 81 | } else { |
| 82 | await fs.promises.writeFile(path, data, options ?? undefined); |
| 83 | } |
| 84 | }, |
| 85 | readdir(path) { |
| 86 | return fs.promises.readdir(path); |
| 87 | }, |
| 88 | readdirWithTypes(path) { |
| 89 | // fs.promises.readdir returns Dirent[]; cast satisfies Fs's FsDirent[] return |
| 90 | return fs.promises.readdir(path, { |
| 91 | withFileTypes: true, |
| 92 | }) as Promise<FsDirent[]>; |
| 93 | }, |
| 94 | rename(oldPath, newPath) { |
| 95 | return fs.promises.rename(oldPath, newPath); |
| 96 | }, |
| 97 | utimes(path, atime, mtime) { |
| 98 | return fs.promises.utimes(path, atime, mtime); |
| 99 | }, |
| 100 | createReadStream(path) { |
| 101 | // Pre-check so missing-file throws synchronously, matching makeMemoryFs contract |
| 102 | if (!fs.existsSync(path)) { |
| 103 | const err = Object.assign( |
| 104 | new Error(`ENOENT: no such file or directory, open '${path}'`), |
| 105 | { code: "ENOENT", errno: -2 }, |
| 106 | ); |
| 107 | throw err; |
| 108 | } |
| 109 | return fs.createReadStream(path); |
| 110 | }, |
| 111 | async copyFile(source, destination) { |
| 112 | await fs.promises.copyFile(source, destination); |
| 113 | }, |
| 114 | existsSync(path) { |
no outgoing calls