| 15 | } |
| 16 | |
| 17 | async function ensureFileExists(filePath: string, content: string = '') { |
| 18 | try { |
| 19 | const uri = vscode.Uri.file(filePath); |
| 20 | |
| 21 | // Check if the directory exists, create if not |
| 22 | const dirPath = path.dirname(filePath); |
| 23 | try { |
| 24 | await vscode.workspace.fs.stat(vscode.Uri.file(dirPath)); |
| 25 | } catch { |
| 26 | // Directory doesn't exist, create it |
| 27 | await vscode.workspace.fs.createDirectory(vscode.Uri.file(dirPath)); |
| 28 | } |
| 29 | |
| 30 | // Check if file exists |
| 31 | try { |
| 32 | await vscode.workspace.fs.stat(uri); |
| 33 | } catch { |
| 34 | // File doesn't exist, create it |
| 35 | await vscode.workspace.fs.writeFile(uri, Buffer.from(content, 'utf8')); |
| 36 | } |
| 37 | |
| 38 | return uri; |
| 39 | } catch (error) { |
| 40 | vscode.window.showErrorMessage(`Error ensuring file exists: ${error}`); |
| 41 | throw error; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | function initWs(context: vscode.ExtensionContext) { |