| 155 | * An implementation of the Virtual FS using Node as the backend, synchronously. |
| 156 | */ |
| 157 | export class NodeJsSyncHost implements virtualFs.Host<Stats> { |
| 158 | get capabilities(): virtualFs.HostCapabilities { |
| 159 | return { synchronous: true }; |
| 160 | } |
| 161 | |
| 162 | write(path: Path, content: virtualFs.FileBuffer): Observable<void> { |
| 163 | return new Observable((obs) => { |
| 164 | mkdirSync(getSystemPath(dirname(path)), { recursive: true }); |
| 165 | writeFileSync(getSystemPath(path), new Uint8Array(content)); |
| 166 | obs.next(); |
| 167 | obs.complete(); |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | read(path: Path): Observable<virtualFs.FileBuffer> { |
| 172 | return new Observable((obs) => { |
| 173 | const buffer = readFileSync(getSystemPath(path)); |
| 174 | |
| 175 | obs.next(new Uint8Array(buffer).buffer); |
| 176 | obs.complete(); |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | delete(path: Path): Observable<void> { |
| 181 | return new Observable<void>((obs) => { |
| 182 | rmSync(getSystemPath(path), { force: true, recursive: true, maxRetries: 3 }); |
| 183 | |
| 184 | obs.complete(); |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | rename(from: Path, to: Path): Observable<void> { |
| 189 | return new Observable((obs) => { |
| 190 | const toSystemPath = getSystemPath(to); |
| 191 | mkdirSync(pathDirname(toSystemPath), { recursive: true }); |
| 192 | renameSync(getSystemPath(from), toSystemPath); |
| 193 | obs.next(); |
| 194 | obs.complete(); |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | list(path: Path): Observable<PathFragment[]> { |
| 199 | return new Observable((obs) => { |
| 200 | const names = readdirSync(getSystemPath(path)); |
| 201 | obs.next(names.map((name) => fragment(name))); |
| 202 | obs.complete(); |
| 203 | }); |
| 204 | } |
| 205 | |
| 206 | exists(path: Path): Observable<boolean> { |
| 207 | return new Observable((obs) => { |
| 208 | obs.next(existsSync(getSystemPath(path))); |
| 209 | obs.complete(); |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | isDirectory(path: Path): Observable<boolean> { |
| 214 | return this.stat(path).pipe(map((stat) => stat.isDirectory())); |
nothing calls this directly
no outgoing calls
no test coverage detected