Move FlipperServer initialisation out of flipper-core
Summary: This diff makes sure flipper-ui-core no longer depends on flipper-server-core. Currently server config is still transferred from UI to server, which doesn't really make sense in future scenarios where server might start before client, but will address that separately Reviewed By: timur-valiev, aigoncharov Differential Revision: D32462835 fbshipit-source-id: 498a944256ba1aabbf963b896953e64d11e27214
This commit is contained in:
committed by
Facebook GitHub Bot
parent
d48f22b8dc
commit
eed19b3a3d
@@ -26,9 +26,12 @@ import type {RenderHost} from 'flipper-ui-core';
|
||||
import fs from 'fs';
|
||||
import {setupMenuBar} from './setupMenuBar';
|
||||
import os from 'os';
|
||||
import {FlipperServerImpl} from 'flipper-server-core';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
// We store this as a global, to make sure the renderHost is available
|
||||
// before flipper-ui-core is loaded and needs those during module initialisation
|
||||
FlipperRenderHostInstance: RenderHost;
|
||||
}
|
||||
}
|
||||
@@ -46,6 +49,8 @@ export function initializeElectron() {
|
||||
const app = remote.app;
|
||||
const execPath = process.execPath || remote.process.execPath;
|
||||
const isProduction = !/node_modules[\\/]electron[\\/]/.test(execPath);
|
||||
const staticPath = getStaticDir();
|
||||
const tempPath = app.getPath('temp');
|
||||
|
||||
function restart(update: boolean = false) {
|
||||
if (isProduction) {
|
||||
@@ -192,11 +197,21 @@ export function initializeElectron() {
|
||||
appPath: app.getAppPath(),
|
||||
homePath: app.getPath('home'),
|
||||
execPath,
|
||||
staticPath: getStaticDir(),
|
||||
tempPath: app.getPath('temp'),
|
||||
staticPath,
|
||||
tempPath,
|
||||
desktopPath: app.getPath('desktop'),
|
||||
},
|
||||
loadDefaultPlugins: getDefaultPluginsIndex,
|
||||
startFlipperServer({logger, ...config}) {
|
||||
return new FlipperServerImpl(
|
||||
{
|
||||
...config,
|
||||
staticPath,
|
||||
tempPath,
|
||||
},
|
||||
logger,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
setupMenuBar();
|
||||
|
||||
@@ -154,6 +154,7 @@ export type FlipperServerCommands = {
|
||||
};
|
||||
|
||||
export interface FlipperServer {
|
||||
start(): Promise<void>;
|
||||
on<Event extends keyof FlipperServerEvents>(
|
||||
event: Event,
|
||||
callback: (payload: FlipperServerEvents[Event]) => void,
|
||||
|
||||
@@ -74,7 +74,7 @@ async function start(deviceTitle: string, appName: string, pluginId: string) {
|
||||
enableIOS: true,
|
||||
enablePhysicalIOS: true,
|
||||
staticPath: path.resolve(__dirname, '..', '..', 'static'),
|
||||
tmpPath: os.tmpdir(),
|
||||
tempPath: os.tmpdir(),
|
||||
validWebSocketOrigins: [],
|
||||
},
|
||||
logger,
|
||||
|
||||
@@ -40,7 +40,11 @@ import {stubLogger} from '../utils/Logger';
|
||||
import {Idler} from '../utils/Idler';
|
||||
import {createState} from '../state/atom';
|
||||
import baseMockConsole from 'jest-mock-console';
|
||||
import {DeviceLogEntry} from 'flipper-common';
|
||||
import {
|
||||
DeviceLogEntry,
|
||||
FlipperServer,
|
||||
FlipperServerCommands,
|
||||
} from 'flipper-common';
|
||||
|
||||
type Renderer = RenderResult<typeof queries>;
|
||||
|
||||
@@ -581,3 +585,26 @@ export function mockConsole() {
|
||||
}
|
||||
|
||||
export type MockedConsole = ReturnType<typeof mockConsole>;
|
||||
|
||||
export function createFlipperServerMock(
|
||||
overrides?: Partial<FlipperServerCommands>,
|
||||
): FlipperServer {
|
||||
return {
|
||||
async start() {},
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
exec: jest
|
||||
.fn()
|
||||
.mockImplementation(
|
||||
(cmd: keyof FlipperServerCommands, ...args: any[]) => {
|
||||
if (overrides?.[cmd]) {
|
||||
return (overrides[cmd] as any)(...args);
|
||||
}
|
||||
return Promise.reject(
|
||||
new Error(`FlipperServerMock exec not implemented: ${cmd}}`),
|
||||
);
|
||||
},
|
||||
),
|
||||
close: jest.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ export interface FlipperServerConfig {
|
||||
enablePhysicalIOS: boolean;
|
||||
validWebSocketOrigins: string[];
|
||||
staticPath: string;
|
||||
tmpPath: string;
|
||||
tempPath: string;
|
||||
}
|
||||
|
||||
// defaultConfig should be used for testing only, and disables by default all features
|
||||
@@ -30,7 +30,7 @@ const testConfig: FlipperServerConfig = {
|
||||
idbPath: '',
|
||||
validWebSocketOrigins: [],
|
||||
staticPath: '/static/',
|
||||
tmpPath: '/temp/',
|
||||
tempPath: '/temp/',
|
||||
};
|
||||
|
||||
let currentConfig: FlipperServerConfig | undefined = undefined;
|
||||
|
||||
@@ -98,7 +98,7 @@ export function xcrunStartLogListener(udid: string, deviceType: DeviceType) {
|
||||
|
||||
function makeTempScreenshotFilePath() {
|
||||
const imageName = uuid() + '.png';
|
||||
return path.join(getFlipperServerConfig().tmpPath, imageName);
|
||||
return path.join(getFlipperServerConfig().tempPath, imageName);
|
||||
}
|
||||
|
||||
async function runScreenshotCommand(
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
"flipper-doctor": "0.0.0",
|
||||
"flipper-plugin": "0.0.0",
|
||||
"flipper-plugin-lib": "0.0.0",
|
||||
"flipper-server-core": "0.0.0",
|
||||
"flipper-ui-core": "0.0.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
"immer": "^9.0.6",
|
||||
|
||||
@@ -10,8 +10,9 @@
|
||||
import type {NotificationEvents} from './dispatcher/notifications';
|
||||
import type {PluginNotification} from './reducers/notifications';
|
||||
import type {NotificationConstructorOptions} from 'electron';
|
||||
import type {FlipperLib} from 'flipper-plugin';
|
||||
import {FlipperLib, TestUtils} from 'flipper-plugin';
|
||||
import path from 'path';
|
||||
import {FlipperServer, Logger} from 'flipper-common';
|
||||
|
||||
type ENVIRONMENT_VARIABLES = 'NODE_ENV' | 'DEV_SERVER_URL' | 'CONFIG';
|
||||
type ENVIRONMENT_PATHS =
|
||||
@@ -106,6 +107,16 @@ export interface RenderHost {
|
||||
paths: Record<ENVIRONMENT_PATHS, string>;
|
||||
openLink(url: string): void;
|
||||
loadDefaultPlugins(): Record<string, any>;
|
||||
startFlipperServer(config: {
|
||||
// TODO: this config is temporarily, settings should be loaded/stored by server, not client
|
||||
logger: Logger;
|
||||
enableAndroid: boolean;
|
||||
androidHome: string;
|
||||
enableIOS: boolean;
|
||||
enablePhysicalIOS: boolean;
|
||||
idbPath: string;
|
||||
validWebSocketOrigins: string[];
|
||||
}): FlipperServer;
|
||||
}
|
||||
|
||||
export function getRenderHostInstance(): RenderHost {
|
||||
@@ -154,5 +165,6 @@ if (process.env.NODE_ENV === 'test') {
|
||||
loadDefaultPlugins() {
|
||||
return {};
|
||||
},
|
||||
startFlipperServer: () => TestUtils.createFlipperServerMock(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ Object {
|
||||
"exec": [MockFunction],
|
||||
"off": [MockFunction],
|
||||
"on": [MockFunction],
|
||||
"start": [Function],
|
||||
},
|
||||
"pluginMenuEntries": Array [],
|
||||
"selectedAppId": "TestApp#Android#MockAndroidDevice#serial",
|
||||
|
||||
@@ -26,6 +26,7 @@ export default class ArchivedDevice extends BaseDevice {
|
||||
}) {
|
||||
super(
|
||||
{
|
||||
async start() {},
|
||||
close() {},
|
||||
exec(command, ..._args: any[]) {
|
||||
throw new Error(
|
||||
|
||||
@@ -14,15 +14,12 @@ import {
|
||||
Logger,
|
||||
NoLongerConnectedToClientError,
|
||||
} from 'flipper-common';
|
||||
import {FlipperServerImpl} from 'flipper-server-core';
|
||||
import {selectClient} from '../reducers/connections';
|
||||
import Client from '../Client';
|
||||
import {notification} from 'antd';
|
||||
import BaseDevice from '../devices/BaseDevice';
|
||||
import {ClientDescription, timeout} from 'flipper-common';
|
||||
import {reportPlatformFailures} from 'flipper-common';
|
||||
import {sideEffect} from '../utils/sideEffect';
|
||||
import {getStaticPath} from '../utils/pathUtils';
|
||||
import constants from '../fb-stubs/constants';
|
||||
import {getRenderHostInstance} from '../RenderHost';
|
||||
|
||||
@@ -30,19 +27,15 @@ export default async (store: Store, logger: Logger) => {
|
||||
const {enableAndroid, androidHome, idbPath, enableIOS, enablePhysicalIOS} =
|
||||
store.getState().settingsState;
|
||||
|
||||
const server = new FlipperServerImpl(
|
||||
{
|
||||
const server = getRenderHostInstance().startFlipperServer({
|
||||
logger,
|
||||
enableAndroid,
|
||||
androidHome,
|
||||
idbPath,
|
||||
enableIOS,
|
||||
enablePhysicalIOS,
|
||||
staticPath: getStaticPath(),
|
||||
tmpPath: getRenderHostInstance().paths.tempPath,
|
||||
validWebSocketOrigins: constants.VALID_WEB_SOCKET_REQUEST_ORIGIN_PREFIXES,
|
||||
},
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
store.dispatch({
|
||||
type: 'SET_FLIPPER_SERVER',
|
||||
|
||||
@@ -14,12 +14,11 @@ import {createStore} from 'redux';
|
||||
import {LaunchEmulatorDialog} from '../LaunchEmulator';
|
||||
|
||||
import {createRootReducer} from '../../../reducers';
|
||||
import {sleep} from 'flipper-plugin';
|
||||
import {createFlipperServerMock} from '../../../test-utils/createFlipperServerMock';
|
||||
import {sleep, TestUtils} from 'flipper-plugin';
|
||||
|
||||
test('Can render and launch android apps - empty', async () => {
|
||||
const store = createStore(createRootReducer());
|
||||
const mockServer = createFlipperServerMock({
|
||||
const mockServer = TestUtils.createFlipperServerMock({
|
||||
'ios-get-simulators': () => Promise.resolve([]),
|
||||
'android-get-emulators': () => Promise.resolve([]),
|
||||
});
|
||||
@@ -49,7 +48,7 @@ test('Can render and launch android apps', async () => {
|
||||
|
||||
const store = createStore(createRootReducer());
|
||||
const launch = jest.fn().mockImplementation(() => Promise.resolve());
|
||||
const mockServer = createFlipperServerMock({
|
||||
const mockServer = TestUtils.createFlipperServerMock({
|
||||
'ios-get-simulators': () => Promise.resolve([]),
|
||||
'android-get-emulators': () =>
|
||||
(p = Promise.resolve(['emulator1', 'emulator2'])),
|
||||
|
||||
@@ -27,8 +27,8 @@ import {PluginDetails} from 'flipper-plugin-lib';
|
||||
import ArchivedDevice from '../devices/ArchivedDevice';
|
||||
import {ClientQuery, DeviceOS} from 'flipper-common';
|
||||
import {TestDevice} from './TestDevice';
|
||||
import {createFlipperServerMock} from './createFlipperServerMock';
|
||||
import {getRenderHostInstance} from '../RenderHost';
|
||||
import {TestUtils} from 'flipper-plugin';
|
||||
|
||||
export interface AppOptions {
|
||||
plugins?: PluginDefinition[];
|
||||
@@ -58,7 +58,7 @@ export default class MockFlipper {
|
||||
private _clients: Client[] = [];
|
||||
private _deviceCounter: number = 0;
|
||||
private _clientCounter: number = 0;
|
||||
flipperServer: FlipperServer = createFlipperServerMock();
|
||||
flipperServer: FlipperServer = TestUtils.createFlipperServerMock();
|
||||
|
||||
public get store(): Store {
|
||||
return this._store;
|
||||
|
||||
@@ -21,6 +21,7 @@ export class TestDevice extends BaseDevice {
|
||||
) {
|
||||
super(
|
||||
{
|
||||
async start() {},
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
exec: jest.fn(),
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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 {FlipperServer, FlipperServerCommands} from 'flipper-common';
|
||||
|
||||
export function createFlipperServerMock(
|
||||
overrides?: Partial<FlipperServerCommands>,
|
||||
): FlipperServer {
|
||||
return {
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
exec: jest
|
||||
.fn()
|
||||
.mockImplementation(
|
||||
(cmd: keyof FlipperServerCommands, ...args: any[]) => {
|
||||
if (overrides?.[cmd]) {
|
||||
return (overrides[cmd] as any)(...args);
|
||||
}
|
||||
return Promise.reject(
|
||||
new Error(`FlipperServerMock exec not implemented: ${cmd}}`),
|
||||
);
|
||||
},
|
||||
),
|
||||
close: jest.fn(),
|
||||
};
|
||||
}
|
||||
@@ -16,9 +16,6 @@
|
||||
{
|
||||
"path": "../flipper-plugin"
|
||||
},
|
||||
{
|
||||
"path": "../flipper-server-core"
|
||||
},
|
||||
{
|
||||
"path": "../plugin-lib"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user