From aea77cc4da4147b5ea4a027b3d624b2fcdf9fb33 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Fri, 20 Oct 2023 05:13:20 -0700 Subject: [PATCH] Hide PWA app from Spotlight Reviewed By: LukeDefeo Differential Revision: D50411182 fbshipit-source-id: 46c089c69aefb58f85a861c7898ee355f094e03c --- desktop/flipper-common/src/server-types.tsx | 1 + .../src/FlipperServerImpl.tsx | 4 + desktop/flipper-server-core/src/index.tsx | 1 + .../src/utils/findInstallation.tsx | 77 +++++++++++++++++++ .../flipper-server/src/findInstallation.tsx | 29 ------- desktop/flipper-server/src/index.tsx | 3 +- .../src/chrome/PWAppInstallationWizard.tsx | 1 + 7 files changed, 85 insertions(+), 31 deletions(-) create mode 100644 desktop/flipper-server-core/src/utils/findInstallation.tsx delete mode 100644 desktop/flipper-server/src/findInstallation.tsx diff --git a/desktop/flipper-common/src/server-types.tsx b/desktop/flipper-common/src/server-types.tsx index 1591ec715..cb285b123 100644 --- a/desktop/flipper-common/src/server-types.tsx +++ b/desktop/flipper-common/src/server-types.tsx @@ -380,6 +380,7 @@ export type FlipperServerCommands = { shutdown: () => Promise; 'is-logged-in': () => Promise; 'environment-info': () => Promise; + 'move-pwa': () => Promise; }; export type GraphResponse = { diff --git a/desktop/flipper-server-core/src/FlipperServerImpl.tsx b/desktop/flipper-server-core/src/FlipperServerImpl.tsx index 5f0e55f2a..c2407ff2e 100644 --- a/desktop/flipper-server-core/src/FlipperServerImpl.tsx +++ b/desktop/flipper-server-core/src/FlipperServerImpl.tsx @@ -58,6 +58,7 @@ import {flipperDataFolder, flipperSettingsFolder} from './utils/paths'; import {DebuggableDevice} from './devices/DebuggableDevice'; import {jfUpload} from './fb-stubs/jf'; import path from 'path'; +import {movePWA} from './utils/findInstallation'; const {access, copyFile, mkdir, unlink, stat, readlink, readFile, writeFile} = promises; @@ -597,6 +598,9 @@ export class FlipperServerImpl implements FlipperServer { 'environment-info': async () => { return this.config.environmentInfo; }, + 'move-pwa': async () => { + await movePWA(); + }, }; registerDevice(device: ServerDevice) { diff --git a/desktop/flipper-server-core/src/index.tsx b/desktop/flipper-server-core/src/index.tsx index 7e8099f8b..951b26a67 100644 --- a/desktop/flipper-server-core/src/index.tsx +++ b/desktop/flipper-server-core/src/index.tsx @@ -13,6 +13,7 @@ export * from './tracker'; export {loadLauncherSettings} from './utils/launcherSettings'; export {loadProcessConfig} from './utils/processConfig'; export {getEnvironmentInfo} from './utils/environmentInfo'; +export {findInstallation} from './utils/findInstallation'; export {getGatekeepers} from './gk'; export {setupPrefetcher} from './fb-stubs/Prefetcher'; export * from './server/attachSocketServer'; diff --git a/desktop/flipper-server-core/src/utils/findInstallation.tsx b/desktop/flipper-server-core/src/utils/findInstallation.tsx new file mode 100644 index 000000000..876ad1914 --- /dev/null +++ b/desktop/flipper-server-core/src/utils/findInstallation.tsx @@ -0,0 +1,77 @@ +/** + * 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 path from 'path'; +import fs from 'fs-extra'; +import os from 'os'; +import GK from '../fb-stubs/GK'; + +const pwaRoot = path.join( + os.homedir(), + 'Applications', + 'Chrome Apps.localized', +); +const appFolder = path.resolve(pwaRoot, '.flipper'); +const defaultAppPath = path.join(pwaRoot, 'Flipper.app'); +const movedAppPath = path.join(appFolder, 'Flipper.app'); + +export async function movePWA(): Promise { + if (os.platform() !== 'darwin') { + return; + } + + if (!GK.get('flipper_move_pwa')) { + return; + } + + // Move PWA into its own folder + // Later we will make the folder hidden so Spotlight stops indexing it + // Sadly, Spotlight can stop indexing only hidden folder, not hidden files + // Therefore, we have to create this parent folder in the first place. + if (!(await fs.pathExists(appFolder))) { + await fs.mkdir(appFolder); + } + await fs.move(defaultAppPath, movedAppPath); +} + +export async function findInstallation(): Promise { + if (os.platform() !== 'darwin') { + return; + } + + try { + if (GK.get('flipper_move_pwa')) { + if (await fs.pathExists(defaultAppPath)) { + await movePWA(); + } + } + } catch (e) { + console.error('Failed to move PWA', e); + } finally { + if (GK.get('flipper_move_pwa')) { + const movedAppPlistPath = path.join( + movedAppPath, + 'Contents', + 'Info.plist', + ); + if (await fs.pathExists(movedAppPlistPath)) { + return movedAppPath; + } + // We should get here only if moving PWA failed + } + const dafaultAppPlistPath = path.join( + defaultAppPath, + 'Contents', + 'Info.plist', + ); + if (await fs.pathExists(dafaultAppPlistPath)) { + return defaultAppPath; + } + } +} diff --git a/desktop/flipper-server/src/findInstallation.tsx b/desktop/flipper-server/src/findInstallation.tsx deleted file mode 100644 index 63b4469be..000000000 --- a/desktop/flipper-server/src/findInstallation.tsx +++ /dev/null @@ -1,29 +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 path from 'path'; -import fs from 'fs-extra'; -import os from 'os'; - -export async function findInstallation(): Promise { - if (os.platform() !== 'darwin') { - return; - } - - const appPath = path.join( - os.homedir(), - 'Applications', - 'Chrome Apps.localized', - 'Flipper.app', - ); - const appPlistPath = path.join(appPath, 'Contents', 'Info.plist'); - if (await fs.pathExists(appPlistPath)) { - return appPath; - } -} diff --git a/desktop/flipper-server/src/index.tsx b/desktop/flipper-server/src/index.tsx index 36b54ef44..77ad9e066 100644 --- a/desktop/flipper-server/src/index.tsx +++ b/desktop/flipper-server/src/index.tsx @@ -31,8 +31,7 @@ import { } from 'flipper-server-core'; import {addLogTailer, isTest, LoggerFormat} from 'flipper-common'; import exitHook from 'exit-hook'; -import {getAuthToken} from 'flipper-server-core'; -import {findInstallation} from './findInstallation'; +import {getAuthToken, findInstallation} from 'flipper-server-core'; const argv = yargs .usage('yarn flipper-server [args]') diff --git a/desktop/flipper-ui-core/src/chrome/PWAppInstallationWizard.tsx b/desktop/flipper-ui-core/src/chrome/PWAppInstallationWizard.tsx index 34cb02002..84ce4f5d2 100644 --- a/desktop/flipper-ui-core/src/chrome/PWAppInstallationWizard.tsx +++ b/desktop/flipper-ui-core/src/chrome/PWAppInstallationWizard.tsx @@ -132,6 +132,7 @@ async function install(event: any) { if (choiceResult.outcome === 'accepted') { tracker.track('pwa-install-outcome', {installed: true}); console.log('PWA installation, user accepted the prompt.'); + return getRenderHostInstance().flipperServer.exec('move-pwa'); } else { tracker.track('pwa-install-outcome', {installed: false}); console.log('PWA installation, user dismissed the prompt.');