diff --git a/desktop/plugin-lib/src/__tests__/getPluginDetails.node.ts b/desktop/plugin-lib/src/__tests__/getPluginDetails.node.ts index c2f5b29a2..a79cd9454 100644 --- a/desktop/plugin-lib/src/__tests__/getPluginDetails.node.ts +++ b/desktop/plugin-lib/src/__tests__/getPluginDetails.node.ts @@ -12,6 +12,9 @@ import path from 'path'; import {getInstalledPluginDetails} from '../getPluginDetails'; import {pluginInstallationDir} from '../pluginPaths'; import {normalizePath} from 'flipper-test-utils'; +import {mocked} from 'ts-jest/utils'; + +jest.mock('fs-extra'); jest.mock('../pluginPaths', () => ({ pluginInstallationDir: '/Users/mock/.flipper/thirdparty', @@ -29,7 +32,6 @@ test('getPluginDetailsV1', async () => { description: 'Description of Test Plugin', gatekeeper: 'GK_flipper_plugin_test', }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV1); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -71,7 +73,6 @@ test('getPluginDetailsV2', async () => { description: 'Description of Test Plugin', gatekeeper: 'GK_flipper_plugin_test', }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV2); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -113,7 +114,6 @@ test('id used as title if the latter omited', async () => { description: 'Description of Test Plugin', gatekeeper: 'GK_flipper_plugin_test', }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV2); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -154,7 +154,6 @@ test('name without "flipper-plugin-" prefix is used as title if the latter omite description: 'Description of Test Plugin', gatekeeper: 'GK_flipper_plugin_test', }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV2); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -198,7 +197,6 @@ test('flipper-plugin-version is parsed', async () => { 'flipper-plugin': '^0.45', }, }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV2); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -246,7 +244,6 @@ test('plugin type and supported devices parsed', async () => { description: 'Description of Test Plugin', gatekeeper: 'GK_flipper_plugin_test', }; - jest.mock('fs-extra', () => jest.fn()); fs.readJson = jest.fn().mockImplementation(() => pluginV2); const details = await getInstalledPluginDetails(pluginPath); details.dir = normalizePath(details.dir); @@ -292,3 +289,86 @@ test('plugin type and supported devices parsed', async () => { } `); }); + +test('can merge two package.json files', async () => { + const pluginBase = { + $schema: 'https://fbflipper.com/schemas/plugin-package/v2.json', + name: 'flipper-plugin-test', + title: 'Test', + version: '3.0.1', + pluginType: 'device', + supportedDevices: [ + {os: 'Android', archived: false}, + {os: 'Android', type: 'physical', specs: ['KaiOS']}, + {os: 'iOS', type: 'emulator'}, + ], + main: 'dist/bundle.js', + flipperBundlerEntry: 'src/index.tsx', + description: 'Description of Test Plugin', + bugs: { + url: 'https://github.com/facebook/flipper/issues', + }, + }; + const pluginAdditional = { + gatekeeper: 'GK_flipper_plugin_test', + bugs: { + url: 'https://fb.com/groups/flippersupport', + email: 'flippersupport@example.localhost', + }, + }; + const mockedFs = mocked(fs); + mockedFs.readJson.mockImplementation((file) => { + if (file === path.join(pluginPath, 'package.json')) { + return pluginBase; + } else if (file === path.join(pluginPath, 'fb', 'package.json')) { + return pluginAdditional; + } + }); + mockedFs.pathExists.mockImplementation(() => Promise.resolve(true)); + const details = await getInstalledPluginDetails(pluginPath); + details.dir = normalizePath(details.dir); + details.entry = normalizePath(details.entry); + expect(details).toMatchInlineSnapshot(` + Object { + "bugs": Object { + "email": "flippersupport@example.localhost", + "url": "https://fb.com/groups/flippersupport", + }, + "category": undefined, + "description": "Description of Test Plugin", + "dir": "/Users/mock/.flipper/thirdparty/flipper-plugin-test", + "engines": undefined, + "entry": "/Users/mock/.flipper/thirdparty/flipper-plugin-test/dist/bundle.js", + "flipperSDKVersion": undefined, + "gatekeeper": "GK_flipper_plugin_test", + "icon": undefined, + "id": "flipper-plugin-test", + "isActivatable": true, + "isBundled": false, + "main": "dist/bundle.js", + "name": "flipper-plugin-test", + "pluginType": "device", + "source": "src/index.tsx", + "specVersion": 2, + "supportedDevices": Array [ + Object { + "archived": false, + "os": "Android", + }, + Object { + "os": "Android", + "specs": Array [ + "KaiOS", + ], + "type": "physical", + }, + Object { + "os": "iOS", + "type": "emulator", + }, + ], + "title": "Test", + "version": "3.0.1", + } + `); +}); diff --git a/desktop/plugin-lib/src/getPluginDetails.ts b/desktop/plugin-lib/src/getPluginDetails.ts index 7f4cf6fb4..7e7a6840c 100644 --- a/desktop/plugin-lib/src/getPluginDetails.ts +++ b/desktop/plugin-lib/src/getPluginDetails.ts @@ -16,6 +16,16 @@ import { } from './PluginDetails'; import {pluginCacheDir} from './pluginPaths'; +export async function readPluginPackageJson(dir: string): Promise { + 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 function isPluginJson(packageJson: any): boolean { return packageJson?.keywords?.includes('flipper-plugin'); } @@ -51,8 +61,7 @@ export async function getInstalledPluginDetails( dir: string, packageJson?: any, ): Promise { - packageJson = - packageJson ?? (await fs.readJson(path.join(dir, 'package.json'))); + packageJson = packageJson ?? (await readPluginPackageJson(dir)); const pluginDetails = getPluginDetails(packageJson); const entry = pluginDetails.specVersion === 1 diff --git a/desktop/plugin-lib/src/getSourcePlugins.ts b/desktop/plugin-lib/src/getSourcePlugins.ts index b66540281..31a57ed58 100644 --- a/desktop/plugin-lib/src/getSourcePlugins.ts +++ b/desktop/plugin-lib/src/getSourcePlugins.ts @@ -14,7 +14,7 @@ import {getPluginSourceFolders} from './pluginPaths'; import pmap from 'p-map'; import pfilter from 'p-filter'; import {satisfies} from 'semver'; -import {getInstalledPluginDetails, isPluginJson} from './getPluginDetails'; +import {getInstalledPluginDetails, isPluginDir} from './getPluginDetails'; import {InstalledPluginDetails} from './PluginDetails'; const flipperVersion = require('../package.json').version; @@ -55,55 +55,18 @@ async function entryPointForPluginFolder( } return await fs .readdir(pluginsDir) - .then((entries) => - entries.map((name) => ({ - dir: path.join(pluginsDir, name), - manifestPath: path.join(pluginsDir, name, 'package.json'), - })), - ) - .then((entries) => - pfilter(entries, ({manifestPath}) => fs.pathExists(manifestPath)), - ) + .then((entries) => entries.map((name) => path.join(pluginsDir, name))) + .then((entries) => pfilter(entries, isPluginDir)) .then((packages) => - pmap(packages, async ({manifestPath, dir}) => { + pmap(packages, async (dir) => { try { - const manifest = await fs.readJson(manifestPath); - return { - dir, - manifest, - }; - } catch (e) { - console.error( - `Could not load plugin from "${dir}", because package.json is invalid.`, - e, - ); - return null; - } - }), - ) - .then((packages) => packages.filter(notNull)) - .then((packages) => packages.filter(({manifest}) => !manifest.workspaces)) - .then((packages) => - packages.filter(({manifest}) => { - if (!isPluginJson(manifest)) { - console.log( - `Skipping package "${manifest.name}" as its "keywords" field does not contain tag "flipper-plugin"`, - ); - return false; - } - return true; - }), - ) - .then((packages) => - pmap(packages, async ({manifest, dir}) => { - try { - const details = await getInstalledPluginDetails(dir, manifest); + const details = await getInstalledPluginDetails(dir); if ( details.flipperSDKVersion && !satisfies(flipperVersion, details.flipperSDKVersion) ) { console.warn( - `⚠️ The current Flipper version (${flipperVersion}) doesn't look compatible with the plugin '${manifest.name}', which expects 'flipper-plugin: ${details.flipperSDKVersion}'`, + `⚠️ The current Flipper version (${flipperVersion}) doesn't look compatible with the plugin '${details.name}', which expects 'flipper-plugin: ${details.flipperSDKVersion}'`, ); } return details; diff --git a/desktop/scripts/build-plugin.ts b/desktop/scripts/build-plugin.ts index e2e77a1f6..bb2af2890 100644 --- a/desktop/scripts/build-plugin.ts +++ b/desktop/scripts/build-plugin.ts @@ -66,6 +66,7 @@ async function buildPlugin() { const outputUnpackedArg = argv['output-unpacked']; const minFlipperVersion = argv['min-flipper-version']; const packageJsonPath = path.join(pluginDir, 'package.json'); + const packageJsonOverridePath = path.join(pluginDir, 'fb', 'package.json'); await runBuild(pluginDir, false); const checksum = await computePackageChecksum(pluginDir); if (previousChecksum !== checksum && argv.version) { @@ -86,7 +87,14 @@ async function buildPlugin() { const packageJsonBackupPath = path.join(tmpDir, 'package.json'); await fs.copy(packageJsonPath, packageJsonBackupPath, {overwrite: true}); try { - const packageJson = await fs.readJson(packageJsonPath); + const packageJsonOverride = + (await fs.readJson(packageJsonOverridePath, { + throws: false, + })) ?? {}; + const packageJson = Object.assign( + await fs.readJson(packageJsonPath), + packageJsonOverride, + ); if (minFlipperVersion) { if (!packageJson.engines) { packageJson.engines = {};