Typescriptify the main process code (11/N)
Summary: Converted static/compilePlugins.js to typescript Reviewed By: passy Differential Revision: D20070227 fbshipit-source-id: ba360b944440babedef94af0d0bc577711165ff6
This commit is contained in:
committed by
Facebook Github Bot
parent
2d551f6b4a
commit
aff433c8ad
@@ -8,7 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const Metro = require('../static/node_modules/metro');
|
const Metro = require('../static/node_modules/metro');
|
||||||
const compilePlugins = require('../static/compilePlugins');
|
import compilePlugins from '../static/compilePlugins';
|
||||||
import util from 'util';
|
import util from 'util';
|
||||||
import tmp from 'tmp';
|
import tmp from 'tmp';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
@@ -48,7 +48,7 @@ export function compileDefaultPlugins(
|
|||||||
defaultPluginDir,
|
defaultPluginDir,
|
||||||
{force: true, failSilently: false, recompileOnChanges: false},
|
{force: true, failSilently: false, recompileOnChanges: false},
|
||||||
)
|
)
|
||||||
.then((defaultPlugins: any[]) =>
|
.then(defaultPlugins =>
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join(defaultPluginDir, 'index.json'),
|
path.join(defaultPluginDir, 'index.json'),
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
|
|||||||
@@ -8,28 +8,54 @@
|
|||||||
* @flow strict-local
|
* @flow strict-local
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const path = require('path');
|
import path from 'path';
|
||||||
const fs = require('fs');
|
import fs from 'fs';
|
||||||
const Metro = require('metro');
|
const Metro = require('metro');
|
||||||
const util = require('util');
|
import util from 'util';
|
||||||
const recursiveReaddir = require('recursive-readdir');
|
import recursiveReaddir from 'recursive-readdir';
|
||||||
const expandTilde = require('expand-tilde');
|
import expandTilde from 'expand-tilde';
|
||||||
const pMap = require('p-map');
|
import pMap from 'p-map';
|
||||||
const HOME_DIR = require('os').homedir();
|
import {homedir} from 'os';
|
||||||
const Watchman = require('./watchman');
|
const Watchman = require('./watchman');
|
||||||
|
|
||||||
const DEFAULT_COMPILE_OPTIONS = {
|
const HOME_DIR = homedir();
|
||||||
|
|
||||||
|
const DEFAULT_COMPILE_OPTIONS: CompileOptions = {
|
||||||
force: false,
|
force: false,
|
||||||
failSilently: true,
|
failSilently: true,
|
||||||
recompileOnChanges: true,
|
recompileOnChanges: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = async (
|
export type CompileOptions = {
|
||||||
reloadCallback,
|
force: boolean;
|
||||||
pluginPaths,
|
failSilently: boolean;
|
||||||
pluginCache,
|
recompileOnChanges: boolean;
|
||||||
options = DEFAULT_COMPILE_OPTIONS,
|
};
|
||||||
) => {
|
|
||||||
|
type DynamicCompileOptions = CompileOptions & {force: boolean};
|
||||||
|
|
||||||
|
export type PluginManifest = {
|
||||||
|
version: string;
|
||||||
|
name: string;
|
||||||
|
main?: string;
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PluginInfo = {
|
||||||
|
rootDir: string;
|
||||||
|
name: string;
|
||||||
|
entry: string;
|
||||||
|
manifest: PluginManifest;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CompiledPluginInfo = PluginManifest & {out: string};
|
||||||
|
|
||||||
|
export default async function(
|
||||||
|
reloadCallback: (() => void) | null,
|
||||||
|
pluginPaths: string[],
|
||||||
|
pluginCache: string,
|
||||||
|
options: CompileOptions = DEFAULT_COMPILE_OPTIONS,
|
||||||
|
) {
|
||||||
options = Object.assign({}, DEFAULT_COMPILE_OPTIONS, options);
|
options = Object.assign({}, DEFAULT_COMPILE_OPTIONS, options);
|
||||||
const plugins = pluginEntryPoints(pluginPaths);
|
const plugins = pluginEntryPoints(pluginPaths);
|
||||||
if (!fs.existsSync(pluginCache)) {
|
if (!fs.existsSync(pluginCache)) {
|
||||||
@@ -41,20 +67,27 @@ module.exports = async (
|
|||||||
const compilations = pMap(
|
const compilations = pMap(
|
||||||
Object.values(plugins),
|
Object.values(plugins),
|
||||||
plugin => {
|
plugin => {
|
||||||
const dynamicOptions = Object.assign(options, {force: false});
|
const dynamicOptions: DynamicCompileOptions = Object.assign(options, {
|
||||||
|
force: false,
|
||||||
|
});
|
||||||
return compilePlugin(plugin, pluginCache, dynamicOptions);
|
return compilePlugin(plugin, pluginCache, dynamicOptions);
|
||||||
},
|
},
|
||||||
{concurrency: 4},
|
{concurrency: 4},
|
||||||
);
|
);
|
||||||
|
|
||||||
const dynamicPlugins = (await compilations).filter(c => c != null);
|
const dynamicPlugins = (await compilations).filter(
|
||||||
|
c => c !== null,
|
||||||
|
) as CompiledPluginInfo[];
|
||||||
console.log('✅ Compiled all plugins.');
|
console.log('✅ Compiled all plugins.');
|
||||||
return dynamicPlugins;
|
return dynamicPlugins;
|
||||||
};
|
}
|
||||||
|
|
||||||
async function startWatchingPluginsUsingWatchman(plugins, onPluginChanged) {
|
async function startWatchingPluginsUsingWatchman(
|
||||||
|
plugins: PluginInfo[],
|
||||||
|
onPluginChanged: (plugin: PluginInfo) => void,
|
||||||
|
) {
|
||||||
// Initializing a watchman for each folder containing plugins
|
// Initializing a watchman for each folder containing plugins
|
||||||
const watchmanRootMap = {};
|
const watchmanRootMap: {[key: string]: any} = {};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
plugins.map(async plugin => {
|
plugins.map(async plugin => {
|
||||||
const watchmanRoot = path.resolve(plugin.rootDir, '..');
|
const watchmanRoot = path.resolve(plugin.rootDir, '..');
|
||||||
@@ -81,24 +114,26 @@ async function startWatchingPluginsUsingWatchman(plugins, onPluginChanged) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function startWatchChanges(
|
async function startWatchChanges(
|
||||||
plugins,
|
plugins: {[key: string]: PluginInfo},
|
||||||
reloadCallback,
|
reloadCallback: (() => void) | null,
|
||||||
pluginCache,
|
pluginCache: string,
|
||||||
options = DEFAULT_COMPILE_OPTIONS,
|
options: CompileOptions = DEFAULT_COMPILE_OPTIONS,
|
||||||
) {
|
) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('🕵️ Watching for plugin changes');
|
console.log('🕵️ Watching for plugin changes');
|
||||||
|
|
||||||
const delayedCompilation = {};
|
const delayedCompilation: {[key: string]: NodeJS.Timeout | null} = {};
|
||||||
const kCompilationDelayMillis = 1000;
|
const kCompilationDelayMillis = 1000;
|
||||||
const onPluginChanged = plugin => {
|
const onPluginChanged = (plugin: PluginInfo) => {
|
||||||
if (!delayedCompilation[plugin.name]) {
|
if (!delayedCompilation[plugin.name]) {
|
||||||
delayedCompilation[plugin.name] = setTimeout(() => {
|
delayedCompilation[plugin.name] = setTimeout(() => {
|
||||||
delayedCompilation[plugin.name] = null;
|
delayedCompilation[plugin.name] = null;
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`🕵️ Detected changes in ${plugin.name}`);
|
console.log(`🕵️ Detected changes in ${plugin.name}`);
|
||||||
const watchOptions = Object.assign(options, {force: true});
|
const watchOptions = Object.assign(options, {force: true});
|
||||||
compilePlugin(plugin, pluginCache, watchOptions).then(reloadCallback);
|
compilePlugin(plugin, pluginCache, watchOptions).then(
|
||||||
|
reloadCallback ?? (() => {}),
|
||||||
|
);
|
||||||
}, kCompilationDelayMillis);
|
}, kCompilationDelayMillis);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -117,7 +152,7 @@ async function startWatchChanges(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function hash(string) {
|
function hash(string: string) {
|
||||||
let hash = 0;
|
let hash = 0;
|
||||||
if (string.length === 0) {
|
if (string.length === 0) {
|
||||||
return hash;
|
return hash;
|
||||||
@@ -131,7 +166,7 @@ function hash(string) {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
const fileToIdMap = new Map();
|
const fileToIdMap = new Map();
|
||||||
const createModuleIdFactory = () => filePath => {
|
const createModuleIdFactory = () => (filePath: string) => {
|
||||||
if (filePath === '__prelude__') {
|
if (filePath === '__prelude__') {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -142,7 +177,7 @@ const createModuleIdFactory = () => filePath => {
|
|||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
};
|
};
|
||||||
function pluginEntryPoints(additionalPaths = []) {
|
function pluginEntryPoints(additionalPaths: string[] = []) {
|
||||||
const defaultPluginPath = path.join(HOME_DIR, '.flipper', 'node_modules');
|
const defaultPluginPath = path.join(HOME_DIR, '.flipper', 'node_modules');
|
||||||
const entryPoints = entryPointForPluginFolder(defaultPluginPath);
|
const entryPoints = entryPointForPluginFolder(defaultPluginPath);
|
||||||
if (typeof additionalPaths === 'string') {
|
if (typeof additionalPaths === 'string') {
|
||||||
@@ -156,7 +191,7 @@ function pluginEntryPoints(additionalPaths = []) {
|
|||||||
});
|
});
|
||||||
return entryPoints;
|
return entryPoints;
|
||||||
}
|
}
|
||||||
function entryPointForPluginFolder(pluginPath) {
|
function entryPointForPluginFolder(pluginPath: string) {
|
||||||
pluginPath = expandTilde(pluginPath);
|
pluginPath = expandTilde(pluginPath);
|
||||||
if (!fs.existsSync(pluginPath)) {
|
if (!fs.existsSync(pluginPath)) {
|
||||||
return {};
|
return {};
|
||||||
@@ -174,13 +209,14 @@ function entryPointForPluginFolder(pluginPath) {
|
|||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
if (packageJSON) {
|
if (packageJSON) {
|
||||||
try {
|
try {
|
||||||
const pkg = JSON.parse(packageJSON);
|
const pkg = JSON.parse(packageJSON) as PluginManifest;
|
||||||
return {
|
const plugin: PluginInfo = {
|
||||||
packageJSON: pkg,
|
manifest: pkg,
|
||||||
name: pkg.name,
|
name: pkg.name,
|
||||||
entry: path.join(pluginPath, name, pkg.main || 'index.js'),
|
entry: path.join(pluginPath, name, pkg.main || 'index.js'),
|
||||||
rootDir: path.join(pluginPath, name),
|
rootDir: path.join(pluginPath, name),
|
||||||
};
|
};
|
||||||
|
return plugin;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error(
|
||||||
`Could not load plugin "${pluginPath}", because package.json is invalid.`,
|
`Could not load plugin "${pluginPath}", because package.json is invalid.`,
|
||||||
@@ -189,39 +225,41 @@ function entryPointForPluginFolder(pluginPath) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.reduce((acc, cv) => {
|
.reduce<{[key: string]: PluginInfo}>((acc, cv) => {
|
||||||
acc[cv.name] = cv;
|
acc[cv!.name] = cv!;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
}
|
}
|
||||||
function mostRecentlyChanged(dir) {
|
async function mostRecentlyChanged(dir: string) {
|
||||||
return util
|
const files = await util.promisify<string, string[]>(recursiveReaddir)(dir);
|
||||||
.promisify(recursiveReaddir)(dir)
|
return files
|
||||||
.then(files =>
|
|
||||||
files
|
|
||||||
.map(f => fs.lstatSync(f).ctime)
|
.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));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
async function compilePlugin(
|
async function compilePlugin(
|
||||||
{rootDir, name, entry, packageJSON},
|
pluginInfo: PluginInfo,
|
||||||
pluginCache,
|
pluginCache: string,
|
||||||
options,
|
options: DynamicCompileOptions,
|
||||||
) {
|
): Promise<CompiledPluginInfo | null> {
|
||||||
|
const {rootDir, manifest, entry, name} = pluginInfo;
|
||||||
const isPreBundled = fs.existsSync(path.join(rootDir, 'dist'));
|
const isPreBundled = fs.existsSync(path.join(rootDir, 'dist'));
|
||||||
const result = Object.assign({}, packageJSON, {rootDir, name, entry});
|
|
||||||
if (isPreBundled) {
|
if (isPreBundled) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`🥫 Using pre-built version of ${name}...`);
|
console.log(`🥫 Using pre-built version of ${name}...`);
|
||||||
result.out = path.resolve(rootDir, result.main);
|
const out = path.join(
|
||||||
return result;
|
rootDir,
|
||||||
}
|
manifest.main ?? path.join('dist', 'index.js'),
|
||||||
const fileName = `${name}@${packageJSON.version || '0.0.0'}.js`;
|
);
|
||||||
const out = path.join(pluginCache, fileName);
|
return Object.assign({}, pluginInfo.manifest, {out});
|
||||||
result.out = out;
|
} else {
|
||||||
// check if plugin needs to be compiled
|
const out = path.join(
|
||||||
|
pluginCache,
|
||||||
|
`${name}@${manifest.version || '0.0.0'}.js`,
|
||||||
|
);
|
||||||
|
const result = Object.assign({}, pluginInfo.manifest, {out});
|
||||||
const rootDirCtime = await mostRecentlyChanged(rootDir);
|
const rootDirCtime = await mostRecentlyChanged(rootDir);
|
||||||
if (
|
if (
|
||||||
!options.force &&
|
!options.force &&
|
||||||
@@ -240,7 +278,7 @@ async function compilePlugin(
|
|||||||
projectRoot: rootDir,
|
projectRoot: rootDir,
|
||||||
watchFolders: [__dirname, rootDir],
|
watchFolders: [__dirname, rootDir],
|
||||||
serializer: {
|
serializer: {
|
||||||
getRunModuleStatement: moduleID =>
|
getRunModuleStatement: (moduleID: string) =>
|
||||||
`module.exports = global.__r(${moduleID}).default;`,
|
`module.exports = global.__r(${moduleID}).default;`,
|
||||||
createModuleIdFactory,
|
createModuleIdFactory,
|
||||||
},
|
},
|
||||||
@@ -277,4 +315,5 @@ async function compilePlugin(
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -195,6 +195,7 @@
|
|||||||
16
|
16
|
||||||
],
|
],
|
||||||
"rocket": [
|
"rocket": [
|
||||||
|
12,
|
||||||
20
|
20
|
||||||
],
|
],
|
||||||
"settings": [
|
"settings": [
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import url from 'url';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import fixPath from 'fix-path';
|
import fixPath from 'fix-path';
|
||||||
import {exec} from 'child_process';
|
import {exec} from 'child_process';
|
||||||
const compilePlugins = require('./compilePlugins');
|
import compilePlugins from './compilePlugins';
|
||||||
import setup from './setup';
|
import setup from './setup';
|
||||||
import delegateToLauncher from './launcher';
|
import delegateToLauncher from './launcher';
|
||||||
import expandTilde from 'expand-tilde';
|
import expandTilde from 'expand-tilde';
|
||||||
@@ -120,7 +120,7 @@ compilePlugins(
|
|||||||
},
|
},
|
||||||
pluginPaths,
|
pluginPaths,
|
||||||
path.join(flipperDir, 'plugins'),
|
path.join(flipperDir, 'plugins'),
|
||||||
).then((dynamicPlugins: string[]) => {
|
).then(dynamicPlugins => {
|
||||||
ipcMain.on('get-dynamic-plugins', event => {
|
ipcMain.on('get-dynamic-plugins', event => {
|
||||||
event.returnValue = dynamicPlugins;
|
event.returnValue = dynamicPlugins;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user