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
120 lines
3.2 KiB
TypeScript
120 lines
3.2 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 fs from 'fs-extra';
|
|
import path from 'path';
|
|
import {
|
|
DownloadablePluginDetails,
|
|
InstalledPluginDetails,
|
|
PluginDetails,
|
|
} from './PluginDetails';
|
|
import {pluginCacheDir} from './pluginPaths';
|
|
|
|
export function getPluginDetails(packageJson: any): PluginDetails {
|
|
const specVersion =
|
|
packageJson.$schema &&
|
|
packageJson.$schema ===
|
|
'https://fbflipper.com/schemas/plugin-package/v2.json'
|
|
? 2
|
|
: 1;
|
|
switch (specVersion) {
|
|
case 1:
|
|
return getPluginDetailsV1(packageJson);
|
|
case 2:
|
|
return getPluginDetailsV2(packageJson);
|
|
default:
|
|
throw new Error(`Unknown plugin format version: ${specVersion}`);
|
|
}
|
|
}
|
|
|
|
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 function getDownloadablePluginDetails(
|
|
packageJson: any,
|
|
downloadUrl: string,
|
|
lastUpdated: Date,
|
|
): 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.
|
|
function getPluginDetailsV1(packageJson: any): PluginDetails {
|
|
return {
|
|
specVersion: 1,
|
|
name: packageJson.name,
|
|
version: packageJson.version,
|
|
main: 'dist/bundle.js',
|
|
source: packageJson.main,
|
|
id: packageJson.name,
|
|
gatekeeper: packageJson.gatekeeper,
|
|
icon: packageJson.icon,
|
|
title: packageJson.title || packageJson.name,
|
|
description: packageJson.description,
|
|
category: packageJson.category,
|
|
bugs: packageJson.bugs,
|
|
flipperSDKVersion: packageJson?.peerDependencies?.['flipper-plugin'],
|
|
};
|
|
}
|
|
|
|
// Plugins packaged using V2 are pre-bundled, so compilation in run-time is not required for them.
|
|
function getPluginDetailsV2(packageJson: any): PluginDetails {
|
|
return {
|
|
specVersion: 2,
|
|
name: packageJson.name,
|
|
version: packageJson.version,
|
|
main: packageJson.main,
|
|
source: packageJson.flipperBundlerEntry,
|
|
id: packageJson.id || packageJson.name,
|
|
gatekeeper: packageJson.gatekeeper,
|
|
icon: packageJson.icon,
|
|
title:
|
|
packageJson.title || packageJson.id || getTitleFromName(packageJson.name),
|
|
description: packageJson.description,
|
|
category: packageJson.category,
|
|
bugs: packageJson.bugs,
|
|
flipperSDKVersion: packageJson?.peerDependencies?.['flipper-plugin'],
|
|
};
|
|
}
|
|
|
|
function getTitleFromName(name: string): string {
|
|
const prefix = 'flipper-plugin-';
|
|
if (name.startsWith(prefix)) {
|
|
return name.substr(prefix.length);
|
|
}
|
|
return name;
|
|
}
|