From 0c778af679e1778cfd19b3bfdbd4c5a8373a5ab8 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Mon, 24 Feb 2020 05:17:16 -0800 Subject: [PATCH] Typescriptify the main process code (2/N) Summary: Converted main.js and setup.js to typescript Reviewed By: mweststrate Differential Revision: D19972273 fbshipit-source-id: f777be737da4233de4d7df3d549206efabfeeadf --- headless/index.tsx | 2 +- scripts/build-utils.js | 2 +- static/{main.js => main.ts} | 46 ++++++++++++++++++++--------------- static/setup.d.ts | 24 ------------------ static/{setup.js => setup.ts} | 30 +++++++++++++++++------ 5 files changed, 51 insertions(+), 53 deletions(-) rename static/{main.js => main.ts} (89%) delete mode 100644 static/setup.d.ts rename static/{setup.js => setup.ts} (66%) diff --git a/headless/index.tsx b/headless/index.tsx index b952c7ace..a71314a57 100644 --- a/headless/index.tsx +++ b/headless/index.tsx @@ -20,7 +20,7 @@ import { exportMetricsFromTrace, } from '../src/utils/exportMetrics'; import {listDevices} from '../src/utils/listDevices'; -import setup from '../static/setup.js'; +import setup from '../static/setup'; import {getPersistentPlugins, pluginsClassMap} from '../src/utils/pluginUtils'; import {serialize} from '../src/utils/serialization'; import {getStringFromErrorLike} from '../src/utils/index'; diff --git a/scripts/build-utils.js b/scripts/build-utils.js index 2ce1202d8..d63eead98 100644 --- a/scripts/build-utils.js +++ b/scripts/build-utils.js @@ -104,7 +104,7 @@ async function compileMain() { }); await Metro.runBuild(config, { platform: 'web', - entry: path.join(staticDir, 'main.js'), + entry: path.join(staticDir, 'main.ts'), out: path.join(staticDir, 'main.bundle.js'), dev: false, minify: false, diff --git a/static/main.js b/static/main.ts similarity index 89% rename from static/main.js rename to static/main.ts index 6733f9174..e0880361e 100644 --- a/static/main.js +++ b/static/main.ts @@ -10,23 +10,25 @@ const [s, ns] = process.hrtime(); let launchStartTime = s * 1e3 + ns / 1e6; -const {app, BrowserWindow, ipcMain, Notification} = require('electron'); -const path = require('path'); -const url = require('url'); -const fs = require('fs'); -const fixPath = require('fix-path'); -const {exec} = require('child_process'); +import {app, BrowserWindow, ipcMain, Notification} from 'electron'; +import path from 'path'; +import url from 'url'; +import fs from 'fs'; +import fixPath from 'fix-path'; +import {exec} from 'child_process'; const compilePlugins = require('./compilePlugins'); -const setup = require('./setup'); +import setup from './setup'; const delegateToLauncher = require('./launcher'); const expandTilde = require('expand-tilde'); const yargs = require('yargs'); +const VERSION: string = (global as any).__VERSION__; + // Adds system PATH folders to process.env.PATH for MacOS production bundles. fixPath(); // disable electron security warnings: https://github.com/electron/electron/blob/master/docs/tutorial/security.md#security-native-capabilities-and-your-responsibility -process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true; +process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'; if (process.platform === 'darwin') { // If we are running on macOS and the app is called Flipper, we add a comment @@ -68,7 +70,7 @@ const argv = yargs '[Internal] Used to provide a user message from the launcher to the user.', type: 'string', }) - .version(global.__VERSION__) + .version(VERSION) .help() .parse(process.argv.slice(1)); @@ -95,11 +97,11 @@ process.env.CONFIG = JSON.stringify({ }); // possible reference to main app window -let win; +let win: BrowserWindow; let appReady = false; let pluginsCompiled = false; -let deeplinkURL = argv.url; -let filePath = argv.file; +let deeplinkURL: string = argv.url; +let filePath: string = argv.file; // tracking setInterval(() => { @@ -130,7 +132,7 @@ const gotTheLock = app.requestSingleInstanceLock(); if (!gotTheLock) { app.quit(); } else { - app.on('second-instance', (event, commandLine, workingDirectory) => { + app.on('second-instance', (_event, _commandLine, _workingDirectory) => { // Someone tried to run a second instance, we should focus our window. if (win) { if (win.isMinimized()) { @@ -195,7 +197,7 @@ app.on('ready', () => { }); }); -ipcMain.on('componentDidMount', event => { +ipcMain.on('componentDidMount', _event => { if (deeplinkURL) { win.webContents.send('flipper-protocol-handler', deeplinkURL); deeplinkURL = null; @@ -226,6 +228,8 @@ ipcMain.on( // Forwarding notification events to renderer process // https://electronjs.org/docs/api/notification#instance-events ['show', 'click', 'close', 'reply', 'action'].forEach(eventName => { + // TODO: refactor this to make typescript happy + // @ts-ignore n.on(eventName, (event, ...args) => { e.sender.send( 'notificationEvent', @@ -259,10 +263,10 @@ function tryCreateWindow() { minWidth: 800, minHeight: 600, center: true, - backgroundThrottling: false, titleBarStyle: 'hiddenInset', vibrancy: 'sidebar', webPreferences: { + backgroundThrottling: false, webSecurity: false, scrollBounce: true, experimentalFeatures: true, @@ -272,15 +276,15 @@ function tryCreateWindow() { }, }); win.once('ready-to-show', () => win.show()); - win.once('close', ({sender}) => { + win.once('close', () => { if (process.env.NODE_ENV === 'development') { // Removes as a default protocol for debug builds. Because even when the // production application is installed, and one tries to deeplink through // browser, it still looks for the debug one and tries to open electron app.removeAsDefaultProtocolClient('flipper'); } - const [x, y] = sender.getPosition(); - const [width, height] = sender.getSize(); + const [x, y] = win.getPosition(); + const [width, height] = win.getSize(); // save window position and size fs.writeFileSync( configPath, @@ -295,7 +299,11 @@ function tryCreateWindow() { }), ); }); - if (config.lastWindowPosition.x && config.lastWindowPosition.y) { + if ( + config.lastWindowPosition && + config.lastWindowPosition.x && + config.lastWindowPosition.y + ) { win.setPosition(config.lastWindowPosition.x, config.lastWindowPosition.y); } const entryUrl = diff --git a/static/setup.d.ts b/static/setup.d.ts deleted file mode 100644 index a435fe97c..000000000 --- a/static/setup.d.ts +++ /dev/null @@ -1,24 +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 - */ - -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}; diff --git a/static/setup.js b/static/setup.ts similarity index 66% rename from static/setup.js rename to static/setup.ts index 3fbdad29f..84d9eea94 100644 --- a/static/setup.js +++ b/static/setup.ts @@ -7,11 +7,26 @@ * @format */ -const path = require('path'); -const os = require('os'); -const fs = require('fs'); +import path from 'path'; +import os from 'os'; +import fs from 'fs'; -module.exports = function(argv) { +export type Config = { + pluginPaths?: string[]; + disabledPlugins?: string[]; + lastWindowPosition?: { + x: number; + y: number; + width: number; + height: number; + }; + updater?: boolean | undefined; + launcherMsg?: string | undefined; + updaterEnabled?: boolean; + launcherEnabled?: boolean; +}; + +export default function setup(argv: any) { // ensure .flipper folder and config exist const flipperDir = path.join(os.homedir(), '.flipper'); if (!fs.existsSync(flipperDir)) { @@ -19,16 +34,15 @@ module.exports = function(argv) { } const configPath = path.join(flipperDir, 'config.json'); - let config = { + let config: Config = { pluginPaths: [], disabledPlugins: [], - lastWindowPosition: {}, }; try { config = { ...config, - ...JSON.parse(fs.readFileSync(configPath)), + ...JSON.parse(fs.readFileSync(configPath).toString()), }; } catch (e) { // file not readable or not parsable, overwrite it with the new config @@ -46,4 +60,4 @@ module.exports = function(argv) { }; return {config, configPath, flipperDir}; -}; +}