Added new package "flipper-plugin-lib" which contains re-usable code related to plugin installation

Summary: No functional changes, just refactoring for easier implementation of plugin updates installation

Reviewed By: passy

Differential Revision: D21902525

fbshipit-source-id: fbfa221a89b879b0d08127676b27df65ef63307d
This commit is contained in:
Anton Nikolaev
2020-06-05 06:07:54 -07:00
committed by Facebook GitHub Bot
parent 579172fa39
commit 339b786fb5
21 changed files with 111 additions and 11 deletions

View File

@@ -0,0 +1,26 @@
/**
* 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
*/
export default interface PluginDetails {
dir: string;
name: string;
specVersion: number;
version: string;
source: string;
main: string;
id: string;
gatekeeper?: string;
icon?: string;
title?: string;
category?: string;
bugs?: {
email?: string;
url?: string;
};
}

View File

@@ -0,0 +1,132 @@
/**
* 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 getPluginDetails from '../getPluginDetails';
test('getPluginDetailsV1', async () => {
const pluginV1 = {
name: 'flipper-plugin-test',
version: '2.0.0',
title: 'Test Plugin',
main: 'src/index.tsx',
gatekeeper: 'GK_flipper_plugin_test',
};
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV1);
const details = await getPluginDetails('./plugins/flipper-plugin-test');
expect(details).toMatchInlineSnapshot(`
Object {
"bugs": undefined,
"category": undefined,
"dir": "./plugins/flipper-plugin-test",
"gatekeeper": "GK_flipper_plugin_test",
"icon": undefined,
"id": "flipper-plugin-test",
"main": "dist/bundle.js",
"name": "flipper-plugin-test",
"source": "src/index.tsx",
"specVersion": 1,
"title": "Test Plugin",
"version": "2.0.0",
}
`);
});
test('getPluginDetailsV2', async () => {
const pluginV2 = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-test',
title: 'Test',
version: '3.0.1',
main: 'dist/bundle.js',
flipperBundlerEntry: 'src/index.tsx',
gatekeeper: 'GK_flipper_plugin_test',
};
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getPluginDetails('./plugins/flipper-plugin-test');
expect(details).toMatchInlineSnapshot(`
Object {
"bugs": undefined,
"category": undefined,
"dir": "./plugins/flipper-plugin-test",
"gatekeeper": "GK_flipper_plugin_test",
"icon": undefined,
"id": "flipper-plugin-test",
"main": "dist/bundle.js",
"name": "flipper-plugin-test",
"source": "src/index.tsx",
"specVersion": 2,
"title": "Test",
"version": "3.0.1",
}
`);
});
test('id used as title if the latter omited', async () => {
const pluginV2 = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-test',
id: 'test',
version: '3.0.1',
main: 'dist/bundle.js',
flipperBundlerEntry: 'src/index.tsx',
gatekeeper: 'GK_flipper_plugin_test',
};
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getPluginDetails('./plugins/flipper-plugin-test');
expect(details).toMatchInlineSnapshot(`
Object {
"bugs": undefined,
"category": undefined,
"dir": "./plugins/flipper-plugin-test",
"gatekeeper": "GK_flipper_plugin_test",
"icon": undefined,
"id": "test",
"main": "dist/bundle.js",
"name": "flipper-plugin-test",
"source": "src/index.tsx",
"specVersion": 2,
"title": "test",
"version": "3.0.1",
}
`);
});
test('name without "flipper-plugin-" prefix is used as title if the latter omited', async () => {
const pluginV2 = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-test',
version: '3.0.1',
main: 'dist/bundle.js',
flipperBundlerEntry: 'src/index.tsx',
gatekeeper: 'GK_flipper_plugin_test',
};
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getPluginDetails('./plugins/flipper-plugin-test');
expect(details).toMatchInlineSnapshot(`
Object {
"bugs": undefined,
"category": undefined,
"dir": "./plugins/flipper-plugin-test",
"gatekeeper": "GK_flipper_plugin_test",
"icon": undefined,
"id": "flipper-plugin-test",
"main": "dist/bundle.js",
"name": "flipper-plugin-test",
"source": "src/index.tsx",
"specVersion": 2,
"title": "test",
"version": "3.0.1",
}
`);
});

View File

@@ -0,0 +1,84 @@
/**
* 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';
export default async function (
pluginDir: string,
packageJson?: any,
): Promise<PluginDetails> {
packageJson =
packageJson || (await fs.readJson(path.join(pluginDir, 'package.json')));
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}`);
}
}
// 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',
source: packageJson.main,
id: packageJson.name,
gatekeeper: packageJson.gatekeeper,
icon: packageJson.icon,
title: packageJson.title || packageJson.name,
category: packageJson.category,
bugs: packageJson.bugs,
};
}
// 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,
source: packageJson.flipperBundlerEntry,
id: packageJson.id || packageJson.name,
gatekeeper: packageJson.gatekeeper,
icon: packageJson.icon,
title:
packageJson.title || packageJson.id || getTitleFromName(packageJson.name),
category: packageJson.category,
bugs: packageJson.bugs,
};
}
function getTitleFromName(name: string) {
const prefix = 'flipper-plugin-';
if (name.startsWith(prefix)) {
return name.substr(prefix.length);
}
}

View File

@@ -0,0 +1,11 @@
/**
* 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
*/
export {default as PluginDetails} from './PluginDetails';
export {default as getPluginDetails} from './getPluginDetails';