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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
efb82e80b5
commit
5383017299
@@ -9,10 +9,14 @@
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {PluginDetails} from './PluginDetails';
|
||||
import {getPluginVersionInstallationDir, pluginCacheDir} from './pluginPaths';
|
||||
import {
|
||||
DownloadablePluginDetails,
|
||||
InstalledPluginDetails,
|
||||
PluginDetails,
|
||||
} from './PluginDetails';
|
||||
import {pluginCacheDir} from './pluginPaths';
|
||||
|
||||
export async function getPluginDetails(pluginDir: string, packageJson: any) {
|
||||
export function getPluginDetails(packageJson: any): PluginDetails {
|
||||
const specVersion =
|
||||
packageJson.$schema &&
|
||||
packageJson.$schema ===
|
||||
@@ -21,60 +25,61 @@ export async function getPluginDetails(pluginDir: string, packageJson: any) {
|
||||
: 1;
|
||||
switch (specVersion) {
|
||||
case 1:
|
||||
return await getPluginDetailsV1(pluginDir, packageJson);
|
||||
return getPluginDetailsV1(packageJson);
|
||||
case 2:
|
||||
return await getPluginDetailsV2(pluginDir, packageJson);
|
||||
return getPluginDetailsV2(packageJson);
|
||||
default:
|
||||
throw new Error(`Unknown plugin format version: ${specVersion}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPluginDetailsFromDir(
|
||||
pluginDir: string,
|
||||
): Promise<PluginDetails> {
|
||||
const packageJson = await fs.readJson(path.join(pluginDir, 'package.json'));
|
||||
return await getPluginDetails(pluginDir, packageJson);
|
||||
export async function getInstalledPluginDetails(
|
||||
dir: string,
|
||||
packageJson?: any,
|
||||
): Promise<InstalledPluginDetails> {
|
||||
packageJson =
|
||||
packageJson ?? (await fs.readJson(path.join(dir, 'package.json')));
|
||||
const pluginDetails = getPluginDetails(packageJson);
|
||||
const entry =
|
||||
pluginDetails.specVersion === 1
|
||||
? path.resolve(
|
||||
pluginCacheDir,
|
||||
`${packageJson.name}@${packageJson.version || '0.0.0'}.js`,
|
||||
)
|
||||
: path.resolve(dir, packageJson.main);
|
||||
return {
|
||||
...pluginDetails,
|
||||
isBundled: false,
|
||||
isActivatable: true,
|
||||
dir,
|
||||
entry,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getPluginDetailsFromPackageJson(packageJson: any) {
|
||||
const pluginDir = getPluginVersionInstallationDir(
|
||||
packageJson.name,
|
||||
packageJson.version,
|
||||
);
|
||||
return await getPluginDetails(pluginDir, packageJson);
|
||||
}
|
||||
|
||||
export async function getDownloadablePluginDetails(
|
||||
export function getDownloadablePluginDetails(
|
||||
packageJson: any,
|
||||
downloadUrl: string,
|
||||
lastUpdated: Date,
|
||||
) {
|
||||
const details = await getPluginDetailsFromPackageJson(packageJson);
|
||||
): DownloadablePluginDetails {
|
||||
const details = getPluginDetails(packageJson);
|
||||
return {
|
||||
...details,
|
||||
isBundled: false,
|
||||
isActivatable: false,
|
||||
downloadUrl,
|
||||
lastUpdated,
|
||||
};
|
||||
}
|
||||
|
||||
// Plugins packaged using V1 are distributed as sources and compiled in run-time.
|
||||
async function getPluginDetailsV1(
|
||||
pluginDir: string,
|
||||
packageJson: any,
|
||||
): Promise<PluginDetails> {
|
||||
function getPluginDetailsV1(packageJson: any): PluginDetails {
|
||||
return {
|
||||
specVersion: 1,
|
||||
dir: pluginDir,
|
||||
name: packageJson.name,
|
||||
version: packageJson.version,
|
||||
main: 'dist/bundle.js',
|
||||
entry: path.join(
|
||||
pluginCacheDir,
|
||||
`${packageJson.name}@${packageJson.version || '0.0.0'}.js`,
|
||||
),
|
||||
source: packageJson.main,
|
||||
id: packageJson.name,
|
||||
isDefault: false,
|
||||
gatekeeper: packageJson.gatekeeper,
|
||||
icon: packageJson.icon,
|
||||
title: packageJson.title || packageJson.name,
|
||||
@@ -86,19 +91,13 @@ async function getPluginDetailsV1(
|
||||
}
|
||||
|
||||
// Plugins packaged using V2 are pre-bundled, so compilation in run-time is not required for them.
|
||||
async function getPluginDetailsV2(
|
||||
pluginDir: string,
|
||||
packageJson: any,
|
||||
): Promise<PluginDetails> {
|
||||
function getPluginDetailsV2(packageJson: any): PluginDetails {
|
||||
return {
|
||||
specVersion: 2,
|
||||
dir: pluginDir,
|
||||
name: packageJson.name,
|
||||
version: packageJson.version,
|
||||
main: packageJson.main,
|
||||
entry: path.resolve(pluginDir, packageJson.main),
|
||||
source: packageJson.flipperBundlerEntry,
|
||||
isDefault: false,
|
||||
id: packageJson.id || packageJson.name,
|
||||
gatekeeper: packageJson.gatekeeper,
|
||||
icon: packageJson.icon,
|
||||
@@ -111,9 +110,10 @@ async function getPluginDetailsV2(
|
||||
};
|
||||
}
|
||||
|
||||
function getTitleFromName(name: string) {
|
||||
function getTitleFromName(name: string): string {
|
||||
const prefix = 'flipper-plugin-';
|
||||
if (name.startsWith(prefix)) {
|
||||
return name.substr(prefix.length);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user