From bb70b53c7c623b5bce54a3e90d83c7cc76243285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Wed, 11 Sep 2019 03:01:18 -0700 Subject: [PATCH] headless Summary: _typescript_ Reviewed By: passy Differential Revision: D17284499 fbshipit-source-id: 7306e36772911044a1d8b36c4a38f79b861eb2e6 --- headless/index.tsx | 31 +++++++++++++++++-------------- static/setup.d.ts | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 static/setup.d.ts diff --git a/headless/index.tsx b/headless/index.tsx index fd0b72420..49d125029 100644 --- a/headless/index.tsx +++ b/headless/index.tsx @@ -6,11 +6,11 @@ */ import path from 'path'; -import {createStore} from 'redux'; +import {createStore, Dispatch, Middleware, MiddlewareAPI} from 'redux'; import {applyMiddleware} from 'redux'; import yargs, {Argv} from 'yargs'; 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 {exportStore, pluginsClassMap} from '../src/utils/exportData'; import { @@ -19,11 +19,11 @@ import { } from '../src/utils/exportMetrics'; import {listDevices} from '../src/utils/listDevices'; import setup from '../static/setup.js'; -import {Store} from '../src/reducers/index'; import {getPersistentPlugins} from '../src/utils/pluginUtils'; import {serialize} from '../src/utils/serialization'; import {getStringFromErrorLike} from '../src/utils/index'; import AndroidDevice from '../src/devices/AndroidDevice'; +import {Store} from 'flipper'; type Action = {exit: boolean; result?: string}; @@ -40,7 +40,7 @@ type UserArguments = { selectPlugins: Array; }; -yargs +(yargs as Argv) .usage('$0 [args]') .command( '*', @@ -107,14 +107,15 @@ yargs .version(global.__VERSION__) .help().argv; // http://yargs.js.org/docs/#api-argv -function shouldExportMetric(metrics): boolean { +function shouldExportMetric(metrics: string): boolean { if (!metrics) { return process.argv.includes('--metrics'); } return true; } -function outputAndExit(output: string): void { +function outputAndExit(output: string | null | undefined): void { + output = output || ''; console.log(`Finished. Outputting ${output.length} characters.`); process.stdout.write(output, () => { process.exit(0); @@ -172,7 +173,7 @@ async function exitActions( store, state.pluginStates, ); - outputAndExit(payload.toString()); + outputAndExit(payload); } else { const {serializedString, errorArray} = await exportStore(store); errorArray.forEach(console.error); @@ -217,7 +218,7 @@ async function startFlipper(userArguments: UserArguments) { const originalConsole = global.console; global.console = new Proxy(console, { get: function(_obj, prop) { - return (...args) => { + return (...args: any[]) => { if (prop === 'error' || verbose) { originalConsole.error(`[${String(prop)}] `, ...args); } @@ -238,18 +239,20 @@ async function startFlipper(userArguments: UserArguments) { // needs to be required after WebSocket polyfill is loaded const devToolsEnhancer = require('remote-redux-devtools'); - const headlessMiddleware = store => next => action => { + const headlessMiddleware: Middleware<{}, State, any> = ( + store: MiddlewareAPI, State>, + ) => (next: Dispatch) => (action: Actions) => { if (exit == 'disconnect' && action.type == 'CLIENT_REMOVED') { // TODO(T42325892): Investigate why the export stalls without exiting the // current eventloop task here. setTimeout(() => { if (shouldExportMetric(metrics) && !metrics) { const state = store.getState(); - exportMetricsWithoutTrace(state, state.pluginStates) - .then(payload => { + exportMetricsWithoutTrace(store as Store, state.pluginStates) + .then((payload: string | null) => { outputAndExit(payload || ''); }) - .catch(e => { + .catch((e: Error) => { errorAndExit(e); }); } else { @@ -257,7 +260,7 @@ async function startFlipper(userArguments: UserArguments) { .then(({serializedString}) => { outputAndExit(serializedString); }) - .catch(e => { + .catch((e: Error) => { errorAndExit(e); }); } @@ -267,7 +270,7 @@ async function startFlipper(userArguments: UserArguments) { }; setup({}); - const store = createStore( + const store = createStore( reducers, devToolsEnhancer.composeWithDevTools(applyMiddleware(headlessMiddleware)), ); diff --git a/static/setup.d.ts b/static/setup.d.ts new file mode 100644 index 000000000..6e639e687 --- /dev/null +++ b/static/setup.d.ts @@ -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};