Use flipper lib implementation from flipper-frontend-core in flipper-ui-core
Summary: See D37139129 Reviewed By: lblasa Differential Revision: D37236771 fbshipit-source-id: 7f93140f72e89cb4a7907f07cceec1fed50cb66b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
17ab7a86ef
commit
851270589f
@@ -1,72 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {assertNever, DownloadFileUpdate} from 'flipper-common';
|
||||
import {FlipperLib, DownloadFileResponse} from 'flipper-plugin';
|
||||
import {RenderHost} from 'flipper-frontend-core';
|
||||
|
||||
export const downloadFileFactory =
|
||||
(renderHost: RenderHost): FlipperLib['remoteServerContext']['downloadFile'] =>
|
||||
async (url, dest, {onProgressUpdate, ...options} = {}) => {
|
||||
const downloadDescriptor = (await renderHost.flipperServer.exec(
|
||||
'download-file-start',
|
||||
url,
|
||||
dest,
|
||||
options,
|
||||
// Casting to DownloadFileResponse to add `completed` field to `downloadDescriptor`.
|
||||
)) as DownloadFileResponse;
|
||||
|
||||
let onProgressUpdateWrapped: (progressUpdate: DownloadFileUpdate) => void;
|
||||
const completed = new Promise<number>((resolve, reject) => {
|
||||
onProgressUpdateWrapped = (progressUpdate: DownloadFileUpdate) => {
|
||||
if (progressUpdate.id === downloadDescriptor.id) {
|
||||
const {status} = progressUpdate;
|
||||
switch (status) {
|
||||
case 'downloading': {
|
||||
onProgressUpdate?.(progressUpdate);
|
||||
break;
|
||||
}
|
||||
case 'success': {
|
||||
resolve(progressUpdate.downloaded);
|
||||
break;
|
||||
}
|
||||
case 'error': {
|
||||
reject(
|
||||
new Error(
|
||||
`File download failed. Last message: ${JSON.stringify(
|
||||
progressUpdate,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
assertNever(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
renderHost.flipperServer.on(
|
||||
'download-file-update',
|
||||
onProgressUpdateWrapped,
|
||||
);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line promise/catch-or-return
|
||||
completed.finally(() => {
|
||||
renderHost.flipperServer.off(
|
||||
'download-file-update',
|
||||
onProgressUpdateWrapped,
|
||||
);
|
||||
});
|
||||
|
||||
downloadDescriptor.completed = completed;
|
||||
|
||||
return downloadDescriptor;
|
||||
};
|
||||
@@ -7,29 +7,16 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {
|
||||
_setFlipperLibImplementation,
|
||||
RemoteServerContext,
|
||||
} from 'flipper-plugin';
|
||||
import {
|
||||
BufferEncoding,
|
||||
ExecOptions,
|
||||
fsConstants,
|
||||
Logger,
|
||||
MkdirOptions,
|
||||
RmOptions,
|
||||
} from 'flipper-common';
|
||||
import {_setFlipperLibImplementation} from 'flipper-plugin';
|
||||
import {Logger} from 'flipper-common';
|
||||
import type {Store} from '../../reducers';
|
||||
import createPaste from '../../fb-stubs/createPaste';
|
||||
import type {BaseDevice} from 'flipper-frontend-core';
|
||||
import constants from '../../fb-stubs/constants';
|
||||
import {BaseDevice, baseFlipperLibImplementation} from 'flipper-frontend-core';
|
||||
import {DetailSidebarImpl} from '../../sandy-chrome/DetailSidebarImpl';
|
||||
import {addNotification} from '../../reducers/notifications';
|
||||
import {deconstructPluginKey} from 'flipper-common';
|
||||
import {DetailSidebarImpl} from '../../sandy-chrome/DetailSidebarImpl';
|
||||
import {RenderHost} from 'flipper-frontend-core';
|
||||
import {setMenuEntries} from '../../reducers/connections';
|
||||
import {downloadFileFactory} from './downloadFile';
|
||||
import {Base64} from 'js-base64';
|
||||
|
||||
export function initializeFlipperLibImplementation(
|
||||
renderHost: RenderHost,
|
||||
@@ -37,13 +24,11 @@ export function initializeFlipperLibImplementation(
|
||||
logger: Logger,
|
||||
) {
|
||||
_setFlipperLibImplementation({
|
||||
isFB: !constants.IS_PUBLIC_BUILD,
|
||||
logger,
|
||||
...baseFlipperLibImplementation(renderHost, logger),
|
||||
enableMenuEntries(entries) {
|
||||
store.dispatch(setMenuEntries(entries));
|
||||
},
|
||||
createPaste,
|
||||
GK: renderHost.GK,
|
||||
selectPlugin(device, client, pluginId, deeplink) {
|
||||
store.dispatch({
|
||||
type: 'SELECT_PLUGIN',
|
||||
@@ -56,8 +41,6 @@ export function initializeFlipperLibImplementation(
|
||||
},
|
||||
});
|
||||
},
|
||||
writeTextToClipboard: renderHost.writeTextToClipboard,
|
||||
openLink: renderHost.openLink,
|
||||
showNotification(pluginId, notification) {
|
||||
const parts = deconstructPluginKey(pluginId);
|
||||
store.dispatch(
|
||||
@@ -69,86 +52,5 @@ export function initializeFlipperLibImplementation(
|
||||
);
|
||||
},
|
||||
DetailsSidebarImplementation: DetailSidebarImpl,
|
||||
importFile: renderHost.importFile,
|
||||
exportFile: renderHost.exportFile,
|
||||
paths: {
|
||||
appPath: renderHost.serverConfig.paths.appPath,
|
||||
homePath: renderHost.serverConfig.paths.homePath,
|
||||
staticPath: renderHost.serverConfig.paths.staticPath,
|
||||
tempPath: renderHost.serverConfig.paths.tempPath,
|
||||
},
|
||||
environmentInfo: {
|
||||
os: renderHost.serverConfig.environmentInfo.os,
|
||||
},
|
||||
intern: {
|
||||
graphGet: (...args) =>
|
||||
renderHost.flipperServer.exec('intern-graph-get', ...args),
|
||||
graphPost: (...args) =>
|
||||
renderHost.flipperServer.exec('intern-graph-post', ...args),
|
||||
},
|
||||
remoteServerContext: {
|
||||
childProcess: {
|
||||
exec: async (
|
||||
command: string,
|
||||
options?: ExecOptions & {encoding?: BufferEncoding},
|
||||
) => renderHost.flipperServer.exec('node-api-exec', command, options),
|
||||
},
|
||||
fs: {
|
||||
access: async (path: string, mode?: number) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-access', path, mode),
|
||||
pathExists: async (path: string, mode?: number) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-pathExists', path, mode),
|
||||
unlink: async (path: string) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-unlink', path),
|
||||
mkdir: (async (
|
||||
path: string,
|
||||
options?: {recursive?: boolean} & MkdirOptions,
|
||||
) =>
|
||||
renderHost.flipperServer.exec(
|
||||
'node-api-fs-mkdir',
|
||||
path,
|
||||
options,
|
||||
)) as RemoteServerContext['fs']['mkdir'],
|
||||
rm: async (path: string, options?: RmOptions) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-rm', path, options),
|
||||
copyFile: async (src: string, dest: string, flags?: number) =>
|
||||
renderHost.flipperServer.exec(
|
||||
'node-api-fs-copyFile',
|
||||
src,
|
||||
dest,
|
||||
flags,
|
||||
),
|
||||
constants: fsConstants,
|
||||
stat: async (path: string) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-stat', path),
|
||||
readlink: async (path: string) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-readlink', path),
|
||||
readFile: (path, options) =>
|
||||
renderHost.flipperServer.exec('node-api-fs-readfile', path, options),
|
||||
readFileBinary: async (path) =>
|
||||
Base64.toUint8Array(
|
||||
await renderHost.flipperServer.exec(
|
||||
'node-api-fs-readfile-binary',
|
||||
path,
|
||||
),
|
||||
),
|
||||
writeFile: (path, contents, options) =>
|
||||
renderHost.flipperServer.exec(
|
||||
'node-api-fs-writefile',
|
||||
path,
|
||||
contents,
|
||||
options,
|
||||
),
|
||||
writeFileBinary: async (path, contents) => {
|
||||
const base64contents = Base64.fromUint8Array(contents);
|
||||
return await renderHost.flipperServer.exec(
|
||||
'node-api-fs-writefile-binary',
|
||||
path,
|
||||
base64contents,
|
||||
);
|
||||
},
|
||||
},
|
||||
downloadFile: downloadFileFactory(renderHost),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user