From e02d823e5fde100ea445e9098cc56eb96eccfe79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 25 Jan 2019 12:10:29 -0800 Subject: [PATCH] split setup Summary: `static/index.js` is called by Electron's main process, it does the setup required to run Flipper. However, some of this setup is electron specific, other is not. This moves all non-Electron specific code into `setup.js` which later can be called from node, too. All Electron specific stuff is still in `static/index.js`. On a side note, this remove the renaming from `.sonar` to `.flipper` config folder, as I think most users should have been migrated by now, users that are not yet migrated will lose their settings, which isn't too much of an issue as our settings are very minimal anyways. Reviewed By: passy Differential Revision: D13638253 fbshipit-source-id: 22ab2a65ffec87b40bd86899630d582d9dc5d05c --- static/index.js | 35 ++--------------------------------- static/setup.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 static/setup.js diff --git a/static/index.js b/static/index.js index fbf699431..4d4188ca5 100644 --- a/static/index.js +++ b/static/index.js @@ -15,20 +15,11 @@ const fs = require('fs'); const {exec} = require('child_process'); const compilePlugins = require('./compilePlugins.js'); const os = require('os'); +const setup = require('./setup'); // disable electron security warnings: https://github.com/electron/electron/blob/master/docs/tutorial/security.md#security-native-capabilities-and-your-responsibility process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true; -if (!process.env.ANDROID_HOME) { - process.env.ANDROID_HOME = '/opt/android_sdk'; -} - -// emulator/emulator is more reliable than tools/emulator, so prefer it if -// it exists -process.env.PATH = `${process.env.ANDROID_HOME}/emulator:${ - process.env.ANDROID_HOME -}/tools:${process.env.PATH}`; - if (process.platform === 'darwin') { // If we are running on macOS and the app is called Flipper, we add a comment // with the old name, to make it findable via Spotlight using its old name. @@ -44,29 +35,7 @@ if (process.platform === 'darwin') { } } -// ensure .flipper folder and config exist -const sonarDir = path.join(os.homedir(), '.sonar'); -const flipperDir = path.join(os.homedir(), '.flipper'); -if (fs.existsSync(flipperDir)) { - // nothing to do -} else if (fs.existsSync(sonarDir)) { - // move .sonar to .flipper - fs.renameSync(sonarDir, flipperDir); -} else { - fs.mkdirSync(flipperDir); -} - -const configPath = path.join(flipperDir, 'config.json'); -let config = {pluginPaths: [], disabledPlugins: [], lastWindowPosition: {}}; - -try { - config = { - ...config, - ...JSON.parse(fs.readFileSync(configPath)), - }; -} catch (e) { - fs.writeFileSync(configPath, JSON.stringify(config)); -} +let {config, configPath, flipperDir} = setup(); const pluginPaths = config.pluginPaths .concat( diff --git a/static/setup.js b/static/setup.js new file mode 100644 index 000000000..27fdf322d --- /dev/null +++ b/static/setup.js @@ -0,0 +1,43 @@ +/** + * 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 + */ + +const path = require('path'); +const os = require('os'); +const fs = require('fs'); + +module.exports = function() { + if (!process.env.ANDROID_HOME) { + process.env.ANDROID_HOME = '/opt/android_sdk'; + } + + // emulator/emulator is more reliable than tools/emulator, so prefer it if + // it exists + process.env.PATH = `${process.env.ANDROID_HOME}/emulator:${ + process.env.ANDROID_HOME + }/tools:${process.env.PATH}`; + + // ensure .flipper folder and config exist + const flipperDir = path.join(os.homedir(), '.flipper'); + if (!fs.existsSync(flipperDir)) { + fs.mkdirSync(flipperDir); + } + + const configPath = path.join(flipperDir, 'config.json'); + let config = {pluginPaths: [], disabledPlugins: [], lastWindowPosition: {}}; + + try { + config = { + ...config, + ...JSON.parse(fs.readFileSync(configPath)), + }; + } catch (e) { + // file not readable or not parsable, overwrite it with the new config + fs.writeFileSync(configPath, JSON.stringify(config)); + } + + return {config, configPath, flipperDir}; +};