From 4e17fb9c48c7a7245d93141cd495db691a1a62d4 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Wed, 11 Aug 2021 11:02:10 -0700 Subject: [PATCH] Extract temp path util Summary: `electron.remote` access is slow so we can cache this like we do in other places. Also means one fewer `electron` import which is gonna add up for Flipper Decap. Reviewed By: mweststrate Differential Revision: D30247431 fbshipit-source-id: 90f0e8df99af8bed40fbebcfd445abaca2965b7c --- desktop/app/src/devices/IOSDevice.tsx | 4 ++-- desktop/app/src/utils/pathUtils.tsx | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/desktop/app/src/devices/IOSDevice.tsx b/desktop/app/src/devices/IOSDevice.tsx index f3fcc2bf4..aa28cd044 100644 --- a/desktop/app/src/devices/IOSDevice.tsx +++ b/desktop/app/src/devices/IOSDevice.tsx @@ -12,7 +12,6 @@ import child_process, {ChildProcess} from 'child_process'; import BaseDevice from './BaseDevice'; import JSONStream from 'JSONStream'; import {Transform} from 'stream'; -import electron from 'electron'; import fs from 'fs'; import {v1 as uuid} from 'uuid'; import path from 'path'; @@ -21,6 +20,7 @@ import {exec} from 'child_process'; import {default as promiseTimeout} from '../utils/promiseTimeout'; import {IOSBridge} from '../utils/IOSBridge'; import split2 from 'split2'; +import {getAppTempPath} from '../utils/pathUtils'; type IOSLogLevel = 'Default' | 'Info' | 'Debug' | 'Error' | 'Fault'; @@ -69,7 +69,7 @@ export default class IOSDevice extends BaseDevice { return Buffer.from([]); } const tmpImageName = uuid() + '.png'; - const tmpDirectory = (electron.app || electron.remote.app).getPath('temp'); + const tmpDirectory = getAppTempPath(); const tmpFilePath = path.join(tmpDirectory, tmpImageName); const command = this.deviceType === 'emulator' diff --git a/desktop/app/src/utils/pathUtils.tsx b/desktop/app/src/utils/pathUtils.tsx index 072529eae..9c695774b 100644 --- a/desktop/app/src/utils/pathUtils.tsx +++ b/desktop/app/src/utils/pathUtils.tsx @@ -9,7 +9,9 @@ import path from 'path'; import fs from 'fs'; -import {remote} from 'electron'; +// In utils this is fine when used with caching. +// eslint-disable-next-line flipper/no-electron-remote-imports +import {default as electron, remote} from 'electron'; import config from '../fb-stubs/config'; let _staticPath = ''; @@ -55,6 +57,17 @@ export function getAppPath() { return _appPath; } +let _tempPath: string | undefined = undefined; +export function getAppTempPath() { + if (!_tempPath) { + // We cache this. + // eslint-disable-next-line no-restricted-properties + _tempPath = (electron.app || electron.remote.app).getPath('temp'); + } + + return _tempPath; +} + export function getChangelogPath() { const changelogPath = getStaticPath(config.isFBBuild ? 'facebook' : '.'); if (fs.existsSync(changelogPath)) {