From 853d80f18216d1dce0ab1c3d0149dbf2604ad89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 23 Aug 2019 09:30:49 -0700 Subject: [PATCH] headless Summary: _typescript_ Reviewed By: passy Differential Revision: D16962733 fbshipit-source-id: 6583c5a4471b12f91895f6449ed9c510265a63e2 --- headless/{index.js => index.tsx} | 170 +++++++++++++++---------------- scripts/build-headless.js | 2 +- tsconfig.json | 4 +- types/nodejs.tsx | 3 + 4 files changed, 88 insertions(+), 91 deletions(-) rename headless/{index.js => index.tsx} (73%) diff --git a/headless/index.js b/headless/index.tsx similarity index 73% rename from headless/index.js rename to headless/index.tsx index c91b325f5..fd0b72420 100644 --- a/headless/index.js +++ b/headless/index.tsx @@ -8,46 +8,44 @@ import path from 'path'; import {createStore} from 'redux'; import {applyMiddleware} from 'redux'; -import yargs from 'yargs'; -import dispatcher from '../src/dispatcher/index.tsx'; -import reducers from '../src/reducers/index.tsx'; -import {init as initLogger} from '../src/fb-stubs/Logger.tsx'; -import {exportStore, pluginsClassMap} from '../src/utils/exportData.tsx'; +import yargs, {Argv} from 'yargs'; +import dispatcher from '../src/dispatcher/index'; +import reducers from '../src/reducers/index'; +import {init as initLogger} from '../src/fb-stubs/Logger'; +import {exportStore, pluginsClassMap} from '../src/utils/exportData'; import { exportMetricsWithoutTrace, exportMetricsFromTrace, -} from '../src/utils/exportMetrics.tsx'; -import {listDevices} from '../src/utils/listDevices.tsx'; -// $FlowFixMe this file exist, trust me, flow! +} from '../src/utils/exportMetrics'; +import {listDevices} from '../src/utils/listDevices'; import setup from '../static/setup.js'; -import type {Store} from '../src/reducers/index.tsx'; -import {getPersistentPlugins} from '../src/utils/pluginUtils.tsx'; -import {serialize} from '../src/utils/serialization.tsx'; -import type BaseDevice from '../src/devices/BaseDevice.tsx'; +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 {getStringFromErrorLike} from '../src/utils/index.tsx'; +type Action = {exit: boolean; result?: string}; -type Action = {|exit: true, result: string|} | {|exit: false|}; - -type UserArguments = {| - securePort: string, - insecurePort: string, - dev: boolean, - exit: 'sigint' | 'disconnect', - verbose: boolean, - metrics: string, - listDevices: boolean, - device: string, - listPlugins: boolean, - selectPlugins: Array, -|}; +type UserArguments = { + securePort: string; + insecurePort: string; + dev: boolean; + exit: 'sigint' | 'disconnect'; + verbose: boolean; + metrics: string; + listDevices: boolean; + device: string; + listPlugins: boolean; + selectPlugins: Array; +}; yargs .usage('$0 [args]') - .command( + .command( '*', 'Start a headless Flipper instance', - yargs => { + (yargs: Argv) => { yargs.option('secure-port', { default: '8088', describe: 'Secure port the Flipper server should run on.', @@ -102,6 +100,7 @@ yargs 'The identifier passed will be matched against the udid of the available devices and the matched device would be selected', type: 'string', }); + return yargs; }, startFlipper, ) @@ -131,7 +130,7 @@ function errorAndExit(error: any): void { async function earlyExitActions( exitClosures: Array<(userArguments: UserArguments) => Promise>, userArguments: UserArguments, - originalConsole: typeof global.console, + _originalConsole?: typeof global.console, ): Promise { for (const exitAction of exitClosures) { try { @@ -147,7 +146,7 @@ async function earlyExitActions( async function exitActions( exitClosures: Array< - (userArguments: UserArguments, store: Store) => Promise, + (userArguments: UserArguments, store: Store) => Promise >, userArguments: UserArguments, store: Store, @@ -188,7 +187,7 @@ async function exitActions( async function storeModifyingActions( storeModifyingClosures: Array< - (userArguments: UserArguments, store: Store) => Promise, + (userArguments: UserArguments, store: Store) => Promise >, userArguments: UserArguments, store: Store, @@ -217,10 +216,10 @@ async function startFlipper(userArguments: UserArguments) { // redirect all logging to stderr const originalConsole = global.console; global.console = new Proxy(console, { - get: function(obj, prop) { + get: function(_obj, prop) { return (...args) => { if (prop === 'error' || verbose) { - originalConsole.error(`[${prop}] `, ...args); + originalConsole.error(`[${String(prop)}] `, ...args); } }; }, @@ -275,21 +274,20 @@ async function startFlipper(userArguments: UserArguments) { const logger = initLogger(store, {isHeadless: true}); const earlyExitClosures: Array< - (userArguments: UserArguments) => Promise, + (userArguments: UserArguments) => Promise > = [ - (userArguments: UserArguments) => { + async (userArguments: UserArguments) => { if (userArguments.listDevices) { - return listDevices().then(async (devices: Array) => { - const mapped = devices.map(device => { - return { - os: device.os, - title: device.title, - deviceType: device.deviceType, - serial: device.serial, - }; - }); - return {exit: true, result: await serialize(mapped)}; + const devices = await listDevices(); + const mapped = devices.map(device => { + return { + os: device.os, + title: device.title, + deviceType: device.deviceType, + serial: device.serial, + }; }); + return {exit: true, result: await serialize(mapped)}; } return Promise.resolve({exit: false}); }, @@ -300,40 +298,37 @@ async function startFlipper(userArguments: UserArguments) { const cleanupDispatchers = dispatcher(store, logger); const storeModifyingClosures: Array< - (userArguments: UserArguments, store: Store) => Promise, + (userArguments: UserArguments, store: Store) => Promise > = [ - (userArguments: UserArguments, store: Store) => { + async (userArguments: UserArguments, store: Store) => { const {device: selectedDeviceID} = userArguments; if (selectedDeviceID) { - return listDevices().then(devices => { - const matchedDevice = devices.find( - device => device.serial === selectedDeviceID, - ); - if (matchedDevice) { - if (matchedDevice.constructor.name === 'AndroidDevice') { - const ports = store.getState().application.serverPorts; - matchedDevice.reverse([ports.secure, ports.insecure]); - } - store.dispatch({ - type: 'REGISTER_DEVICE', - payload: matchedDevice, - }); - store.dispatch({ - type: 'SELECT_DEVICE', - payload: matchedDevice, - }); - return {exit: false}; + const devices = await listDevices(); + const matchedDevice = devices.find( + device => device.serial === selectedDeviceID, + ); + if (matchedDevice) { + if (matchedDevice instanceof AndroidDevice) { + const ports = store.getState().application.serverPorts; + matchedDevice.reverse([ports.secure, ports.insecure]); } - return Promise.reject( - new Error(`No device matching the serial ${selectedDeviceID}`), - ); - }); + store.dispatch({ + type: 'REGISTER_DEVICE', + payload: matchedDevice, + }); + store.dispatch({ + type: 'SELECT_DEVICE', + payload: matchedDevice, + }); + return {exit: false}; + } + throw new Error(`No device matching the serial ${selectedDeviceID}`); } - return Promise.resolve({ + return { exit: false, - }); + }; }, - (userArguments: UserArguments, store: Store) => { + async (userArguments: UserArguments, store: Store) => { const {selectPlugins} = userArguments; const selectedPlugins = selectPlugins.filter(selectPlugin => { return selectPlugin != undefined; @@ -344,14 +339,14 @@ async function startFlipper(userArguments: UserArguments) { payload: selectedPlugins, }); } - return Promise.resolve({ + return { exit: false, - }); + }; }, ]; const exitActionClosures: Array< - (userArguments: UserArguments, store: Store) => Promise, + (userArguments: UserArguments, store: Store) => Promise > = [ async (userArguments: UserArguments, store: Store) => { const {listPlugins} = userArguments; @@ -367,20 +362,19 @@ async function startFlipper(userArguments: UserArguments) { exit: false, }); }, - (userArguments: UserArguments, store: Store) => { + async (userArguments: UserArguments, store: Store) => { const {metrics} = userArguments; if (shouldExportMetric(metrics) && metrics && metrics.length > 0) { - return exportMetricsFromTrace( - metrics, - pluginsClassMap(store.getState().plugins), - store.getState().plugins.selectedPlugins, - ) - .then(payload => { - return {exit: true, result: payload ? payload.toString() : ''}; - }) - .catch(error => { - return {exit: true, result: error}; - }); + try { + const payload = await exportMetricsFromTrace( + metrics, + pluginsClassMap(store.getState().plugins), + store.getState().plugins.selectedPlugins, + ); + return {exit: true, result: payload ? payload.toString() : ''}; + } catch (error) { + return {exit: true, result: error}; + } } return Promise.resolve({exit: false}); }, diff --git a/scripts/build-headless.js b/scripts/build-headless.js index 6a54a1913..ea23586d8 100644 --- a/scripts/build-headless.js +++ b/scripts/build-headless.js @@ -93,7 +93,7 @@ async function createZip(buildDir, distDir, targets) { const distDir = path.join(__dirname, '..', 'dist'); // eslint-disable-next-line no-console console.log('Created build directory', buildDir); - await compile(buildDir, path.join(__dirname, '..', 'headless', 'index.js')); + await compile(buildDir, path.join(__dirname, '..', 'headless', 'index.tsx')); const versionNumber = getVersionNumber(); const buildRevision = await genMercurialRevision(); await preludeBundle(buildDir, versionNumber, buildRevision); diff --git a/tsconfig.json b/tsconfig.json index 65541c9b2..e8872ee55 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,9 +13,9 @@ "downlevelIteration": true, "esModuleInterop": true, "paths": { - "flipper": ["./src/index.js"] + "flipper": ["./src/index.tsx"] } }, - "include": ["src/**/*", "types/*"], + "include": ["src/**/*", "types/*", "headless/*"], "exclude": ["node_modules", "**/*.spec.ts"] } diff --git a/types/nodejs.tsx b/types/nodejs.tsx index 5a165885d..8c506f14f 100644 --- a/types/nodejs.tsx +++ b/types/nodejs.tsx @@ -8,7 +8,10 @@ declare module NodeJS { interface Global { __REVISION__: string | undefined; + __VERSION__: string; electronRequire: (name: string) => void; window: Window | undefined; + WebSocket: any; + fetch: any; } }