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
This commit is contained in:
Anton Nikolaev
2020-02-27 05:28:02 -08:00
committed by Facebook Github Bot
parent 09d0b4c841
commit caf04e4e4a
7 changed files with 88 additions and 44 deletions

View File

@@ -83,6 +83,7 @@
"@types/react-redux": "^7.1.5", "@types/react-redux": "^7.1.5",
"@types/react-virtualized-auto-sizer": "^1.0.0", "@types/react-virtualized-auto-sizer": "^1.0.0",
"@types/react-window": "^1.8.1", "@types/react-window": "^1.8.1",
"@types/recursive-readdir": "^2.2.0",
"@types/redux-persist": "^4.3.1", "@types/redux-persist": "^4.3.1",
"@types/requestidlecallback": "^0.3.1", "@types/requestidlecallback": "^0.3.1",
"@types/rsocket-core": "^0.0.5", "@types/rsocket-core": "^0.0.5",
@@ -90,9 +91,10 @@
"@types/testing-library__react": "^9.1.2", "@types/testing-library__react": "^9.1.2",
"@types/tmp": "^0.1.0", "@types/tmp": "^0.1.0",
"@types/uuid": "^3.4.5", "@types/uuid": "^3.4.5",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"@types/ws": "^7.2.0", "@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-code-frame": "^6.26.0",
"babel-eslint": "^10.0.1", "babel-eslint": "^10.0.1",
"electron": "7.1.2", "electron": "7.1.2",
@@ -214,18 +216,20 @@
"rm-modules": "rimraf node_modules static/node_modules", "rm-modules": "rimraf node_modules static/node_modules",
"rm-temp": "rimraf $TMPDIR/jest* $TMPDIR/react-native-packager*", "rm-temp": "rimraf $TMPDIR/jest* $TMPDIR/react-native-packager*",
"rm-bundle": "rimraf static/main.bundle.*", "rm-bundle": "rimraf static/main.bundle.*",
"reset": "yarn rm-dist && yarn rm-temp && yarn cache clean && yarn rm-modules && yarn rm-bundle", "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 scripts/start-dev-server.ts --inspect=9229", "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 scripts/start-dev-server.ts --inspect-brk=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 scripts/start-dev-server.ts", "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 node scripts/build-release.js $@", "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 node scripts/build-headless.js $@", "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", "fix": "eslint . --fix --ext .js,.tsx",
"test": "jest --testPathPattern=\"node\\.(js|tsx)$\" --no-cache", "test": "jest --testPathPattern=\"node\\.(js|tsx)$\" --no-cache",
"test:debug": "node --inspect node_modules/.bin/jest --runInBand", "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-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", "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": { "optionalDependencies": {
"7zip-bin-mac": "^1.0.1" "7zip-bin-mac": "^1.0.1"

View File

@@ -7,10 +7,10 @@
* @format * @format
*/ */
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const lineReplace = require('line-replace'); import lineReplace from 'line-replace';
const yazl = require('yazl'); import yazl from 'yazl';
const {exec: createBinary} = require('pkg'); const {exec: createBinary} = require('pkg');
const { const {
buildFolder, buildFolder,
@@ -22,10 +22,14 @@ const {
const PLUGINS_FOLDER_NAME = 'plugins'; const PLUGINS_FOLDER_NAME = 'plugins';
function preludeBundle(dir, versionNumber, buildRevision) { function preludeBundle(
dir: string,
versionNumber: string,
buildRevision: string,
) {
const revisionStr = const revisionStr =
buildRevision == null ? '' : `global.__REVISION__="${buildRevision}";`; buildRevision == null ? '' : `global.__REVISION__="${buildRevision}";`;
return new Promise((resolve, reject) => return new Promise(resolve =>
lineReplace({ lineReplace({
file: path.join(dir, 'bundle.js'), file: path.join(dir, 'bundle.js'),
line: 1, line: 1,
@@ -36,8 +40,8 @@ function preludeBundle(dir, versionNumber, buildRevision) {
); );
} }
async function createZip(buildDir, distDir, targets) { async function createZip(buildDir: string, distDir: string, targets: string[]) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
const zip = new yazl.ZipFile(); const zip = new yazl.ZipFile();
// add binaries for each target // add binaries for each target
@@ -64,9 +68,8 @@ async function createZip(buildDir, distDir, targets) {
} }
(async () => { (async () => {
const targets = {}; const targets: {mac?: string; linux?: string; win?: string} = {};
let platformPostfix; let platformPostfix: string = '';
if (process.argv.indexOf('--mac') > -1) { if (process.argv.indexOf('--mac') > -1) {
targets.mac = 'node10-macos-x64'; targets.mac = 'node10-macos-x64';
platformPostfix = '-macos'; platformPostfix = '-macos';
@@ -79,13 +82,13 @@ async function createZip(buildDir, distDir, targets) {
targets.win = 'node10-win-x64'; targets.win = 'node10-win-x64';
platformPostfix = '-win'; 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'); 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 is automatically added by pkg
platformPostfix = ''; platformPostfix = '';
} }
// Compiling all plugins takes a long time. Use this flag for quicker // Compiling all plugins takes a long time. Use this flag for quicker
// developement iteration by not including any plugins. // developement iteration by not including any plugins.
const skipPlugins = process.argv.indexOf('--no-plugins') > -1; const skipPlugins = process.argv.indexOf('--no-plugins') > -1;

View File

@@ -7,11 +7,10 @@
* @format * @format
*/ */
const path = require('path'); import path from 'path';
const fs = require('fs-extra'); import fs from 'fs-extra';
const builder = require('electron-builder'); import {Platform, Arch, ElectronDownloadOptions, build} from 'electron-builder';
const Platform = builder.Platform; import {spawn} from 'promisify-child-process';
const cp = require('promisify-child-process');
const { const {
buildFolder, buildFolder,
compile, compile,
@@ -21,14 +20,14 @@ const {
getVersionNumber, getVersionNumber,
genMercurialRevision, genMercurialRevision,
} = require('./build-utils.js'); } = require('./build-utils.js');
const fetch = require('node-fetch'); import fetch from 'node-fetch';
const { const {
ICONS, ICONS,
buildLocalIconPath, buildLocalIconPath,
getIconURL, getIconURL,
} = require('../src/utils/icons.js'); } = require('../src/utils/icons.js');
function generateManifest(versionNumber) { function generateManifest(versionNumber: string) {
const filePath = path.join(__dirname, '..', 'dist'); const filePath = path.join(__dirname, '..', 'dist');
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
fs.mkdirSync(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 // eslint-disable-next-line no-console
console.log('Creating package.json manifest'); console.log('Creating package.json manifest');
const manifest = require('../package.json'); const manifest = require('../package.json');
@@ -63,14 +66,14 @@ function modifyPackageManifest(buildFolder, versionNumber, hgRevision) {
); );
} }
async function buildDist(buildFolder) { async function buildDist(buildFolder: string) {
const targetsRaw = []; const targetsRaw: Map<Platform, Map<Arch, string[]>>[] = [];
const postBuildCallbacks = []; const postBuildCallbacks: (() => void)[] = [];
if (process.argv.indexOf('--mac') > -1) { if (process.argv.indexOf('--mac') > -1) {
targetsRaw.push(Platform.MAC.createTarget(['dir'])); targetsRaw.push(Platform.MAC.createTarget(['dir']));
postBuildCallbacks.push(() => 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'), cwd: path.join(__dirname, '..', 'dist', 'mac'),
encoding: 'utf-8', encoding: 'utf-8',
}), }),
@@ -87,19 +90,19 @@ async function buildDist(buildFolder) {
} }
// merge all target maps into a single map // merge all target maps into a single map
let targetsMerged = []; let targetsMerged: [Platform, Map<Arch, string[]>][] = [];
for (const target of targetsRaw) { for (const target of targetsRaw) {
targetsMerged = targetsMerged.concat(Array.from(target)); targetsMerged = targetsMerged.concat(Array.from(target));
} }
const targets = new Map(targetsMerged); const targets = new Map(targetsMerged);
const electronDownload = {}; const electronDownloadOptions: ElectronDownloadOptions = {};
if (process.env.electron_config_cache) { if (process.env.electron_config_cache) {
electronDownload.cache = process.env.electron_config_cache; electronDownloadOptions.cache = process.env.electron_config_cache;
} }
try { try {
await builder.build({ await build({
publish: 'never', publish: 'never',
config: { config: {
appId: `com.facebook.sonar`, appId: `com.facebook.sonar`,
@@ -107,7 +110,7 @@ async function buildDist(buildFolder) {
buildResources: path.join(__dirname, '..', 'static'), buildResources: path.join(__dirname, '..', 'static'),
output: path.join(__dirname, '..', 'dist'), output: path.join(__dirname, '..', 'dist'),
}, },
electronDownload, electronDownload: electronDownloadOptions,
npmRebuild: false, npmRebuild: false,
}, },
projectDir: buildFolder, projectDir: buildFolder,
@@ -119,17 +122,19 @@ async function buildDist(buildFolder) {
} }
} }
function copyStaticFolder(buildFolder) { function copyStaticFolder(buildFolder: string) {
fs.copySync(path.join(__dirname, '..', 'static'), buildFolder, { fs.copySync(path.join(__dirname, '..', 'static'), buildFolder, {
dereference: true, dereference: true,
}); });
} }
function downloadIcons(buildFolder) { function downloadIcons(buildFolder: string) {
const iconURLs = Object.entries(ICONS).reduce((acc, [name, sizes]) => { const iconURLs = Object.entries(ICONS).reduce((acc, [name, sizes]) => {
acc.push( acc.push(
// get icons in @1x and @2x // get icons in @1x and @2x
// @ts-ignore
...sizes.map(size => ({name, size, density: 1})), ...sizes.map(size => ({name, size, density: 1})),
// @ts-ignore
...sizes.map(size => ({name, size, density: 2})), ...sizes.map(size => ({name, size, density: 2})),
); );
return acc; return acc;

View File

@@ -7,7 +7,6 @@
* @format * @format
*/ */
/* This file needs to be plain JS to be imported by scripts/build-release.js */
/* eslint-disable import/no-commonjs */ /* eslint-disable import/no-commonjs */
const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32]; const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32];

View File

@@ -10,7 +10,8 @@
"jsx": "react", "jsx": "react",
"moduleResolution": "node", "moduleResolution": "node",
"baseUrl": ".", "baseUrl": ".",
"downlevelIteration": true, "downlevelIteration": true,
"skipLibCheck": true,
"paths": { "paths": {
"flipper": ["./src/index.tsx"], "flipper": ["./src/index.tsx"],
"live-plugin-manager": ["./types/live-plugin-manager.d.tsx"] "live-plugin-manager": ["./types/live-plugin-manager.d.tsx"]

18
types/line-replace.d.ts vendored Normal file
View File

@@ -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;
}

View File

@@ -1589,6 +1589,13 @@
"@types/prop-types" "*" "@types/prop-types" "*"
csstype "^2.2.0" 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": "@types/redux-devtools-extension@^2.13.2":
version "2.13.2" version "2.13.2"
resolved "https://registry.yarnpkg.com/@types/redux-devtools-extension/-/redux-devtools-extension-2.13.2.tgz#b2e09a8c163b2b0f5072e6a5ac41474000df2111" resolved "https://registry.yarnpkg.com/@types/redux-devtools-extension/-/redux-devtools-extension-2.13.2.tgz#b2e09a8c163b2b0f5072e6a5ac41474000df2111"
@@ -1759,6 +1766,13 @@
dependencies: dependencies:
"@types/yargs-parser" "*" "@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": "@typescript-eslint/eslint-plugin@^2.19.2":
version "2.19.2" version "2.19.2"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.19.2.tgz#e279aaae5d5c1f2547b4cff99204e1250bc7a058" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.19.2.tgz#e279aaae5d5c1f2547b4cff99204e1250bc7a058"