Typecscriptify the main process code (10/N)
Summary: Converted scripts/build-utils.js to typescript Reviewed By: passy Differential Revision: D20067220 fbshipit-source-id: 29c1a4aed11b9d682290daf57db5507736ad8c69
This commit is contained in:
committed by
Facebook Github Bot
parent
caf04e4e4a
commit
2d551f6b4a
@@ -12,20 +12,20 @@ import path from 'path';
|
||||
import lineReplace from 'line-replace';
|
||||
import yazl from 'yazl';
|
||||
const {exec: createBinary} = require('pkg');
|
||||
const {
|
||||
import {
|
||||
buildFolder,
|
||||
compile,
|
||||
compileDefaultPlugins,
|
||||
getVersionNumber,
|
||||
genMercurialRevision,
|
||||
} = require('./build-utils.js');
|
||||
} from './build-utils';
|
||||
|
||||
const PLUGINS_FOLDER_NAME = 'plugins';
|
||||
|
||||
function preludeBundle(
|
||||
dir: string,
|
||||
versionNumber: string,
|
||||
buildRevision: string,
|
||||
buildRevision: string | null,
|
||||
) {
|
||||
const revisionStr =
|
||||
buildRevision == null ? '' : `global.__REVISION__="${buildRevision}";`;
|
||||
|
||||
@@ -11,7 +11,7 @@ import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {Platform, Arch, ElectronDownloadOptions, build} from 'electron-builder';
|
||||
import {spawn} from 'promisify-child-process';
|
||||
const {
|
||||
import {
|
||||
buildFolder,
|
||||
compile,
|
||||
compileMain,
|
||||
@@ -19,7 +19,7 @@ const {
|
||||
compileDefaultPlugins,
|
||||
getVersionNumber,
|
||||
genMercurialRevision,
|
||||
} = require('./build-utils.js');
|
||||
} from './build-utils';
|
||||
import fetch from 'node-fetch';
|
||||
const {
|
||||
ICONS,
|
||||
@@ -44,7 +44,7 @@ function generateManifest(versionNumber: string) {
|
||||
function modifyPackageManifest(
|
||||
buildFolder: string,
|
||||
versionNumber: string,
|
||||
hgRevision: string,
|
||||
hgRevision: string | null,
|
||||
) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('Creating package.json manifest');
|
||||
|
||||
@@ -9,29 +9,34 @@
|
||||
|
||||
const Metro = require('../static/node_modules/metro');
|
||||
const compilePlugins = require('../static/compilePlugins');
|
||||
const util = require('util');
|
||||
const tmp = require('tmp');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const cp = require('promisify-child-process');
|
||||
const recursiveReaddir = require('recursive-readdir');
|
||||
import util from 'util';
|
||||
import tmp from 'tmp';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {spawn} from 'promisify-child-process';
|
||||
import recursiveReaddir from 'recursive-readdir';
|
||||
|
||||
function mostRecentlyChanged(dir, ignores) {
|
||||
return util
|
||||
.promisify(recursiveReaddir)(dir, ignores)
|
||||
.then(files =>
|
||||
files
|
||||
async function mostRecentlyChanged(
|
||||
dir: string,
|
||||
ignores: string[],
|
||||
): Promise<Date> {
|
||||
const files = await util.promisify<string, string[], string[]>(
|
||||
recursiveReaddir,
|
||||
)(dir, ignores);
|
||||
return files
|
||||
.map(f => fs.lstatSync(f).ctime)
|
||||
.reduce((a, b) => (a > b ? a : b), new Date(0)),
|
||||
);
|
||||
.reduce((a, b) => (a > b ? a : b), new Date(0));
|
||||
}
|
||||
|
||||
function die(err) {
|
||||
export function die(err: Error) {
|
||||
console.error(err.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function compileDefaultPlugins(defaultPluginDir, skipAll = false) {
|
||||
export function compileDefaultPlugins(
|
||||
defaultPluginDir: string,
|
||||
skipAll: boolean = false,
|
||||
) {
|
||||
return compilePlugins(
|
||||
null,
|
||||
skipAll
|
||||
@@ -43,7 +48,7 @@ function compileDefaultPlugins(defaultPluginDir, skipAll = false) {
|
||||
defaultPluginDir,
|
||||
{force: true, failSilently: false, recompileOnChanges: false},
|
||||
)
|
||||
.then(defaultPlugins =>
|
||||
.then((defaultPlugins: any[]) =>
|
||||
fs.writeFileSync(
|
||||
path.join(defaultPluginDir, 'index.json'),
|
||||
JSON.stringify(
|
||||
@@ -57,7 +62,7 @@ function compileDefaultPlugins(defaultPluginDir, skipAll = false) {
|
||||
.catch(die);
|
||||
}
|
||||
|
||||
function compile(buildFolder, entry) {
|
||||
export function compile(buildFolder: string, entry: string) {
|
||||
console.log(`⚙️ Compiling renderer bundle...`);
|
||||
const projectRoots = path.join(__dirname, '..');
|
||||
return Metro.runBuild(
|
||||
@@ -92,7 +97,7 @@ function compile(buildFolder, entry) {
|
||||
.catch(die);
|
||||
}
|
||||
|
||||
async function compileMain({dev}) {
|
||||
export async function compileMain({dev}: {dev: boolean}) {
|
||||
const staticDir = path.resolve(__dirname, '..', 'static');
|
||||
const out = path.join(staticDir, 'main.bundle.js');
|
||||
// check if main needs to be compiled
|
||||
@@ -138,10 +143,10 @@ async function compileMain({dev}) {
|
||||
die(err);
|
||||
}
|
||||
}
|
||||
function buildFolder() {
|
||||
export function buildFolder(): Promise<string> {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('Creating build directory');
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
tmp.dir({prefix: 'flipper-build-'}, (err, buildFolder) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
@@ -149,28 +154,30 @@ function buildFolder() {
|
||||
resolve(buildFolder);
|
||||
}
|
||||
});
|
||||
}).catch(die);
|
||||
}).catch(e => {
|
||||
die(e);
|
||||
return '';
|
||||
});
|
||||
}
|
||||
function getVersionNumber() {
|
||||
export function getVersionNumber() {
|
||||
let {version} = require('../package.json');
|
||||
const buildNumber = process.argv.join(' ').match(/--version=(\d+)/);
|
||||
if (buildNumber && buildNumber.length > 0) {
|
||||
version = [...version.split('.').slice(0, 2), buildNumber[1]].join('.');
|
||||
}
|
||||
return version;
|
||||
} // Asynchronously determine current mercurial revision as string or `null` in case of any error.
|
||||
function genMercurialRevision() {
|
||||
return cp
|
||||
.spawn('hg', ['log', '-r', '.', '-T', '{node}'], {encoding: 'utf8'})
|
||||
.catch(err => null)
|
||||
.then(res => (res && res.stdout) || null);
|
||||
}
|
||||
module.exports = {
|
||||
buildFolder,
|
||||
compile,
|
||||
compileMain,
|
||||
die,
|
||||
compileDefaultPlugins,
|
||||
getVersionNumber,
|
||||
genMercurialRevision,
|
||||
};
|
||||
|
||||
// Asynchronously determine current mercurial revision as string or `null` in case of any error.
|
||||
export function genMercurialRevision(): Promise<string | null> {
|
||||
return spawn('hg', ['log', '-r', '.', '-T', '{node}'], {encoding: 'utf8'})
|
||||
.then(
|
||||
res =>
|
||||
(res &&
|
||||
(typeof res.stdout === 'string'
|
||||
? res.stdout
|
||||
: res.stdout?.toString())) ||
|
||||
null,
|
||||
)
|
||||
.catch(() => null);
|
||||
}
|
||||
@@ -18,8 +18,8 @@ import chalk from 'chalk';
|
||||
import http from 'http';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import {compileMain} from './build-utils';
|
||||
const Watchman = require('../static/watchman');
|
||||
const {compileMain} = require('./build-utils');
|
||||
const Metro = require('../static/node_modules/metro');
|
||||
const MetroResolver = require('../static/node_modules/metro-resolver');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user