()
| 6 | * Facade on top of MemFS file system provider for more convenient programmatic use. |
| 7 | */ |
| 8 | export const FileSystem = () => { |
| 9 | const fs = new MemFS(); |
| 10 | const encoder = new TextEncoder(); |
| 11 | const decoder = new TextDecoder(); |
| 12 | |
| 13 | return { |
| 14 | register(context: vscode.ExtensionContext) { |
| 15 | context.subscriptions.push( |
| 16 | vscode.workspace.registerFileSystemProvider("memfs", fs, { |
| 17 | isCaseSensitive: true, |
| 18 | }) |
| 19 | ); |
| 20 | }, |
| 21 | writeFile(path: string, content: string) { |
| 22 | if (!path.startsWith("/")) path = "/" + path; |
| 23 | |
| 24 | // const uri = vscode.Uri.from({ scheme: "memfs", path: path }); |
| 25 | const uri = vscode.Uri.from({ scheme: "memfs", path: path }); |
| 26 | |
| 27 | fs.writeFile(uri, encoder.encode(content), { |
| 28 | create: true, |
| 29 | overwrite: true, |
| 30 | }); |
| 31 | }, |
| 32 | readFile(path: string): string { |
| 33 | if (!path.startsWith("/")) path = "/" + path; |
| 34 | |
| 35 | const buffer = fs.readFile( |
| 36 | vscode.Uri.from({ scheme: "memfs", path: path }) |
| 37 | ); |
| 38 | |
| 39 | return decoder.decode(buffer); |
| 40 | }, |
| 41 | onDidChangeFile: fs.onDidChangeFile.bind(fs), |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | export type FileSystem = ReturnType<typeof FileSystem>; |
no outgoing calls
no test coverage detected