headless
Summary: _typescript_ Reviewed By: passy Differential Revision: D17284499 fbshipit-source-id: 7306e36772911044a1d8b36c4a38f79b861eb2e6
This commit is contained in:
committed by
Facebook Github Bot
parent
fbe8e92991
commit
bb70b53c7c
@@ -6,11 +6,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {createStore} from 'redux';
|
import {createStore, Dispatch, Middleware, MiddlewareAPI} from 'redux';
|
||||||
import {applyMiddleware} from 'redux';
|
import {applyMiddleware} from 'redux';
|
||||||
import yargs, {Argv} from 'yargs';
|
import yargs, {Argv} from 'yargs';
|
||||||
import dispatcher from '../src/dispatcher/index';
|
import dispatcher from '../src/dispatcher/index';
|
||||||
import reducers from '../src/reducers/index';
|
import reducers, {Actions, State} from '../src/reducers/index';
|
||||||
import {init as initLogger} from '../src/fb-stubs/Logger';
|
import {init as initLogger} from '../src/fb-stubs/Logger';
|
||||||
import {exportStore, pluginsClassMap} from '../src/utils/exportData';
|
import {exportStore, pluginsClassMap} from '../src/utils/exportData';
|
||||||
import {
|
import {
|
||||||
@@ -19,11 +19,11 @@ import {
|
|||||||
} from '../src/utils/exportMetrics';
|
} from '../src/utils/exportMetrics';
|
||||||
import {listDevices} from '../src/utils/listDevices';
|
import {listDevices} from '../src/utils/listDevices';
|
||||||
import setup from '../static/setup.js';
|
import setup from '../static/setup.js';
|
||||||
import {Store} from '../src/reducers/index';
|
|
||||||
import {getPersistentPlugins} from '../src/utils/pluginUtils';
|
import {getPersistentPlugins} from '../src/utils/pluginUtils';
|
||||||
import {serialize} from '../src/utils/serialization';
|
import {serialize} from '../src/utils/serialization';
|
||||||
import {getStringFromErrorLike} from '../src/utils/index';
|
import {getStringFromErrorLike} from '../src/utils/index';
|
||||||
import AndroidDevice from '../src/devices/AndroidDevice';
|
import AndroidDevice from '../src/devices/AndroidDevice';
|
||||||
|
import {Store} from 'flipper';
|
||||||
|
|
||||||
type Action = {exit: boolean; result?: string};
|
type Action = {exit: boolean; result?: string};
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ type UserArguments = {
|
|||||||
selectPlugins: Array<string>;
|
selectPlugins: Array<string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
yargs
|
(yargs as Argv<UserArguments>)
|
||||||
.usage('$0 [args]')
|
.usage('$0 [args]')
|
||||||
.command<UserArguments>(
|
.command<UserArguments>(
|
||||||
'*',
|
'*',
|
||||||
@@ -107,14 +107,15 @@ yargs
|
|||||||
.version(global.__VERSION__)
|
.version(global.__VERSION__)
|
||||||
.help().argv; // http://yargs.js.org/docs/#api-argv
|
.help().argv; // http://yargs.js.org/docs/#api-argv
|
||||||
|
|
||||||
function shouldExportMetric(metrics): boolean {
|
function shouldExportMetric(metrics: string): boolean {
|
||||||
if (!metrics) {
|
if (!metrics) {
|
||||||
return process.argv.includes('--metrics');
|
return process.argv.includes('--metrics');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function outputAndExit(output: string): void {
|
function outputAndExit(output: string | null | undefined): void {
|
||||||
|
output = output || '';
|
||||||
console.log(`Finished. Outputting ${output.length} characters.`);
|
console.log(`Finished. Outputting ${output.length} characters.`);
|
||||||
process.stdout.write(output, () => {
|
process.stdout.write(output, () => {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
@@ -172,7 +173,7 @@ async function exitActions(
|
|||||||
store,
|
store,
|
||||||
state.pluginStates,
|
state.pluginStates,
|
||||||
);
|
);
|
||||||
outputAndExit(payload.toString());
|
outputAndExit(payload);
|
||||||
} else {
|
} else {
|
||||||
const {serializedString, errorArray} = await exportStore(store);
|
const {serializedString, errorArray} = await exportStore(store);
|
||||||
errorArray.forEach(console.error);
|
errorArray.forEach(console.error);
|
||||||
@@ -217,7 +218,7 @@ async function startFlipper(userArguments: UserArguments) {
|
|||||||
const originalConsole = global.console;
|
const originalConsole = global.console;
|
||||||
global.console = new Proxy(console, {
|
global.console = new Proxy(console, {
|
||||||
get: function(_obj, prop) {
|
get: function(_obj, prop) {
|
||||||
return (...args) => {
|
return (...args: any[]) => {
|
||||||
if (prop === 'error' || verbose) {
|
if (prop === 'error' || verbose) {
|
||||||
originalConsole.error(`[${String(prop)}] `, ...args);
|
originalConsole.error(`[${String(prop)}] `, ...args);
|
||||||
}
|
}
|
||||||
@@ -238,18 +239,20 @@ async function startFlipper(userArguments: UserArguments) {
|
|||||||
// needs to be required after WebSocket polyfill is loaded
|
// needs to be required after WebSocket polyfill is loaded
|
||||||
const devToolsEnhancer = require('remote-redux-devtools');
|
const devToolsEnhancer = require('remote-redux-devtools');
|
||||||
|
|
||||||
const headlessMiddleware = store => next => action => {
|
const headlessMiddleware: Middleware<{}, State, any> = (
|
||||||
|
store: MiddlewareAPI<Dispatch<Actions>, State>,
|
||||||
|
) => (next: Dispatch<Actions>) => (action: Actions) => {
|
||||||
if (exit == 'disconnect' && action.type == 'CLIENT_REMOVED') {
|
if (exit == 'disconnect' && action.type == 'CLIENT_REMOVED') {
|
||||||
// TODO(T42325892): Investigate why the export stalls without exiting the
|
// TODO(T42325892): Investigate why the export stalls without exiting the
|
||||||
// current eventloop task here.
|
// current eventloop task here.
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (shouldExportMetric(metrics) && !metrics) {
|
if (shouldExportMetric(metrics) && !metrics) {
|
||||||
const state = store.getState();
|
const state = store.getState();
|
||||||
exportMetricsWithoutTrace(state, state.pluginStates)
|
exportMetricsWithoutTrace(store as Store, state.pluginStates)
|
||||||
.then(payload => {
|
.then((payload: string | null) => {
|
||||||
outputAndExit(payload || '');
|
outputAndExit(payload || '');
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch((e: Error) => {
|
||||||
errorAndExit(e);
|
errorAndExit(e);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -257,7 +260,7 @@ async function startFlipper(userArguments: UserArguments) {
|
|||||||
.then(({serializedString}) => {
|
.then(({serializedString}) => {
|
||||||
outputAndExit(serializedString);
|
outputAndExit(serializedString);
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch((e: Error) => {
|
||||||
errorAndExit(e);
|
errorAndExit(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -267,7 +270,7 @@ async function startFlipper(userArguments: UserArguments) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
setup({});
|
setup({});
|
||||||
const store = createStore(
|
const store = createStore<State, Actions, {}, {}>(
|
||||||
reducers,
|
reducers,
|
||||||
devToolsEnhancer.composeWithDevTools(applyMiddleware(headlessMiddleware)),
|
devToolsEnhancer.composeWithDevTools(applyMiddleware(headlessMiddleware)),
|
||||||
);
|
);
|
||||||
|
|||||||
22
static/setup.d.ts
vendored
Normal file
22
static/setup.d.ts
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
type Config = {
|
||||||
|
pluginPaths?: string[],
|
||||||
|
disabledPlugins?: string[],
|
||||||
|
lastWindowPosition?: {
|
||||||
|
width: number,
|
||||||
|
height: number
|
||||||
|
},
|
||||||
|
updater?: boolean | undefined,
|
||||||
|
launcherMsg?: string | undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function(argv: {
|
||||||
|
updater?: boolean,
|
||||||
|
launcherMsg?: string
|
||||||
|
}): {config: Config, configPath: string, flipperDir: string};
|
||||||
Reference in New Issue
Block a user