diff --git a/src/services/native/index.ts b/src/services/native/index.ts index cc2a8a2a..1aeb5657 100644 --- a/src/services/native/index.ts +++ b/src/services/native/index.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/strict-boolean-expressions */ /* eslint-disable @typescript-eslint/require-await */ import { app, dialog, ipcMain, MessageBoxOptions, shell } from 'electron'; import fs from 'fs-extra'; @@ -278,6 +277,21 @@ ${message.message} } } + public async moveToTrash(filePath: string): Promise { + if (!filePath) { + logger.error('NativeService.moveToTrash() filePath is empty', { filePath }); + return false; + } + logger.debug(`NativeService.moveToTrash() Moving to trash: ${filePath}`); + try { + await shell.trashItem(filePath); + return true; + } catch (error) { + logger.error('NativeService.moveToTrash() failed', { error, filePath }); + return false; + } + } + public formatFileUrlToAbsolutePath(urlWithFileProtocol: string): string { logger.info('getting url', { url: urlWithFileProtocol, function: 'formatFileUrlToAbsolutePath' }); let pathname = ''; diff --git a/src/services/native/interface.ts b/src/services/native/interface.ts index 94b2efd2..a89fe977 100644 --- a/src/services/native/interface.ts +++ b/src/services/native/interface.ts @@ -70,6 +70,12 @@ export interface INativeService { path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise; pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise; pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise; + /** + * Move a file or directory to the trash bin. + * @param filePath The absolute path of the file or directory to move to the trash. + * @returns A promise that resolves to true if the operation was successful, false otherwise. + */ + moveToTrash(filePath: string): Promise; quit(): void; showElectronMessageBox(options: Electron.MessageBoxOptions, windowName?: WindowNames): Promise; /** @@ -92,6 +98,7 @@ export const NativeServiceIPCDescriptor = { log: ProxyPropertyType.Function, mkdir: ProxyPropertyType.Function, movePath: ProxyPropertyType.Function, + moveToTrash: ProxyPropertyType.Function, open: ProxyPropertyType.Function, openInEditor: ProxyPropertyType.Function, openInGitGuiApp: ProxyPropertyType.Function,