Refactor Open
Summary: Extract our launch UI logic into flipper-server-core. Reviewed By: passy Differential Revision: D51115241 fbshipit-source-id: 185e381eab6b480d86a5e1201f45c070104d0cea
This commit is contained in:
committed by
Facebook GitHub Bot
parent
9164e04e29
commit
137e75ad46
@@ -13,13 +13,13 @@ export * from './tracker';
|
||||
export {loadLauncherSettings} from './utils/launcherSettings';
|
||||
export {loadProcessConfig} from './utils/processConfig';
|
||||
export {getEnvironmentInfo} from './utils/environmentInfo';
|
||||
export {findInstallation} from './utils/findInstallation';
|
||||
export {getGatekeepers} from './gk';
|
||||
export {setupPrefetcher} from './fb-stubs/Prefetcher';
|
||||
export * from './server/attachSocketServer';
|
||||
export * from './server/startFlipperServer';
|
||||
export * from './server/startServer';
|
||||
export * from './server/utilities';
|
||||
export * from './utils/openUI';
|
||||
export {isFBBuild} from './fb-stubs/constants';
|
||||
export {initializeLogger} from './fb-stubs/Logger';
|
||||
|
||||
|
||||
59
desktop/flipper-server-core/src/utils/openUI.tsx
Normal file
59
desktop/flipper-server-core/src/utils/openUI.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import open from 'open';
|
||||
import {getAuthToken} from '../app-connectivity/certificate-exchange/certificate-utils';
|
||||
import {findInstallation} from './findInstallation';
|
||||
import {tracker} from '../tracker';
|
||||
|
||||
export enum UIPreference {
|
||||
Browser,
|
||||
PWA,
|
||||
}
|
||||
|
||||
export async function openUI(preference: UIPreference, port: number) {
|
||||
console.info('[flipper-server] Launch UI');
|
||||
|
||||
const token = await getAuthToken();
|
||||
console.info(
|
||||
`[flipper-server] Get authentication token: ${token?.length != 0}`,
|
||||
);
|
||||
|
||||
const openInBrowser = async () => {
|
||||
console.info('[flipper-server] Open in browser');
|
||||
const url = new URL(`http://localhost:${port}`);
|
||||
|
||||
console.info(`[flipper-server] Go to: ${url.toString()}`);
|
||||
|
||||
open(url.toString(), {app: {name: open.apps.chrome}});
|
||||
|
||||
tracker.track('server-open-ui', {
|
||||
browser: true,
|
||||
hasToken: token?.length != 0,
|
||||
});
|
||||
};
|
||||
|
||||
if (preference === UIPreference.Browser) {
|
||||
await openInBrowser();
|
||||
} else {
|
||||
const path = await findInstallation();
|
||||
if (path) {
|
||||
console.info('[flipper-server] Open in PWA. Location:', path);
|
||||
tracker.track('server-open-ui', {
|
||||
browser: false,
|
||||
hasToken: token?.length != 0,
|
||||
});
|
||||
open(path);
|
||||
} else {
|
||||
await openInBrowser();
|
||||
}
|
||||
}
|
||||
|
||||
console.info('[flipper-server] Launch UI completed');
|
||||
}
|
||||
@@ -16,22 +16,22 @@ import {attachDevServer} from './attachDevServer';
|
||||
import {initializeLogger} from './logger';
|
||||
import fs from 'fs-extra';
|
||||
import yargs from 'yargs';
|
||||
import open from 'open';
|
||||
import os from 'os';
|
||||
import {initCompanionEnv} from 'flipper-server-companion';
|
||||
import {
|
||||
UIPreference,
|
||||
checkPortInUse,
|
||||
checkServerRunning,
|
||||
compareServerVersion,
|
||||
getEnvironmentInfo,
|
||||
openUI,
|
||||
shutdownRunningInstance,
|
||||
startFlipperServer,
|
||||
startServer,
|
||||
tracker,
|
||||
} from 'flipper-server-core';
|
||||
import {addLogTailer, isTest, LoggerFormat} from 'flipper-common';
|
||||
import {addLogTailer, isProduction, isTest, LoggerFormat} from 'flipper-common';
|
||||
import exitHook from 'exit-hook';
|
||||
import {getAuthToken, findInstallation} from 'flipper-server-core';
|
||||
|
||||
const argv = yargs
|
||||
.usage('yarn flipper-server [args]')
|
||||
@@ -295,47 +295,15 @@ async function start() {
|
||||
}
|
||||
|
||||
async function launch() {
|
||||
console.info('[flipper-server] Launch UI');
|
||||
|
||||
const token = await getAuthToken();
|
||||
console.info(
|
||||
`[flipper-server] Get authentication token: ${token?.length != 0}`,
|
||||
);
|
||||
|
||||
if (!argv.open) {
|
||||
console.warn(
|
||||
'[flipper-server] Not opening UI, --open flag was not provided',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const openInBrowser = async () => {
|
||||
console.info('[flipper-server] Open in browser');
|
||||
const url = new URL(`http://localhost:${argv.port}`);
|
||||
|
||||
console.info(`[flipper-server] Go to: ${chalk.blue(url.toString())}`);
|
||||
|
||||
open(url.toString(), {app: {name: open.apps.chrome}});
|
||||
|
||||
tracker.track('server-open-ui', {
|
||||
browser: true,
|
||||
hasToken: token?.length != 0,
|
||||
});
|
||||
};
|
||||
|
||||
if (argv.bundler) {
|
||||
await openInBrowser();
|
||||
} else {
|
||||
const path = await findInstallation();
|
||||
if (path) {
|
||||
tracker.track('server-open-ui', {
|
||||
browser: false,
|
||||
hasToken: token?.length != 0,
|
||||
});
|
||||
open(path);
|
||||
} else {
|
||||
await openInBrowser();
|
||||
}
|
||||
}
|
||||
|
||||
console.info('[flipper-server] Launch UI completed');
|
||||
const preference = isProduction() ? UIPreference.PWA : UIPreference.Browser;
|
||||
openUI(preference, argv.port);
|
||||
}
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
|
||||
Reference in New Issue
Block a user