From 3f771c05dde9fda6974c4d42bf00793dea75c3ea Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Mon, 20 Dec 2021 11:37:25 -0800 Subject: [PATCH] Remove fs usage from Navigator plugin Reviewed By: mweststrate Differential Revision: D33019873 fbshipit-source-id: 6d48971dc04161b64fdd32ac4e658b56aa98b7ec --- .../flipper-plugin/src/plugin/FlipperLib.tsx | 3 ++- .../src/test-utils/test-utils.tsx | 1 + .../utils/flipperLibImplementation/index.tsx | 1 + .../navigation/util/appMatchPatterns.tsx | 26 +++++++------------ 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/desktop/flipper-plugin/src/plugin/FlipperLib.tsx b/desktop/flipper-plugin/src/plugin/FlipperLib.tsx index e1fee4975..eea1f363b 100644 --- a/desktop/flipper-plugin/src/plugin/FlipperLib.tsx +++ b/desktop/flipper-plugin/src/plugin/FlipperLib.tsx @@ -167,8 +167,9 @@ export interface FlipperLib { }, ): Promise; paths: { - homePath: string; appPath: string; + homePath: string; + staticPath: string; tempPath: string; }; environmentInfo: { diff --git a/desktop/flipper-plugin/src/test-utils/test-utils.tsx b/desktop/flipper-plugin/src/test-utils/test-utils.tsx index 57f7fa5cb..0e66855ba 100644 --- a/desktop/flipper-plugin/src/test-utils/test-utils.tsx +++ b/desktop/flipper-plugin/src/test-utils/test-utils.tsx @@ -407,6 +407,7 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib { paths: { appPath: process.cwd(), homePath: `/dev/null`, + staticPath: process.cwd(), tempPath: `/dev/null`, }, environmentInfo: { diff --git a/desktop/flipper-ui-core/src/utils/flipperLibImplementation/index.tsx b/desktop/flipper-ui-core/src/utils/flipperLibImplementation/index.tsx index 7463071b5..b2fd12fa4 100644 --- a/desktop/flipper-ui-core/src/utils/flipperLibImplementation/index.tsx +++ b/desktop/flipper-ui-core/src/utils/flipperLibImplementation/index.tsx @@ -74,6 +74,7 @@ export function initializeFlipperLibImplementation( paths: { appPath: renderHost.serverConfig.paths.appPath, homePath: renderHost.serverConfig.paths.homePath, + staticPath: renderHost.serverConfig.paths.staticPath, tempPath: renderHost.serverConfig.paths.tempPath, }, environmentInfo: { diff --git a/desktop/plugins/public/navigation/util/appMatchPatterns.tsx b/desktop/plugins/public/navigation/util/appMatchPatterns.tsx index 15154b4d8..7f52096e7 100644 --- a/desktop/plugins/public/navigation/util/appMatchPatterns.tsx +++ b/desktop/plugins/public/navigation/util/appMatchPatterns.tsx @@ -7,18 +7,10 @@ * @format */ -import fs from 'fs'; import {path} from 'flipper-plugin'; import {AppMatchPattern} from '../types'; import {Device, getFlipperLib} from 'flipper-plugin'; -let patternsPath: string | undefined; - -function getPatternsBasePath() { - return (patternsPath = - patternsPath ?? path.join(getFlipperLib().paths.appPath, 'facebook')); -} - const extractAppNameFromSelectedApp = (selectedApp: string | null) => { if (selectedApp == null) { return null; @@ -31,7 +23,7 @@ export const getAppMatchPatterns = ( selectedApp: string | null, device: Device, ) => { - return new Promise>((resolve, reject) => { + return new Promise>(async (resolve, reject) => { const appName = extractAppNameFromSelectedApp(selectedApp); if (appName === 'Facebook') { let filename: string; @@ -42,14 +34,14 @@ export const getAppMatchPatterns = ( } else { return; } - const patternsFilePath = path.join(getPatternsBasePath(), filename); - fs.readFile(patternsFilePath, (err, data) => { - if (err) { - reject(err); - } else { - resolve(JSON.parse(data.toString())); - } - }); + const patternsFilePath = path.join( + getFlipperLib().paths.staticPath, + 'facebook', + filename, + ); + const patternsFileContentString = + await getFlipperLib().remoteServerContext.fs.readFile(patternsFilePath); + return JSON.parse(patternsFileContentString); } else if (appName != null) { console.log('No rule for app ' + appName); resolve([]);