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
This commit is contained in:
Daniel Büchele
2019-01-25 12:10:29 -08:00
committed by Facebook Github Bot
parent 978b14c3d3
commit e02d823e5f
2 changed files with 45 additions and 33 deletions

View File

@@ -15,20 +15,11 @@ const fs = require('fs');
const {exec} = require('child_process'); const {exec} = require('child_process');
const compilePlugins = require('./compilePlugins.js'); const compilePlugins = require('./compilePlugins.js');
const os = require('os'); 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 // 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; 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 (process.platform === 'darwin') {
// If we are running on macOS and the app is called Flipper, we add a comment // 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. // 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 let {config, configPath, flipperDir} = setup();
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));
}
const pluginPaths = config.pluginPaths const pluginPaths = config.pluginPaths
.concat( .concat(

43
static/setup.js Normal file
View File

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