Files
flipper/desktop/plugin-lib/src/getPluginDetails.ts
Michel Weststrate 64747dc417 move plugin management from ui-core to server-core
Summary:
Follow up of D32665064, this diff moves all plugin management logic from flipper-ui to flipper-server. Things like downloading, installing, querying new plugins.

Loading plugins is handled separately in the next diff.

Reviewed By: nikoant

Differential Revision: D32666537

fbshipit-source-id: 9786b82987f00180bb26200e38735b334dc4d5c3
2021-12-08 04:30:56 -08:00

74 lines
2.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 {
getPluginDetails,
InstalledPluginDetails,
isPluginJson,
} from 'flipper-common';
import {pluginCacheDir} from './pluginPaths';
export async function readPluginPackageJson(dir: string): Promise<any> {
const baseJson = await fs.readJson(path.join(dir, 'package.json'));
if (await fs.pathExists(path.join(dir, 'fb', 'package.json'))) {
const addedJson = await fs.readJson(path.join(dir, 'fb', 'package.json'));
return Object.assign({}, baseJson, addedJson);
} else {
return baseJson;
}
}
export async function isPluginDir(dir: string): Promise<boolean> {
const packageJsonPath = path.join(dir, 'package.json');
const json = (await fs.pathExists(packageJsonPath))
? await fs.readJson(path.join(dir, 'package.json'), {
throws: false,
})
: undefined;
return isPluginJson(json);
}
export async function getInstalledPluginDetails(
dir: string,
packageJson?: any,
): Promise<InstalledPluginDetails> {
packageJson = packageJson ?? (await readPluginPackageJson(dir));
const pluginDetails = getPluginDetails(packageJson);
const [hasOverviewDocs, hasSetupDocs] = await Promise.all([
pluginDetails.publishedDocs?.overview === undefined
? fs.pathExists(path.join(dir, 'docs', 'overview.mdx'))
: Promise.resolve(pluginDetails.publishedDocs.overview),
pluginDetails.publishedDocs?.setup === undefined
? fs.pathExists(path.join(dir, 'docs', 'setup.mdx'))
: Promise.resolve(pluginDetails.publishedDocs.setup),
]);
if (hasOverviewDocs || hasSetupDocs) {
pluginDetails.publishedDocs = {
overview: hasOverviewDocs,
setup: hasSetupDocs,
};
}
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,
};
}