From 14ae2fea2c06ce056188cabd69835eb80991168d Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Wed, 6 Feb 2019 03:15:41 -0800 Subject: [PATCH] Move configuration to module Summary: Moves the JSON config access to one module instead of ad-hoc de-serialising the object all over the place. This also adds a type for it so we hopefully won't typo things that easily. Reviewed By: danielbuechele Differential Revision: D13956612 fbshipit-source-id: 48098deafedf18cc86c8802a3c40fba9394b8262 --- src/chrome/ScreenCaptureButtons.js | 13 +++---- src/dispatcher/plugins.js | 7 ++-- src/utils/__tests__/processConfig.node.js | 42 +++++++++++++++++++++++ src/utils/processConfig.js | 40 +++++++++++++++++++++ 4 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 src/utils/__tests__/processConfig.node.js create mode 100644 src/utils/processConfig.js diff --git a/src/chrome/ScreenCaptureButtons.js b/src/chrome/ScreenCaptureButtons.js index a1068c2b9..68f5cd8d9 100644 --- a/src/chrome/ScreenCaptureButtons.js +++ b/src/chrome/ScreenCaptureButtons.js @@ -17,16 +17,13 @@ import {exec, spawn} from 'child_process'; import {remote} from 'electron'; import path from 'path'; import {reportPlatformFailures} from '../utils/metrics'; - -let CAPTURE_LOCATION = remote.app.getPath('desktop'); -try { - CAPTURE_LOCATION = expandTilde( - JSON.parse(window.process.env.CONFIG).screenCapturePath || CAPTURE_LOCATION, - ); -} catch (e) {} - +import config from '../utils/processConfig'; import type BaseDevice from '../devices/BaseDevice'; +const CAPTURE_LOCATION = expandTilde( + config().screenCapturePath || remote.app.getPath('desktop'), +); + type PullTransfer = any; type Props = {| diff --git a/src/dispatcher/plugins.js b/src/dispatcher/plugins.js index dc77d1dac..859eaddff 100644 --- a/src/dispatcher/plugins.js +++ b/src/dispatcher/plugins.js @@ -24,6 +24,7 @@ import GK from '../fb-stubs/GK'; import {FlipperBasePlugin} from '../plugin.js'; import {setupMenuBar} from '../MenuBar.js'; import path from 'path'; +import {default as config} from '../utils/processConfig.js'; export type PluginDefinition = { name: string, @@ -129,11 +130,7 @@ export const checkDisabled = (disabledPlugins: Array) => ( ): boolean => { let disabledList: Set = new Set(); try { - disabledList = new Set( - // $FlowFixMe process.env not defined in electron API spec - JSON.parse(remote?.process.env.CONFIG || process.env.CONFIG || '{}') - .disabledPlugins || [], - ); + disabledList = config().disabledPlugins; } catch (e) { console.error(e); } diff --git a/src/utils/__tests__/processConfig.node.js b/src/utils/__tests__/processConfig.node.js new file mode 100644 index 000000000..ad7c958df --- /dev/null +++ b/src/utils/__tests__/processConfig.node.js @@ -0,0 +1,42 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import {default as config, resetConfigForTesting} from '../processConfig.js'; + +afterEach(() => { + resetConfigForTesting(); +}); + +test('config is decoded from env', () => { + process.env.CONFIG = JSON.stringify({ + disabledPlugins: ['pluginA', 'pluginB', 'pluginC'], + pluginPaths: ['/a/path', 'b/path'], + lastWindowPosition: {x: 4, y: 8, width: 15, height: 16}, + updaterEnabled: false, + screenCapturePath: '/my/screenshot/path', + }); + + expect(config()).toEqual({ + disabledPlugins: new Set(['pluginA', 'pluginB', 'pluginC']), + pluginPaths: ['/a/path', 'b/path'], + lastWindowPosition: {x: 4, y: 8, width: 15, height: 16}, + updaterEnabled: false, + screenCapturePath: '/my/screenshot/path', + }); +}); + +test('config is decoded from env with defaults', () => { + process.env.CONFIG = '{}'; + + expect(config()).toEqual({ + disabledPlugins: new Set([]), + pluginPaths: [], + lastWindowPosition: undefined, + updaterEnabled: true, + screenCapturePath: undefined, + }); +}); diff --git a/src/utils/processConfig.js b/src/utils/processConfig.js new file mode 100644 index 000000000..ccf10e268 --- /dev/null +++ b/src/utils/processConfig.js @@ -0,0 +1,40 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import {remote} from 'electron'; + +export type ProcessConfig = {| + disabledPlugins: Set, + pluginPaths: Array, + lastWindowPosition: ?{x: number, y: number, width: number, height: number}, + screenCapturePath: ?string, + updaterEnabled: boolean, +|}; + +let configObj = null; +export default function config(): ProcessConfig { + if (configObj === null) { + const json = JSON.parse( + // $FlowFixMe: process.env not in type defs + remote?.process.env.CONFIG || process.env.CONFIG || '{}', + ); + configObj = { + disabledPlugins: new Set(json.disabledPlugins || []), + pluginPaths: json.pluginPaths || [], + lastWindowPosition: json.lastWindowPosition, + updaterEnabled: + typeof json.updaterEnabled === 'boolean' ? json.updaterEnabled : true, + screenCapturePath: json.screenCapturePath, + }; + } + + return configObj; +} + +export function resetConfigForTesting() { + configObj = null; +}