Add support to load settings from options (flag) and default to specific options

Summary: This change gives priority to a user option settingsString to set up the flipper-server configuration and load them from a json string. Also giving the user the chance to avoid flipper-server looking at the launcher config files in the computer

Reviewed By: mweststrate

Differential Revision: D34210110

fbshipit-source-id: 9e852b79da106b5140c59116fd7d0c0f3155e620
This commit is contained in:
Andres Orozco Gonzalez
2022-02-16 06:21:58 -08:00
committed by Facebook GitHub Bot
parent ff872400c7
commit b2dae4da81
5 changed files with 55 additions and 7 deletions

View File

@@ -48,6 +48,17 @@ const argv = yargs
type: 'string', type: 'string',
demandOption: true, demandOption: true,
}, },
settingsString: {
describe: `override the existing defaults settings of flipper (settings.json file) e.g "{"androidHome":"/usr/local/bin","enableAndroid":true}"`,
type: 'string',
default: '',
},
launcherSettings: {
describe:
'Open Flipper with the configuration stored in .config folder for the launcher',
type: 'boolean',
default: true,
},
// TODO: support filtering events // TODO: support filtering events
// TODO: support verbose mode // TODO: support verbose mode
// TODO: support post processing messages // TODO: support post processing messages
@@ -90,9 +101,9 @@ async function start(deviceQuery: string, appName: string, pluginId: string) {
execPath: process.execPath, execPath: process.execPath,
desktopPath: `/dev/null`, desktopPath: `/dev/null`,
}, },
launcherSettings: await loadLauncherSettings(), launcherSettings: await loadLauncherSettings(argv.launcherSettings),
processConfig: loadProcessConfig(process.env), processConfig: loadProcessConfig(process.env),
settings: await loadSettings(), settings: await loadSettings(argv.settingsString),
validWebSocketOrigins: [], validWebSocketOrigins: [],
}, },
logger, logger,

View File

@@ -65,7 +65,13 @@ function deserialize(content: string): LauncherSettings {
}; };
} }
export async function loadLauncherSettings(): Promise<LauncherSettings> { export async function loadLauncherSettings(
enableLauncherSettings: boolean = true,
): Promise<LauncherSettings> {
if (!enableLauncherSettings) {
return defaultLauncherSettings;
}
const fileName = getLauncherSettingsFile(); const fileName = getLauncherSettingsFile();
try { try {
const content = (await fs.readFile(fileName)).toString(); const content = (await fs.readFile(fileName)).toString();

View File

@@ -13,7 +13,16 @@ import xdg from 'xdg-basedir';
import {Settings, Tristate} from 'flipper-common'; import {Settings, Tristate} from 'flipper-common';
import {readFile, writeFile, pathExists, mkdirp} from 'fs-extra'; import {readFile, writeFile, pathExists, mkdirp} from 'fs-extra';
export async function loadSettings(): Promise<Settings> { export async function loadSettings(
settingsString: string = '',
): Promise<Settings> {
if (settingsString !== '') {
try {
return replaceDefaultSettings(JSON.parse(settingsString));
} catch (e) {
throw new Error("couldn't read the user settingsString");
}
}
if (!pathExists(getSettingsFile())) { if (!pathExists(getSettingsFile())) {
return getDefaultSettings(); return getDefaultSettings();
} }
@@ -74,3 +83,7 @@ function getDefaultAndroidSdkPath() {
function getWindowsSdkPath() { function getWindowsSdkPath() {
return `${os.homedir()}\\AppData\\Local\\android\\sdk`; return `${os.homedir()}\\AppData\\Local\\android\\sdk`;
} }
function replaceDefaultSettings(userSettings: Partial<Settings>): Settings {
return {...getDefaultSettings(), ...userSettings};
}

View File

@@ -44,6 +44,17 @@ const argv = yargs
type: 'boolean', type: 'boolean',
default: false, default: false,
}, },
settingsString: {
describe: `override the existing defaults settings of flipper (settings.json file) e.g "{"androidHome":"/usr/local/bin","enableAndroid":true}"`,
type: 'string',
default: '',
},
launcherSettings: {
describe:
'Open Flipper with the configuration stored in .config folder for the launcher',
type: 'boolean',
default: true,
},
}) })
.version('DEV') .version('DEV')
.help() .help()
@@ -71,7 +82,12 @@ async function start() {
staticDir, staticDir,
entry: 'index.web.dev.html', entry: 'index.web.dev.html',
}); });
const flipperServer = await startFlipperServer(rootDir, staticDir); const flipperServer = await startFlipperServer(
rootDir,
staticDir,
argv.settingsString,
argv.launcherSettings,
);
if (argv.failFast) { if (argv.failFast) {
flipperServer.on('server-state', ({state}) => { flipperServer.on('server-state', ({state}) => {
if (state === 'error') { if (state === 'error') {

View File

@@ -28,6 +28,8 @@ import fs from 'fs';
export async function startFlipperServer( export async function startFlipperServer(
rootDir: string, rootDir: string,
staticPath: string, staticPath: string,
settingsString: string,
enableLauncherSettings: boolean,
): Promise<FlipperServerImpl> { ): Promise<FlipperServerImpl> {
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
// By default Node.JS has its internal certificate storage and doesn't use // By default Node.JS has its internal certificate storage and doesn't use
@@ -85,9 +87,9 @@ export async function startFlipperServer(
tempPath: os.tmpdir(), tempPath: os.tmpdir(),
desktopPath: desktopPath, desktopPath: desktopPath,
}, },
launcherSettings: await loadLauncherSettings(), launcherSettings: await loadLauncherSettings(enableLauncherSettings),
processConfig: loadProcessConfig(env), processConfig: loadProcessConfig(env),
settings: await loadSettings(), settings: await loadSettings(settingsString),
validWebSocketOrigins: ['localhost:', 'http://localhost:'], validWebSocketOrigins: ['localhost:', 'http://localhost:'],
}, },
logger, logger,