Files
flipper/desktop/plugin-lib/src/getPluginDetails.ts
Anton Nikolaev 899fcd0783 Device plugin management (1/n): use static metadata for checking plugin compatibility with devices
Summary:
*Stack summary*: this stack adds ability to manage device plugins in the same way as client plugins: install, update, uninstall, enable (star) and disable (unstar) them.

*Diff summary*: changed the way how plugin compatibility with devices is checked from dynamic call to "supportsDevice" to static checks of "supportedDevices" metadata property which make it possible to check compatibility without even downloading plugin from Marketplace.

Changelog: plugin compatibility with devices is now checked according to metadata in property "supportedDevices" in package.json

Reviewed By: mweststrate

Differential Revision: D26315848

fbshipit-source-id: 6e4b052c4ea0507ee185fc17999b6211cdb11093
2021-02-16 10:50:18 -08:00

124 lines
3.4 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'],
pluginType: packageJson?.pluginType,
supportedDevices: packageJson?.supportedDevices,
};
}
// 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'],
pluginType: packageJson?.pluginType,
supportedDevices: packageJson?.supportedDevices,
};
}
function getTitleFromName(name: string): string {
const prefix = 'flipper-plugin-';
if (name.startsWith(prefix)) {
return name.substr(prefix.length);
}
return name;
}