From 71bb121ab82e4d38a93ae5a0745ad2aa06a2191f Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Mon, 9 Sep 2019 10:22:39 -0700 Subject: [PATCH] Make dispatcher/plugins strict Summary: _typescript_ Reviewed By: jknoxville Differential Revision: D17258265 fbshipit-source-id: 875d434120422c782074d4c345f765684533e399 --- src/dispatcher/plugins.tsx | 23 +++++++++++++---------- types/nodejs.tsx | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/dispatcher/plugins.tsx b/src/dispatcher/plugins.tsx index 94d9baa46..4298a3f2e 100644 --- a/src/dispatcher/plugins.tsx +++ b/src/dispatcher/plugins.tsx @@ -25,6 +25,7 @@ import {setupMenuBar} from '../MenuBar'; import path from 'path'; import {default as config} from '../utils/processConfig'; import isProduction from '../utils/isProduction'; +import {notNull} from '../utils/typeUtils'; export type PluginDefinition = { id?: string; @@ -51,7 +52,7 @@ export default (store: Store, logger: Logger) => { .filter(checkDisabled(disabledPlugins)) .filter(checkGK(gatekeepedPlugins)) .map(requirePlugin(failedPlugins)) - .filter(Boolean); + .filter(notNull); store.dispatch(addGatekeepedPlugins(gatekeepedPlugins)); store.dispatch(addDisabledPlugins(disabledPlugins)); @@ -87,18 +88,19 @@ function getBundledPlugins(): Array { let bundledPlugins: Array = []; try { - // TODO We can probably define this in the globals file. - bundledPlugins = (global as any).electronRequire( + bundledPlugins = global.electronRequire( path.join(pluginPath, 'index.json'), ); } catch (e) { console.error(e); } - return bundledPlugins.map(plugin => ({ - ...plugin, - out: path.join(pluginPath, plugin.out), - })); + return bundledPlugins + .filter(plugin => notNull(plugin.out)) + .map(plugin => ({ + ...plugin, + out: path.join(pluginPath, plugin.out!), + })); } export function getDynamicPlugins() { @@ -146,11 +148,11 @@ export const checkDisabled = (disabledPlugins: Array) => ( export const requirePlugin = ( failedPlugins: Array<[PluginDefinition, string]>, - reqFn: Function = (global as any).electronRequire, + reqFn: Function = global.electronRequire, ) => { return ( pluginDefinition: PluginDefinition, - ): typeof FlipperPlugin | typeof FlipperDevicePlugin => { + ): typeof FlipperPlugin | typeof FlipperDevicePlugin | null => { try { let plugin = reqFn(pluginDefinition.out); if (plugin.default) { @@ -169,7 +171,8 @@ export const requirePlugin = ( 'Field "id" not allowed in package.json. The plugin\'s name will be used as ID"', ); } else { - plugin[key] = plugin[key] || pluginDefinition[key]; + plugin[key] = + plugin[key] || pluginDefinition[key as keyof PluginDefinition]; } }); diff --git a/types/nodejs.tsx b/types/nodejs.tsx index 8c506f14f..1e490a05f 100644 --- a/types/nodejs.tsx +++ b/types/nodejs.tsx @@ -9,7 +9,7 @@ declare module NodeJS { interface Global { __REVISION__: string | undefined; __VERSION__: string; - electronRequire: (name: string) => void; + electronRequire: (name: string) => any; window: Window | undefined; WebSocket: any; fetch: any;