Files
flipper/desktop/plugin-lib/src/getUpdatablePlugins.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

130 lines
3.8 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 {InstalledPluginDetails} from './PluginDetails';
import {getInstalledPlugins} from './pluginInstaller';
import semver from 'semver';
import {getNpmHostedPlugins, NpmPackageDescriptor} from './getNpmHostedPlugins';
import NpmApi from 'npm-api';
import {getInstalledPluginDetails, getPluginDetails} from './getPluginDetails';
import {getPluginVersionInstallationDir} from './pluginPaths';
import pmap from 'p-map';
import {notNull} from './typeUtils';
const npmApi = new NpmApi();
export type UpdateResult =
| {kind: 'not-installed'; version: string}
| {kind: 'up-to-date'}
| {kind: 'error'; error: Error}
| {kind: 'update-available'; version: string};
export type UpdatablePlugin = {
updateStatus: UpdateResult;
};
export type UpdatablePluginDetails = InstalledPluginDetails & UpdatablePlugin;
export async function getUpdatablePlugins(
query?: string,
): Promise<UpdatablePluginDetails[]> {
const installedPlugins = await getInstalledPlugins();
const npmHostedPlugins = new Map<string, NpmPackageDescriptor>(
(await getNpmHostedPlugins()).map((p) => [p.name, p]),
);
const annotatedInstalledPlugins = await pmap(
installedPlugins,
async (installedPlugin): Promise<UpdatablePluginDetails> => {
try {
const npmPackageDescriptor = npmHostedPlugins.get(installedPlugin.name);
if (npmPackageDescriptor) {
npmHostedPlugins.delete(installedPlugin.name);
if (
semver.lt(installedPlugin.version, npmPackageDescriptor.version)
) {
const pkg = await npmApi.repo(npmPackageDescriptor.name).package();
const npmPluginDetails = await getInstalledPluginDetails(
getPluginVersionInstallationDir(
npmPackageDescriptor.name,
npmPackageDescriptor.version,
),
pkg,
);
return {
...npmPluginDetails,
updateStatus: {
kind: 'update-available',
version: npmPluginDetails.version,
},
};
}
}
const updateStatus: UpdateResult = {kind: 'up-to-date'};
return {
...installedPlugin,
updateStatus,
};
} catch (error) {
return {
...installedPlugin,
updateStatus: {
kind: 'error',
error,
},
};
}
},
{
concurrency: 4,
},
);
const annotatedNotInstalledPlugins = await pmap(
npmHostedPlugins.values(),
async (notInstalledPlugin) => {
try {
const pkg = await npmApi.repo(notInstalledPlugin.name).package();
const npmPluginDetails = getPluginDetails(pkg);
if (npmPluginDetails.specVersion === 1) {
return null;
}
return {
...npmPluginDetails,
updateStatus: {
kind: 'not-installed',
version: npmPluginDetails.version,
},
} as UpdatablePluginDetails;
} catch (error) {
console.log(
`Failed to load details from npm for plugin ${notInstalledPlugin.name}`,
error,
);
return null;
}
},
{
concurrency: 4,
},
);
return [
...annotatedInstalledPlugins.sort((p1, p2) =>
p1.name.localeCompare(p2.name),
),
...annotatedNotInstalledPlugins
.filter(notNull)
.sort((p1, p2) => p1.name.localeCompare(p2.name)),
].filter(
(p) =>
!query ||
p.name.includes(query) ||
p.id.includes(query) ||
p.description?.includes(query) ||
p.title?.includes(query),
);
}