From 3314c77ce9835b8fd536559cbb5dea372e49b9a9 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Wed, 21 Sep 2022 09:26:19 -0700 Subject: [PATCH] Add exportFileBinary to FlipperLib Reviewed By: antonk52 Differential Revision: D39692937 fbshipit-source-id: 7b3c78d004a9734cd8ae660d5782be1f02c00009 --- .../app/src/electron/initializeElectron.tsx | 12 ++++++++++++ .../flipper-frontend-core/src/RenderHost.tsx | 1 + .../src/flipperLibImplementation/index.tsx | 1 + .../src/plugin/FlipperLib.tsx | 19 +++++++++++++++++++ .../src/test-utils/test-utils.tsx | 1 + .../src/initializeRenderHost.tsx | 3 +++ .../src/initializeRenderHost.tsx | 3 +++ desktop/scripts/jest-setup-after.tsx | 3 +++ 8 files changed, 43 insertions(+) diff --git a/desktop/app/src/electron/initializeElectron.tsx b/desktop/app/src/electron/initializeElectron.tsx index f6477f11e..f7798a152 100644 --- a/desktop/app/src/electron/initializeElectron.tsx +++ b/desktop/app/src/electron/initializeElectron.tsx @@ -151,6 +151,18 @@ export async function initializeElectron( await fs.promises.writeFile(filePath, data, {encoding}); return filePath; }, + async exportFileBinary(data, {defaultPath} = {}) { + const {filePath} = await electronIpcClient.send('showSaveDialog', { + defaultPath, + }); + + if (!filePath) { + return; + } + + await fs.promises.writeFile(filePath, data, {encoding: 'binary'}); + return filePath; + }, openLink(url: string) { shell.openExternal(url); }, diff --git a/desktop/flipper-frontend-core/src/RenderHost.tsx b/desktop/flipper-frontend-core/src/RenderHost.tsx index 0081310d8..0dbc1313f 100644 --- a/desktop/flipper-frontend-core/src/RenderHost.tsx +++ b/desktop/flipper-frontend-core/src/RenderHost.tsx @@ -126,6 +126,7 @@ export interface RenderHost { showSelectDirectoryDialog?(defaultPath?: string): Promise; importFile: FlipperLib['importFile']; exportFile: FlipperLib['exportFile']; + exportFileBinary: FlipperLib['exportFileBinary']; hasFocus(): boolean; onIpcEvent( event: Event, diff --git a/desktop/flipper-frontend-core/src/flipperLibImplementation/index.tsx b/desktop/flipper-frontend-core/src/flipperLibImplementation/index.tsx index 53d48ca5f..57b4e8ca6 100644 --- a/desktop/flipper-frontend-core/src/flipperLibImplementation/index.tsx +++ b/desktop/flipper-frontend-core/src/flipperLibImplementation/index.tsx @@ -36,6 +36,7 @@ export function baseFlipperLibImplementation( openLink: renderHost.openLink, importFile: renderHost.importFile, exportFile: renderHost.exportFile, + exportFileBinary: renderHost.exportFileBinary, paths: { appPath: renderHost.serverConfig.paths.appPath, homePath: renderHost.serverConfig.paths.homePath, diff --git a/desktop/flipper-plugin-core/src/plugin/FlipperLib.tsx b/desktop/flipper-plugin-core/src/plugin/FlipperLib.tsx index 52071e6a7..68835018e 100644 --- a/desktop/flipper-plugin-core/src/plugin/FlipperLib.tsx +++ b/desktop/flipper-plugin-core/src/plugin/FlipperLib.tsx @@ -172,6 +172,25 @@ export interface FlipperLib { encoding?: FileEncoding; }, ): Promise; + /** + * @returns + * An exported file path (if available) or a file name. + * If user cancelled a file selection - undefined. + */ + exportFileBinary( + /** + * New file data + */ + data: Uint8Array, + options?: { + /** + * A file path suggestion for a new file. + * A dialog to save file will use it as a starting point. + * Either a complete path to the newly created file, a path to a directory containing the file, or the file name. + */ + defaultPath?: string; + }, + ): Promise; paths: { appPath: string; homePath: string; diff --git a/desktop/flipper-plugin-core/src/test-utils/test-utils.tsx b/desktop/flipper-plugin-core/src/test-utils/test-utils.tsx index e156a25a5..c3a49419a 100644 --- a/desktop/flipper-plugin-core/src/test-utils/test-utils.tsx +++ b/desktop/flipper-plugin-core/src/test-utils/test-utils.tsx @@ -60,6 +60,7 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib { openLink: createStubFunction(), showNotification: createStubFunction(), exportFile: createStubFunction(), + exportFileBinary: createStubFunction(), importFile: createStubFunction(), paths: { appPath: process.cwd(), diff --git a/desktop/flipper-server-companion/src/initializeRenderHost.tsx b/desktop/flipper-server-companion/src/initializeRenderHost.tsx index bca3c4ab3..53d50dfe7 100644 --- a/desktop/flipper-server-companion/src/initializeRenderHost.tsx +++ b/desktop/flipper-server-companion/src/initializeRenderHost.tsx @@ -28,6 +28,9 @@ export function initializeRenderHost( async exportFile() { throw new Error('Not implemented'); }, + async exportFileBinary() { + throw new Error('Not implemented'); + }, openLink(url: string) { throw new Error('Not implemented'); }, diff --git a/desktop/flipper-ui-browser/src/initializeRenderHost.tsx b/desktop/flipper-ui-browser/src/initializeRenderHost.tsx index fa65e8eff..d4c458b0e 100644 --- a/desktop/flipper-ui-browser/src/initializeRenderHost.tsx +++ b/desktop/flipper-ui-browser/src/initializeRenderHost.tsx @@ -48,6 +48,9 @@ export function initializeRenderHost( async exportFile() { throw new Error('Not implemented'); }, + async exportFileBinary() { + throw new Error('Not implemented'); + }, openLink(url: string) { window.open(url, '_blank'); }, diff --git a/desktop/scripts/jest-setup-after.tsx b/desktop/scripts/jest-setup-after.tsx index 931a74da2..a19544147 100644 --- a/desktop/scripts/jest-setup-after.tsx +++ b/desktop/scripts/jest-setup-after.tsx @@ -170,6 +170,9 @@ function createStubRenderHost(): RenderHost { async exportFile() { return undefined; }, + async exportFileBinary() { + return undefined; + }, hasFocus() { return true; },