Files
flipper/desktop/plugin-lib/src/getSourcePlugins.ts
Anton Nikolaev 5383017299 Separate interfaces for installed, bundled and downloadable plugins
Summary:
I've re-designed interfaces describing plugins as I found that mental overhead working with them became too expensive because of slightly flawed design. However this cascaded changes in many files so you can see how extensively these interfaces used in our codebase.

Before this change we had one interface PluginDetails which described three different entities: 1) plugins installed on the disk 2) plugins bundled into Flipper 3) plugins available on Marketplace. It's hard to use this "general" PluginDetails interface because of this as you always need to think about all three use cases everywhere.

After this change we have 3 separate interfaces: InstalledPluginDetails, BundledPluginDetails and DownloadablePluginDetails and things became much type-safer now.

Reviewed By: mweststrate

Differential Revision: D25530383

fbshipit-source-id: b93593916a980c04e36dc6ffa168797645a0ff9c
2020-12-15 09:31:57 -08:00

131 lines
4.0 KiB
TypeScript

/**
* 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
*/
import path from 'path';
import fs from 'fs-extra';
import expandTilde from 'expand-tilde';
import {getPluginSourceFolders} from './pluginPaths';
import pmap from 'p-map';
import pfilter from 'p-filter';
import {satisfies} from 'semver';
import {getInstalledPluginDetails} from './getPluginDetails';
import {InstalledPluginDetails} from './PluginDetails';
const flipperVersion = require('../package.json').version;
export async function getSourcePlugins(): Promise<InstalledPluginDetails[]> {
const pluginFolders = await getPluginSourceFolders();
const entryPoints: {[key: string]: InstalledPluginDetails} = {};
const additionalPlugins = await pmap(pluginFolders, (path) =>
entryPointForPluginFolder(path),
);
for (const p of additionalPlugins) {
Object.keys(p).forEach((key) => {
entryPoints[key] = p[key];
});
}
const allPlugins = Object.values(entryPoints);
if (process.env.FLIPPER_ENABLED_PLUGINS) {
const pluginNames = new Set<string>(
process.env.FLIPPER_ENABLED_PLUGINS.split(',').map((x) =>
x.toLowerCase(),
),
);
return allPlugins.filter(
(x) =>
pluginNames.has(x.name) ||
pluginNames.has(x.id) ||
pluginNames.has(x.name.replace('flipper-plugin-', '')),
);
}
return allPlugins;
}
async function entryPointForPluginFolder(
pluginsDir: string,
): Promise<{[key: string]: InstalledPluginDetails}> {
pluginsDir = expandTilde(pluginsDir);
if (!fs.existsSync(pluginsDir)) {
return {};
}
return await fs
.readdir(pluginsDir)
.then((entries) =>
entries.map((name) => ({
dir: path.join(pluginsDir, name),
manifestPath: path.join(pluginsDir, name, 'package.json'),
})),
)
.then((entries) =>
pfilter(entries, ({manifestPath}) => fs.pathExists(manifestPath)),
)
.then((packages) =>
pmap(packages, async ({manifestPath, dir}) => {
try {
const manifest = await fs.readJson(manifestPath);
return {
dir,
manifest,
};
} catch (e) {
console.error(
`Could not load plugin from "${dir}", because package.json is invalid.`,
);
console.error(e);
return null;
}
}),
)
.then((packages) => packages.filter(notNull))
.then((packages) => packages.filter(({manifest}) => !manifest.workspaces))
.then((packages) =>
packages.filter(({manifest: {keywords, name}}) => {
if (!keywords || !keywords.includes('flipper-plugin')) {
console.log(
`Skipping package "${name}" as its "keywords" field does not contain tag "flipper-plugin"`,
);
return false;
}
return true;
}),
)
.then((packages) =>
pmap(packages, async ({manifest, dir}) => {
try {
const details = await getInstalledPluginDetails(dir, manifest);
if (
details.flipperSDKVersion &&
!satisfies(flipperVersion, details.flipperSDKVersion)
) {
console.warn(
`⚠️ The current Flipper version (${flipperVersion}) doesn't look compatible with the plugin '${manifest.name}', which expects 'flipper-plugin: ${details.flipperSDKVersion}'`,
);
}
return details;
} catch (e) {
console.error(
`Could not load plugin from "${dir}", because package.json is invalid.`,
);
console.error(e);
return null;
}
}),
)
.then((plugins) => plugins.filter(notNull))
.then((plugins) =>
plugins.reduce<{[key: string]: InstalledPluginDetails}>((acc, cv) => {
acc[cv!.name] = cv!;
return acc;
}, {}),
);
}
function notNull<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}