|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as path from 'path' |
| 3 | +import { Logger } from '@shared/utils/logger' |
| 4 | +import { t } from '@/i18n' |
| 5 | +import { create_safe_path } from '@/utils/path-sanitizer' |
| 6 | +import { uri_exists } from './uri-exists' |
| 7 | + |
| 8 | +export const create_file_if_needed = async (params: { |
| 9 | + file_path: string |
| 10 | + content: string |
| 11 | + workspace_name?: string |
| 12 | +}): Promise<boolean> => { |
| 13 | + Logger.info({ |
| 14 | + function_name: 'create_file_if_needed', |
| 15 | + message: 'start', |
| 16 | + data: { filePath: params.file_path, workspace_name: params.workspace_name } |
| 17 | + }) |
| 18 | + if ( |
| 19 | + !vscode.workspace.workspaceFolders || |
| 20 | + vscode.workspace.workspaceFolders.length == 0 |
| 21 | + ) { |
| 22 | + vscode.window.showErrorMessage( |
| 23 | + t('command.apply-response.error.no-workspace-folder') |
| 24 | + ) |
| 25 | + Logger.warn({ |
| 26 | + function_name: 'create_file_if_needed', |
| 27 | + message: 'No workspace folder open.' |
| 28 | + }) |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + let workspace_folder_path: string | undefined |
| 33 | + |
| 34 | + if (params.workspace_name) { |
| 35 | + const target_workspace = vscode.workspace.workspaceFolders.find( |
| 36 | + (folder) => folder.name == params.workspace_name |
| 37 | + ) |
| 38 | + if (target_workspace) { |
| 39 | + workspace_folder_path = target_workspace.uri.fsPath |
| 40 | + } else { |
| 41 | + Logger.warn({ |
| 42 | + function_name: 'create_file_if_needed', |
| 43 | + message: `Workspace named "${params.workspace_name}" not found. Falling back to the first workspace.`, |
| 44 | + data: params.file_path |
| 45 | + }) |
| 46 | + workspace_folder_path = vscode.workspace.workspaceFolders[0].uri.fsPath |
| 47 | + } |
| 48 | + } else { |
| 49 | + workspace_folder_path = vscode.workspace.workspaceFolders[0].uri.fsPath |
| 50 | + } |
| 51 | + |
| 52 | + const safe_path = create_safe_path(workspace_folder_path, params.file_path) |
| 53 | + |
| 54 | + if (!safe_path) { |
| 55 | + vscode.window.showErrorMessage( |
| 56 | + t('command.apply-response.error.invalid-file-path-traversal', { |
| 57 | + path: params.file_path |
| 58 | + }) |
| 59 | + ) |
| 60 | + Logger.error({ |
| 61 | + function_name: 'create_file_if_needed', |
| 62 | + message: 'Invalid file path', |
| 63 | + data: params.file_path |
| 64 | + }) |
| 65 | + return false |
| 66 | + } |
| 67 | + const file_uri = vscode.Uri.file(safe_path) |
| 68 | + const directory_uri = vscode.Uri.file(path.dirname(safe_path)) |
| 69 | + |
| 70 | + if (!(await uri_exists(directory_uri))) { |
| 71 | + try { |
| 72 | + await vscode.workspace.fs.createDirectory(directory_uri) |
| 73 | + Logger.info({ |
| 74 | + function_name: 'create_file_if_needed', |
| 75 | + message: 'Directory created', |
| 76 | + data: directory_uri.fsPath |
| 77 | + }) |
| 78 | + } catch (error) { |
| 79 | + Logger.error({ |
| 80 | + function_name: 'create_file_if_needed', |
| 81 | + message: 'Failed to create directory', |
| 82 | + data: { directory: directory_uri.fsPath, error } |
| 83 | + }) |
| 84 | + vscode.window.showErrorMessage( |
| 85 | + t('command.apply-response.error.failed-to-create-directory', { |
| 86 | + path: directory_uri.fsPath |
| 87 | + }) |
| 88 | + ) |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + await vscode.workspace.fs.writeFile( |
| 95 | + file_uri, |
| 96 | + Buffer.from(params.content, 'utf8') |
| 97 | + ) |
| 98 | + Logger.info({ |
| 99 | + function_name: 'create_file_if_needed', |
| 100 | + message: 'File created', |
| 101 | + data: safe_path |
| 102 | + }) |
| 103 | + return true |
| 104 | + } catch (error) { |
| 105 | + Logger.error({ |
| 106 | + function_name: 'create_file_if_needed', |
| 107 | + message: 'Failed to write file', |
| 108 | + data: { safe_path, error } |
| 109 | + }) |
| 110 | + vscode.window.showErrorMessage( |
| 111 | + t('command.apply-response.error.failed-to-write-file', { |
| 112 | + path: safe_path |
| 113 | + }) |
| 114 | + ) |
| 115 | + } |
| 116 | + return false |
| 117 | +} |
0 commit comments