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}; +};