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

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

View File

@@ -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<Platform, Map<Arch, string[]>>[] = [];
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<Arch, string[]>][] = [];
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;