Use the single type representing plugins
Summary: Use interface PluginDetails everywhere where plugins are handled and removed PluginDefinition type which was effectively a subset of PluginDetails Reviewed By: mweststrate Differential Revision: D21927456 fbshipit-source-id: 434ebeef955b922cc11757e78fbba8dec05f1060
This commit is contained in:
committed by
Facebook GitHub Bot
parent
907cb9e3cc
commit
db3f04a2d7
@@ -15,9 +15,11 @@ export default interface PluginDetails {
|
||||
source: string;
|
||||
main: string;
|
||||
id: string;
|
||||
isDefault: boolean;
|
||||
entry: string;
|
||||
gatekeeper?: string;
|
||||
icon?: string;
|
||||
title: string;
|
||||
icon?: string;
|
||||
description?: string;
|
||||
category?: string;
|
||||
bugs?: {
|
||||
|
||||
@@ -8,7 +8,16 @@
|
||||
*/
|
||||
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import getPluginDetails from '../getPluginDetails';
|
||||
import {pluginInstallationDir} from '../pluginPaths';
|
||||
|
||||
jest.mock('../pluginPaths', () => ({
|
||||
pluginInstallationDir: '/Users/mock/.flipper/thirdparty',
|
||||
pluginCacheDir: '/Users/mock/.flipper/plugins',
|
||||
}));
|
||||
|
||||
const pluginPath = path.join(pluginInstallationDir, 'flipper-plugin-test');
|
||||
|
||||
test('getPluginDetailsV1', async () => {
|
||||
const pluginV1 = {
|
||||
@@ -21,16 +30,18 @@ test('getPluginDetailsV1', async () => {
|
||||
};
|
||||
jest.mock('fs-extra', () => jest.fn());
|
||||
fs.readJson = jest.fn().mockImplementation(() => pluginV1);
|
||||
const details = await getPluginDetails('./plugins/flipper-plugin-test');
|
||||
const details = await getPluginDetails(pluginPath);
|
||||
expect(details).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"bugs": undefined,
|
||||
"category": undefined,
|
||||
"description": "Description of Test Plugin",
|
||||
"dir": "./plugins/flipper-plugin-test",
|
||||
"dir": "/Users/mock/.flipper/thirdparty/flipper-plugin-test",
|
||||
"entry": "/Users/mock/.flipper/plugins/flipper-plugin-test@2.0.0.js",
|
||||
"gatekeeper": "GK_flipper_plugin_test",
|
||||
"icon": undefined,
|
||||
"id": "flipper-plugin-test",
|
||||
"isDefault": false,
|
||||
"main": "dist/bundle.js",
|
||||
"name": "flipper-plugin-test",
|
||||
"source": "src/index.tsx",
|
||||
@@ -54,16 +65,18 @@ test('getPluginDetailsV2', async () => {
|
||||
};
|
||||
jest.mock('fs-extra', () => jest.fn());
|
||||
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
|
||||
const details = await getPluginDetails('./plugins/flipper-plugin-test');
|
||||
const details = await getPluginDetails(pluginPath);
|
||||
expect(details).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"bugs": undefined,
|
||||
"category": undefined,
|
||||
"description": "Description of Test Plugin",
|
||||
"dir": "./plugins/flipper-plugin-test",
|
||||
"dir": "/Users/mock/.flipper/thirdparty/flipper-plugin-test",
|
||||
"entry": "/Users/mock/.flipper/thirdparty/flipper-plugin-test/dist/bundle.js",
|
||||
"gatekeeper": "GK_flipper_plugin_test",
|
||||
"icon": undefined,
|
||||
"id": "flipper-plugin-test",
|
||||
"isDefault": false,
|
||||
"main": "dist/bundle.js",
|
||||
"name": "flipper-plugin-test",
|
||||
"source": "src/index.tsx",
|
||||
@@ -87,16 +100,18 @@ test('id used as title if the latter omited', async () => {
|
||||
};
|
||||
jest.mock('fs-extra', () => jest.fn());
|
||||
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
|
||||
const details = await getPluginDetails('./plugins/flipper-plugin-test');
|
||||
const details = await getPluginDetails(pluginPath);
|
||||
expect(details).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"bugs": undefined,
|
||||
"category": undefined,
|
||||
"description": "Description of Test Plugin",
|
||||
"dir": "./plugins/flipper-plugin-test",
|
||||
"dir": "/Users/mock/.flipper/thirdparty/flipper-plugin-test",
|
||||
"entry": "/Users/mock/.flipper/thirdparty/flipper-plugin-test/dist/bundle.js",
|
||||
"gatekeeper": "GK_flipper_plugin_test",
|
||||
"icon": undefined,
|
||||
"id": "test",
|
||||
"isDefault": false,
|
||||
"main": "dist/bundle.js",
|
||||
"name": "flipper-plugin-test",
|
||||
"source": "src/index.tsx",
|
||||
@@ -119,16 +134,18 @@ test('name without "flipper-plugin-" prefix is used as title if the latter omite
|
||||
};
|
||||
jest.mock('fs-extra', () => jest.fn());
|
||||
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
|
||||
const details = await getPluginDetails('./plugins/flipper-plugin-test');
|
||||
const details = await getPluginDetails(pluginPath);
|
||||
expect(details).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"bugs": undefined,
|
||||
"category": undefined,
|
||||
"description": "Description of Test Plugin",
|
||||
"dir": "./plugins/flipper-plugin-test",
|
||||
"dir": "/Users/mock/.flipper/thirdparty/flipper-plugin-test",
|
||||
"entry": "/Users/mock/.flipper/thirdparty/flipper-plugin-test/dist/bundle.js",
|
||||
"gatekeeper": "GK_flipper_plugin_test",
|
||||
"icon": undefined,
|
||||
"id": "flipper-plugin-test",
|
||||
"isDefault": false,
|
||||
"main": "dist/bundle.js",
|
||||
"name": "flipper-plugin-test",
|
||||
"source": "src/index.tsx",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import PluginDetails from './PluginDetails';
|
||||
import {pluginCacheDir} from './pluginPaths';
|
||||
|
||||
export default async function (
|
||||
pluginDir: string,
|
||||
@@ -44,8 +45,13 @@ async function getPluginDetailsV1(
|
||||
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,
|
||||
@@ -66,7 +72,9 @@ async function getPluginDetailsV2(
|
||||
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,
|
||||
|
||||
@@ -10,17 +10,19 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {promisify} from 'util';
|
||||
import {homedir} from 'os';
|
||||
import {PluginManager as PM} from 'live-plugin-manager';
|
||||
import decompress from 'decompress';
|
||||
import decompressTargz from 'decompress-targz';
|
||||
import decompressUnzip from 'decompress-unzip';
|
||||
import tmp from 'tmp';
|
||||
import PluginDetails from './PluginDetails';
|
||||
import getPluginDetails from './getPluginDetails';
|
||||
import {pluginInstallationDir} from './pluginPaths';
|
||||
|
||||
export type PluginMap = Map<string, PluginDetails>;
|
||||
|
||||
const getTmpDir = promisify(tmp.dir) as () => Promise<string>;
|
||||
|
||||
export const PLUGIN_DIR = path.join(homedir(), '.flipper', 'thirdparty');
|
||||
|
||||
function providePluginManager(): PM {
|
||||
return new PM({
|
||||
ignoredDependencies: [/^flipper$/, /^react$/, /^react-dom$/, /^@types\//],
|
||||
@@ -38,10 +40,10 @@ async function installPluginFromTempDir(pluginDir: string) {
|
||||
);
|
||||
const name = packageJSON.name;
|
||||
|
||||
await fs.ensureDir(PLUGIN_DIR);
|
||||
await fs.ensureDir(pluginInstallationDir);
|
||||
// create empty watchman config (required by metro's file watcher)
|
||||
await fs.writeFile(path.join(PLUGIN_DIR, '.watchmanconfig'), '{}');
|
||||
const destinationDir = path.join(PLUGIN_DIR, name);
|
||||
await fs.writeFile(path.join(pluginInstallationDir, '.watchmanconfig'), '{}');
|
||||
const destinationDir = path.join(pluginInstallationDir, name);
|
||||
// Clean up existing destination files.
|
||||
await fs.remove(destinationDir);
|
||||
await fs.ensureDir(destinationDir);
|
||||
@@ -118,3 +120,32 @@ export async function installPluginFromFile(packagePath: string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function readInstalledPlugins(): Promise<PluginMap> {
|
||||
const pluginDirExists = await fs.pathExists(pluginInstallationDir);
|
||||
if (!pluginDirExists) {
|
||||
return new Map();
|
||||
}
|
||||
const dirs = await fs.readdir(pluginInstallationDir);
|
||||
const plugins = await Promise.all<[string, PluginDetails]>(
|
||||
dirs.map(
|
||||
(name) =>
|
||||
new Promise(async (resolve, reject) => {
|
||||
const pluginDir = path.join(pluginInstallationDir, name);
|
||||
if (!(await fs.lstat(pluginDir)).isDirectory()) {
|
||||
return resolve(undefined);
|
||||
}
|
||||
try {
|
||||
resolve([name, await getPluginDetails(pluginDir)]);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}),
|
||||
),
|
||||
);
|
||||
return new Map(plugins.filter(Boolean));
|
||||
}
|
||||
|
||||
export async function removePlugin(name: string): Promise<void> {
|
||||
await fs.remove(path.join(pluginInstallationDir, name));
|
||||
}
|
||||
17
desktop/plugin-lib/src/pluginPaths.ts
Normal file
17
desktop/plugin-lib/src/pluginPaths.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 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 path from 'path';
|
||||
import {homedir} from 'os';
|
||||
|
||||
export const flipperDataDir = path.join(homedir(), '.flipper');
|
||||
|
||||
export const pluginInstallationDir = path.join(flipperDataDir, 'thirdparty');
|
||||
|
||||
export const pluginCacheDir = path.join(flipperDataDir, 'plugins');
|
||||
Reference in New Issue
Block a user