Files
flipper/desktop/plugin-lib/src/getPluginDetails.ts
Anton Nikolaev 02d695cb28 Keep multiple installed versions of each plugin
Summary:
This diff changes directory structure for installed plugins to allow installation of multiple versions simultaneously, e.g. to to allow downloading new plugin version while user is still using the previous one, and to have possibility of fast rollback to the previous installed if necessary. The new folder for installed plugins is located in `~/.flipper/installed-plugins` and has the following structure:

  flipper-plugin-reactotron
    1.0.0
      ...
      package.json
    1.0.1
      ...
      package.json
  flipper-plugin-network
    0.67.1
      ...
      package.json
    0.67.2
      ...
      package.json

The tricky part here is that we also need to migrate already installed plugins from the old folder `~/.flipper/thirdparty` to the new folder and maintain the new structure for them.

Another tricky part is that we need to periodically cleanup old versions. For now we will just keep 2 versions of each plugin. Cleanup is performed in background right after Flipper startup.

Reviewed By: mweststrate

Differential Revision: D25393474

fbshipit-source-id: 26617ac26114148f797cc3d6765a42242edc205e
2020-12-15 09:31:54 -08:00

120 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 {PluginDetails} from './PluginDetails';
import {getPluginVersionInstallationDir, pluginCacheDir} from './pluginPaths';
export async function getPluginDetails(pluginDir: string, packageJson: any) {
const specVersion =
packageJson.$schema &&
packageJson.$schema ===
'https://fbflipper.com/schemas/plugin-package/v2.json'
? 2
: 1;
switch (specVersion) {
case 1:
return await getPluginDetailsV1(pluginDir, packageJson);
case 2:
return await getPluginDetailsV2(pluginDir, 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 getPluginDetailsFromPackageJson(packageJson: any) {
const pluginDir = getPluginVersionInstallationDir(
packageJson.name,
packageJson.version,
);
return await getPluginDetails(pluginDir, packageJson);
}
export async function getDownloadablePluginDetails(
packageJson: any,
downloadUrl: string,
lastUpdated: Date,
) {
const details = await getPluginDetailsFromPackageJson(packageJson);
return {
...details,
downloadUrl,
lastUpdated,
};
}
// Plugins packaged using V1 are distributed as sources and compiled in run-time.
async function getPluginDetailsV1(
pluginDir: string,
packageJson: any,
): Promise<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,
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.
async function getPluginDetailsV2(
pluginDir: string,
packageJson: any,
): Promise<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,
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) {
const prefix = 'flipper-plugin-';
if (name.startsWith(prefix)) {
return name.substr(prefix.length);
}
}