From caf04e4e4acb91e66b68a207fdbedd497f584824 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Thu, 27 Feb 2020 05:28:02 -0800 Subject: [PATCH] Typescriptify the main process code (9/N) Summary: Converted scripts/build-release.js and scripts/build-headless.js to typescript Reviewed By: passy Differential Revision: D20066504 fbshipit-source-id: 25f336062361e1211b581f96979978a6bf4fe6d4 --- package.json | 22 ++++++---- .../{build-headless.js => build-headless.ts} | 31 +++++++------ .../{build-release.js => build-release.ts} | 43 +++++++++++-------- src/utils/icons.js | 1 - tsconfig.json | 3 +- types/line-replace.d.ts | 18 ++++++++ yarn.lock | 14 ++++++ 7 files changed, 88 insertions(+), 44 deletions(-) rename scripts/{build-headless.js => build-headless.ts} (85%) rename scripts/{build-release.js => build-release.ts} (82%) create mode 100644 types/line-replace.d.ts diff --git a/package.json b/package.json index 0f9fd12e1..e2db0855e 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "@types/react-redux": "^7.1.5", "@types/react-virtualized-auto-sizer": "^1.0.0", "@types/react-window": "^1.8.1", + "@types/recursive-readdir": "^2.2.0", "@types/redux-persist": "^4.3.1", "@types/requestidlecallback": "^0.3.1", "@types/rsocket-core": "^0.0.5", @@ -90,9 +91,10 @@ "@types/testing-library__react": "^9.1.2", "@types/tmp": "^0.1.0", "@types/uuid": "^3.4.5", - "@typescript-eslint/eslint-plugin": "^2.19.2", - "@typescript-eslint/parser": "^2.19.2", "@types/ws": "^7.2.0", + "@types/yazl": "^2.4.2", + "@typescript-eslint/eslint-plugin": "^2.19.2", + "@typescript-eslint/parser": "^2.19.2", "babel-code-frame": "^6.26.0", "babel-eslint": "^10.0.1", "electron": "7.1.2", @@ -214,18 +216,20 @@ "rm-modules": "rimraf node_modules static/node_modules", "rm-temp": "rimraf $TMPDIR/jest* $TMPDIR/react-native-packager*", "rm-bundle": "rimraf static/main.bundle.*", - "reset": "yarn rm-dist && yarn rm-temp && yarn cache clean && yarn rm-modules && yarn rm-bundle", - "start": "cross-env NODE_ENV=development ts-node --files scripts/start-dev-server.ts --inspect=9229", - "start:break": "cross-env NODE_ENV=development ts-node scripts/start-dev-server.ts --inspect-brk=9229", - "start:no-embedded-plugins": "cross-env NODE_ENV=development cross-env FLIPPER_NO_EMBEDDED_PLUGINS=true ts-node scripts/start-dev-server.ts", - "build": "yarn rm-dist && cross-env NODE_ENV=production node scripts/build-release.js $@", - "build-headless": "yarn rm-dist && mkdir dist && cross-env NODE_ENV=production node scripts/build-headless.js $@", + "reset": "yarn rm-dist && yarn rm-temp && yarn cache clean && yarn rm-bundle && yarn rm-modules", + "start": "cross-env NODE_ENV=development TS_NODE_FILES=true node --require ts-node/register scripts/start-dev-server.ts --inspect=9229", + "start:break": "cross-env NODE_ENV=development TS_NODE_FILES=true node --require ts-node/register scripts/start-dev-server.ts --inspect-brk=9229", + "start:no-embedded-plugins": "cross-env NODE_ENV=development cross-env FLIPPER_NO_EMBEDDED_PLUGINS=true TS_NODE_FILES=true node --require ts-node/register scripts/start-dev-server.ts", + "build": "yarn rm-dist && cross-env NODE_ENV=production TS_NODE_FILES=true node --require ts-node/register scripts/build-release.ts $@", + "build-headless": "yarn rm-dist && mkdir dist && cross-env NODE_ENV=production TS_NODE_FILES=true node --require ts-node/register scripts/build-headless.ts $@", "fix": "eslint . --fix --ext .js,.tsx", "test": "jest --testPathPattern=\"node\\.(js|tsx)$\" --no-cache", "test:debug": "node --inspect node_modules/.bin/jest --runInBand", "test-electron": "jest --testPathPattern=\"electron\\.(js|tsx)$\" --testEnvironment=@jest-runner/electron/environment --runner=@jest-runner/electron --no-cache", "test-with-device": "USE_ELECTRON_STUBS=1 jest --testPathPattern=\"device\\.(js|tsx)$\" --detectOpenHandles --no-cache", - "lint": "eslint . --ext .js,.ts,.tsx && flow check && tsc --noemit" + "tsc": "tsc --noemit", + "lint": "eslint . --ext .js,.ts,.tsx && flow check && yarn tsc", + "everything": "yarn reset && yarn install && yarn lint && yarn test && yarn test-electron && yarn build --mac --win --linux && yarn build-headless --mac --linux && yarn start" }, "optionalDependencies": { "7zip-bin-mac": "^1.0.1" diff --git a/scripts/build-headless.js b/scripts/build-headless.ts similarity index 85% rename from scripts/build-headless.js rename to scripts/build-headless.ts index 438d40bc8..9c3347e38 100644 --- a/scripts/build-headless.js +++ b/scripts/build-headless.ts @@ -7,10 +7,10 @@ * @format */ -const fs = require('fs'); -const path = require('path'); -const lineReplace = require('line-replace'); -const yazl = require('yazl'); +import fs from 'fs'; +import path from 'path'; +import lineReplace from 'line-replace'; +import yazl from 'yazl'; const {exec: createBinary} = require('pkg'); const { buildFolder, @@ -22,10 +22,14 @@ const { const PLUGINS_FOLDER_NAME = 'plugins'; -function preludeBundle(dir, versionNumber, buildRevision) { +function preludeBundle( + dir: string, + versionNumber: string, + buildRevision: string, +) { const revisionStr = buildRevision == null ? '' : `global.__REVISION__="${buildRevision}";`; - return new Promise((resolve, reject) => + return new Promise(resolve => lineReplace({ file: path.join(dir, 'bundle.js'), line: 1, @@ -36,8 +40,8 @@ function preludeBundle(dir, versionNumber, buildRevision) { ); } -async function createZip(buildDir, distDir, targets) { - return new Promise((resolve, reject) => { +async function createZip(buildDir: string, distDir: string, targets: string[]) { + return new Promise(resolve => { const zip = new yazl.ZipFile(); // add binaries for each target @@ -64,9 +68,8 @@ async function createZip(buildDir, distDir, targets) { } (async () => { - const targets = {}; - let platformPostfix; - + const targets: {mac?: string; linux?: string; win?: string} = {}; + let platformPostfix: string = ''; if (process.argv.indexOf('--mac') > -1) { targets.mac = 'node10-macos-x64'; platformPostfix = '-macos'; @@ -79,13 +82,13 @@ async function createZip(buildDir, distDir, targets) { targets.win = 'node10-win-x64'; platformPostfix = '-win'; } - if (targets.length === 0) { + const length = Object.keys(targets).length; + if (length === 0) { throw new Error('No targets specified. eg. --mac, --win, or --linux'); - } else if (Object.keys(targets).length > 1) { + } else if (length > 1) { // platformPostfix is automatically added by pkg platformPostfix = ''; } - // Compiling all plugins takes a long time. Use this flag for quicker // developement iteration by not including any plugins. const skipPlugins = process.argv.indexOf('--no-plugins') > -1; diff --git a/scripts/build-release.js b/scripts/build-release.ts similarity index 82% rename from scripts/build-release.js rename to scripts/build-release.ts index 040674d07..6b2b5299a 100755 --- a/scripts/build-release.js +++ b/scripts/build-release.ts @@ -7,11 +7,10 @@ * @format */ -const path = require('path'); -const fs = require('fs-extra'); -const builder = require('electron-builder'); -const Platform = builder.Platform; -const cp = require('promisify-child-process'); +import path from 'path'; +import fs from 'fs-extra'; +import {Platform, Arch, ElectronDownloadOptions, build} from 'electron-builder'; +import {spawn} from 'promisify-child-process'; const { buildFolder, compile, @@ -21,14 +20,14 @@ const { getVersionNumber, genMercurialRevision, } = require('./build-utils.js'); -const fetch = require('node-fetch'); +import fetch from 'node-fetch'; const { ICONS, buildLocalIconPath, getIconURL, } = require('../src/utils/icons.js'); -function generateManifest(versionNumber) { +function generateManifest(versionNumber: string) { const filePath = path.join(__dirname, '..', 'dist'); if (!fs.existsSync(filePath)) { fs.mkdirSync(filePath); @@ -42,7 +41,11 @@ function generateManifest(versionNumber) { ); } -function modifyPackageManifest(buildFolder, versionNumber, hgRevision) { +function modifyPackageManifest( + buildFolder: string, + versionNumber: string, + hgRevision: string, +) { // eslint-disable-next-line no-console console.log('Creating package.json manifest'); const manifest = require('../package.json'); @@ -63,14 +66,14 @@ function modifyPackageManifest(buildFolder, versionNumber, hgRevision) { ); } -async function buildDist(buildFolder) { - const targetsRaw = []; - const postBuildCallbacks = []; +async function buildDist(buildFolder: string) { + const targetsRaw: Map>[] = []; + const postBuildCallbacks: (() => void)[] = []; if (process.argv.indexOf('--mac') > -1) { targetsRaw.push(Platform.MAC.createTarget(['dir'])); postBuildCallbacks.push(() => - cp.spawn('zip', ['-qyr9', '../Flipper-mac.zip', 'Flipper.app'], { + spawn('zip', ['-qyr9', '../Flipper-mac.zip', 'Flipper.app'], { cwd: path.join(__dirname, '..', 'dist', 'mac'), encoding: 'utf-8', }), @@ -87,19 +90,19 @@ async function buildDist(buildFolder) { } // merge all target maps into a single map - let targetsMerged = []; + let targetsMerged: [Platform, Map][] = []; for (const target of targetsRaw) { targetsMerged = targetsMerged.concat(Array.from(target)); } const targets = new Map(targetsMerged); - const electronDownload = {}; + const electronDownloadOptions: ElectronDownloadOptions = {}; if (process.env.electron_config_cache) { - electronDownload.cache = process.env.electron_config_cache; + electronDownloadOptions.cache = process.env.electron_config_cache; } try { - await builder.build({ + await build({ publish: 'never', config: { appId: `com.facebook.sonar`, @@ -107,7 +110,7 @@ async function buildDist(buildFolder) { buildResources: path.join(__dirname, '..', 'static'), output: path.join(__dirname, '..', 'dist'), }, - electronDownload, + electronDownload: electronDownloadOptions, npmRebuild: false, }, projectDir: buildFolder, @@ -119,17 +122,19 @@ async function buildDist(buildFolder) { } } -function copyStaticFolder(buildFolder) { +function copyStaticFolder(buildFolder: string) { fs.copySync(path.join(__dirname, '..', 'static'), buildFolder, { dereference: true, }); } -function downloadIcons(buildFolder) { +function downloadIcons(buildFolder: string) { const iconURLs = Object.entries(ICONS).reduce((acc, [name, sizes]) => { acc.push( // get icons in @1x and @2x + // @ts-ignore ...sizes.map(size => ({name, size, density: 1})), + // @ts-ignore ...sizes.map(size => ({name, size, density: 2})), ); return acc; diff --git a/src/utils/icons.js b/src/utils/icons.js index 5f02f0f72..10bb45931 100644 --- a/src/utils/icons.js +++ b/src/utils/icons.js @@ -7,7 +7,6 @@ * @format */ -/* This file needs to be plain JS to be imported by scripts/build-release.js */ /* eslint-disable import/no-commonjs */ const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32]; diff --git a/tsconfig.json b/tsconfig.json index 9801cea36..cb8fd0f76 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "jsx": "react", "moduleResolution": "node", "baseUrl": ".", - "downlevelIteration": true, + "downlevelIteration": true, + "skipLibCheck": true, "paths": { "flipper": ["./src/index.tsx"], "live-plugin-manager": ["./types/live-plugin-manager.d.tsx"] diff --git a/types/line-replace.d.ts b/types/line-replace.d.ts new file mode 100644 index 000000000..7f5125851 --- /dev/null +++ b/types/line-replace.d.ts @@ -0,0 +1,18 @@ +/** + * 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 + */ + +declare module 'line-replace' { + export default function(args: { + file: string; + line: number; + text: string; + addNewLine: boolean; + callback: (args: {file: string; line: number; replacedText: string; text: string}) => void; + }): void; +} diff --git a/yarn.lock b/yarn.lock index 16d45d4e7..93e4d732a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1589,6 +1589,13 @@ "@types/prop-types" "*" csstype "^2.2.0" +"@types/recursive-readdir@^2.2.0": + version "2.2.0" + resolved "https://registry.yarnpkg.com/@types/recursive-readdir/-/recursive-readdir-2.2.0.tgz#b39cd5474fd58ea727fe434d5c68b7a20ba9121c" + integrity sha512-HGk753KRu2N4mWduovY4BLjYq4jTOL29gV2OfGdGxHcPSWGFkC5RRIdk+VTs5XmYd7MVAD+JwKrcb5+5Y7FOCg== + dependencies: + "@types/node" "*" + "@types/redux-devtools-extension@^2.13.2": version "2.13.2" resolved "https://registry.yarnpkg.com/@types/redux-devtools-extension/-/redux-devtools-extension-2.13.2.tgz#b2e09a8c163b2b0f5072e6a5ac41474000df2111" @@ -1759,6 +1766,13 @@ dependencies: "@types/yargs-parser" "*" +"@types/yazl@^2.4.2": + version "2.4.2" + resolved "https://registry.yarnpkg.com/@types/yazl/-/yazl-2.4.2.tgz#d5f8a4752261badbf1a36e8b49e042dc18ec84bc" + integrity sha512-T+9JH8O2guEjXNxqmybzQ92mJUh2oCwDDMSSimZSe1P+pceZiFROZLYmcbqkzV5EUwz6VwcKXCO2S2yUpra6XQ== + dependencies: + "@types/node" "*" + "@typescript-eslint/eslint-plugin@^2.19.2": version "2.19.2" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.19.2.tgz#e279aaae5d5c1f2547b4cff99204e1250bc7a058"