Load either installed or bundled version of plugin depending on which is newer

Summary: Load either installed or bundled version of plugin depending on which is newer.

Reviewed By: mweststrate

Differential Revision: D21858965

fbshipit-source-id: aa46eafe0b5137134fadad827749672441f2c9e5
This commit is contained in:
Anton Nikolaev
2020-06-03 07:37:11 -07:00
committed by Facebook GitHub Bot
parent e31ddbc648
commit e65b355fb6
8 changed files with 76 additions and 27 deletions

View File

@@ -15,6 +15,7 @@ import dispatcher, {
checkDisabled,
checkGK,
requirePlugin,
filterNewestVersionOfEachPlugin,
} from '../plugins';
import path from 'path';
import {ipcRenderer, remote} from 'electron';
@@ -140,3 +141,22 @@ test('requirePlugin loads plugin', () => {
expect(plugin!.prototype).toBeInstanceOf(FlipperPlugin);
expect(plugin!.id).toBe(TestPlugin.id);
});
test('newest version of each plugin is taken', () => {
const plugins: PluginDefinition[] = [
{name: 'flipper-plugin-test1', version: '0.1.0'},
{name: 'flipper-plugin-test2', version: '0.1.0-alpha.201'},
{name: 'flipper-plugin-test2', version: '0.1.0-alpha.21'},
{name: 'flipper-plugin-test1', version: '0.10.0'},
];
const filteredPlugins = filterNewestVersionOfEachPlugin(plugins);
expect(filteredPlugins).toHaveLength(2);
expect(filteredPlugins).toContainEqual({
name: 'flipper-plugin-test1',
version: '0.10.0',
});
expect(filteredPlugins).toContainEqual({
name: 'flipper-plugin-test2',
version: '0.1.0-alpha.201',
});
});

View File

@@ -29,6 +29,7 @@ import {default as config} from '../utils/processConfig';
import isProduction from '../utils/isProduction';
import {notNull} from '../utils/typeUtils';
import {sideEffect} from '../utils/sideEffect';
import semver from 'semver';
// eslint-disable-next-line import/no-unresolved
import getPluginIndex from '../utils/getDefaultPluginsIndex';
@@ -58,7 +59,10 @@ export default (store: Store, logger: Logger) => {
const initialPlugins: Array<
typeof FlipperPlugin | typeof FlipperDevicePlugin
> = [...getBundledPlugins(), ...getDynamicPlugins()]
> = filterNewestVersionOfEachPlugin([
...getBundledPlugins(),
...getDynamicPlugins(),
])
.filter(checkDisabled(disabledPlugins))
.filter(checkGK(gatekeepedPlugins))
.map(requirePlugin(failedPlugins, defaultPluginsIndex))
@@ -83,6 +87,21 @@ export default (store: Store, logger: Logger) => {
);
};
export function filterNewestVersionOfEachPlugin(
plugins: PluginDefinition[],
): PluginDefinition[] {
const pluginByName: {[key: string]: PluginDefinition} = {};
for (const plugin of plugins) {
if (
!pluginByName[plugin.name] ||
semver.gt(plugin.version, pluginByName[plugin.name].version, true)
) {
pluginByName[plugin.name] = plugin;
}
}
return Object.values(pluginByName);
}
function getBundledPlugins(): Array<PluginDefinition> {
// DefaultPlugins that are included in the bundle.
// List of defaultPlugins is written at build time

View File

@@ -14,7 +14,7 @@ import fs from 'fs-extra';
import {spawn} from 'promisify-child-process';
import {getWatchFolders} from 'flipper-pkg-lib';
import getAppWatchFolders from './get-app-watch-folders';
import getPlugins from '../static/getPlugins';
import {getSourcePlugins} from '../static/getPlugins';
import {
appDir,
staticDir,
@@ -22,7 +22,7 @@ import {
headlessDir,
babelTransformationsDir,
} from './paths';
import getPluginFolders from '../static/getPluginFolders';
import {getPluginSourceFolders} from '../static/getPluginFolders';
const dev = process.env.NODE_ENV !== 'production';
@@ -33,7 +33,7 @@ export function die(err: Error) {
export async function generatePluginEntryPoints() {
console.log('⚙️ Generating plugin entry points...');
const plugins = await getPlugins();
const plugins = await getSourcePlugins();
if (await fs.pathExists(defaultPluginsIndexDir)) {
await fs.remove(defaultPluginsIndexDir);
}
@@ -107,7 +107,7 @@ export async function compileHeadless(buildFolder: string) {
headlessDir,
...(await getWatchFolders(staticDir)),
...(await getAppWatchFolders()),
...(await getPluginFolders()),
...(await getPluginSourceFolders()),
]
.filter((value, index, self) => self.indexOf(value) === index)
.filter(fs.pathExistsSync);
@@ -128,7 +128,7 @@ export async function compileRenderer(buildFolder: string) {
console.log(`⚙️ Compiling renderer bundle...`);
const watchFolders = [
...(await getAppWatchFolders()),
...(await getPluginFolders()),
...(await getPluginSourceFolders()),
];
try {
await compile(

View File

@@ -25,8 +25,8 @@ import MetroResolver from 'metro-resolver';
import {staticDir, appDir, babelTransformationsDir} from './paths';
import isFB from './isFB';
import getAppWatchFolders from './get-app-watch-folders';
import getPlugins from '../static/getPlugins';
import getPluginFolders from '../static/getPluginFolders';
import {getSourcePlugins} from '../static/getPlugins';
import {getPluginSourceFolders} from '../static/getPluginFolders';
import startWatchPlugins from '../static/startWatchPlugins';
import ensurePluginFoldersWatchable from '../static/ensurePluginFoldersWatchable';
@@ -88,7 +88,7 @@ function launchElectron(port: number) {
async function startMetroServer(app: Express, server: http.Server) {
const watchFolders = (await getAppWatchFolders()).concat(
await getPluginFolders(),
await getPluginSourceFolders(),
);
const baseConfig = await Metro.loadConfig();
const config = Object.assign({}, baseConfig, {
@@ -206,7 +206,7 @@ async function startWatchChanges(io: socketIo.Server) {
),
),
);
const plugins = await getPlugins();
const plugins = await getSourcePlugins();
await startWatchPlugins(plugins, () => {
io.emit('refresh');
});

View File

@@ -14,7 +14,7 @@ import recursiveReaddir from 'recursive-readdir';
import pMap from 'p-map';
import {homedir} from 'os';
import {runBuild, PluginDetails} from 'flipper-pkg-lib';
import getPlugins from './getPlugins';
import {getSourcePlugins, getInstalledPlugins} from './getPlugins';
import startWatchPlugins from './startWatchPlugins';
import ensurePluginFoldersWatchable from './ensurePluginFoldersWatchable';
@@ -41,7 +41,7 @@ export default async function (
): Promise<CompiledPluginDetails[]> {
if (process.env.FLIPPER_FAST_REFRESH) {
console.log(
'🥫 Skipping loading of third-party plugins because Fast Refresh is enabled',
'🥫 Skipping loading of installed plugins because Fast Refresh is enabled',
);
return [];
}
@@ -50,9 +50,12 @@ export default async function (
const defaultPlugins = (
await fs.readJson(path.join(__dirname, 'defaultPlugins', 'index.json'))
).map((p: any) => p.name) as string[];
const dynamicPlugins = (await getPlugins(true)).filter(
(p) => !defaultPlugins.includes(p.name),
);
const dynamicPlugins = [
...(await getInstalledPlugins()),
...(await getSourcePlugins()).filter(
(p) => !defaultPlugins.includes(p.name),
),
];
await fs.ensureDir(pluginCache);
if (options.recompileOnChanges) {
await startWatchChanges(

View File

@@ -7,7 +7,7 @@
* @format
*/
import getPluginFolders from './getPluginFolders';
import {getPluginSourceFolders} from './getPluginFolders';
import fs from 'fs-extra';
const watchmanconfigName = '.watchmanconfig';
@@ -15,7 +15,7 @@ const watchmanconfigName = '.watchmanconfig';
import path from 'path';
export default async function ensurePluginFoldersWatchable() {
const pluginFolders = await getPluginFolders();
const pluginFolders = await getPluginSourceFolders();
for (const pluginFolder of pluginFolders) {
if (!(await hasParentWithWatchmanConfig(pluginFolder))) {
// If no watchman config found in the plugins folder or any its parent, we need to create it.

View File

@@ -12,13 +12,12 @@ import fs from 'fs-extra';
import expandTilde from 'expand-tilde';
import {homedir} from 'os';
export default async function getPluginFolders(
includeThirdparty: boolean = false,
) {
export function getPluginsInstallationFolder(): string {
return path.join(homedir(), '.flipper', 'thirdparty');
}
export async function getPluginSourceFolders(): Promise<string[]> {
const pluginFolders: string[] = [];
if (includeThirdparty) {
pluginFolders.push(path.join(homedir(), '.flipper', 'thirdparty'));
}
if (process.env.FLIPPER_NO_EMBEDDED_PLUGINS === 'true') {
console.log(
'🥫 Skipping embedded plugins because "--no-embedded-plugins" flag provided',

View File

@@ -10,15 +10,23 @@
import path from 'path';
import fs from 'fs-extra';
import expandTilde from 'expand-tilde';
import getPluginFolders from './getPluginFolders';
import {
getPluginsInstallationFolder,
getPluginSourceFolders,
} from './getPluginFolders';
import {PluginDetails, getPluginDetails} from 'flipper-pkg-lib';
import pmap from 'p-map';
import pfilter from 'p-filter';
export default async function getPlugins(
includeThirdparty: boolean = false,
export async function getSourcePlugins(): Promise<PluginDetails[]> {
return await getPluginsFromFolders(await getPluginSourceFolders());
}
export async function getInstalledPlugins(): Promise<PluginDetails[]> {
return await getPluginsFromFolders([getPluginsInstallationFolder()]);
}
async function getPluginsFromFolders(
pluginFolders: string[],
): Promise<PluginDetails[]> {
const pluginFolders = await getPluginFolders(includeThirdparty);
const entryPoints: {[key: string]: PluginDetails} = {};
const additionalPlugins = await pmap(pluginFolders, (path) =>
entryPointForPluginFolder(path),