Merge plugin package.json from public and fb-internal parts

Summary: Allow splitting package.json to public one and fb-internal one located in "fb/package.json". When plugin is packaged, fields in package.json are overwritten by fields from "fb/package.json" if they exist. This give us a way to specify additional metadata which only make sense internally (e.g. oncall and internal links to docs and support).

Reviewed By: mweststrate

Differential Revision: D28542101

fbshipit-source-id: c0167461897a994e5731aaf0fe625de052eda864
This commit is contained in:
Anton Nikolaev
2021-05-21 07:14:13 -07:00
committed by Facebook GitHub Bot
parent d680a2807f
commit 5ae104cc59
4 changed files with 112 additions and 52 deletions

View File

@@ -12,6 +12,9 @@ import path from 'path';
import {getInstalledPluginDetails} from '../getPluginDetails'; import {getInstalledPluginDetails} from '../getPluginDetails';
import {pluginInstallationDir} from '../pluginPaths'; import {pluginInstallationDir} from '../pluginPaths';
import {normalizePath} from 'flipper-test-utils'; import {normalizePath} from 'flipper-test-utils';
import {mocked} from 'ts-jest/utils';
jest.mock('fs-extra');
jest.mock('../pluginPaths', () => ({ jest.mock('../pluginPaths', () => ({
pluginInstallationDir: '/Users/mock/.flipper/thirdparty', pluginInstallationDir: '/Users/mock/.flipper/thirdparty',
@@ -29,7 +32,6 @@ test('getPluginDetailsV1', async () => {
description: 'Description of Test Plugin', description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test', gatekeeper: 'GK_flipper_plugin_test',
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV1); fs.readJson = jest.fn().mockImplementation(() => pluginV1);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); details.dir = normalizePath(details.dir);
@@ -71,7 +73,6 @@ test('getPluginDetailsV2', async () => {
description: 'Description of Test Plugin', description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test', gatekeeper: 'GK_flipper_plugin_test',
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2); fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); details.dir = normalizePath(details.dir);
@@ -113,7 +114,6 @@ test('id used as title if the latter omited', async () => {
description: 'Description of Test Plugin', description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test', gatekeeper: 'GK_flipper_plugin_test',
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2); fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); 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', description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test', gatekeeper: 'GK_flipper_plugin_test',
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2); fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); details.dir = normalizePath(details.dir);
@@ -198,7 +197,6 @@ test('flipper-plugin-version is parsed', async () => {
'flipper-plugin': '^0.45', 'flipper-plugin': '^0.45',
}, },
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2); fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); details.dir = normalizePath(details.dir);
@@ -246,7 +244,6 @@ test('plugin type and supported devices parsed', async () => {
description: 'Description of Test Plugin', description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test', gatekeeper: 'GK_flipper_plugin_test',
}; };
jest.mock('fs-extra', () => jest.fn());
fs.readJson = jest.fn().mockImplementation(() => pluginV2); fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath); const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir); 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",
}
`);
});

View File

@@ -16,6 +16,16 @@ import {
} from './PluginDetails'; } from './PluginDetails';
import {pluginCacheDir} from './pluginPaths'; 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 function isPluginJson(packageJson: any): boolean { export function isPluginJson(packageJson: any): boolean {
return packageJson?.keywords?.includes('flipper-plugin'); return packageJson?.keywords?.includes('flipper-plugin');
} }
@@ -51,8 +61,7 @@ export async function getInstalledPluginDetails(
dir: string, dir: string,
packageJson?: any, packageJson?: any,
): Promise<InstalledPluginDetails> { ): Promise<InstalledPluginDetails> {
packageJson = packageJson = packageJson ?? (await readPluginPackageJson(dir));
packageJson ?? (await fs.readJson(path.join(dir, 'package.json')));
const pluginDetails = getPluginDetails(packageJson); const pluginDetails = getPluginDetails(packageJson);
const entry = const entry =
pluginDetails.specVersion === 1 pluginDetails.specVersion === 1

View File

@@ -14,7 +14,7 @@ import {getPluginSourceFolders} from './pluginPaths';
import pmap from 'p-map'; import pmap from 'p-map';
import pfilter from 'p-filter'; import pfilter from 'p-filter';
import {satisfies} from 'semver'; import {satisfies} from 'semver';
import {getInstalledPluginDetails, isPluginJson} from './getPluginDetails'; import {getInstalledPluginDetails, isPluginDir} from './getPluginDetails';
import {InstalledPluginDetails} from './PluginDetails'; import {InstalledPluginDetails} from './PluginDetails';
const flipperVersion = require('../package.json').version; const flipperVersion = require('../package.json').version;
@@ -55,55 +55,18 @@ async function entryPointForPluginFolder(
} }
return await fs return await fs
.readdir(pluginsDir) .readdir(pluginsDir)
.then((entries) => .then((entries) => entries.map((name) => path.join(pluginsDir, name)))
entries.map((name) => ({ .then((entries) => pfilter(entries, isPluginDir))
dir: path.join(pluginsDir, name),
manifestPath: path.join(pluginsDir, name, 'package.json'),
})),
)
.then((entries) =>
pfilter(entries, ({manifestPath}) => fs.pathExists(manifestPath)),
)
.then((packages) => .then((packages) =>
pmap(packages, async ({manifestPath, dir}) => { pmap(packages, async (dir) => {
try { try {
const manifest = await fs.readJson(manifestPath); const details = await getInstalledPluginDetails(dir);
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);
if ( if (
details.flipperSDKVersion && details.flipperSDKVersion &&
!satisfies(flipperVersion, details.flipperSDKVersion) !satisfies(flipperVersion, details.flipperSDKVersion)
) { ) {
console.warn( 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; return details;

View File

@@ -66,6 +66,7 @@ async function buildPlugin() {
const outputUnpackedArg = argv['output-unpacked']; const outputUnpackedArg = argv['output-unpacked'];
const minFlipperVersion = argv['min-flipper-version']; const minFlipperVersion = argv['min-flipper-version'];
const packageJsonPath = path.join(pluginDir, 'package.json'); const packageJsonPath = path.join(pluginDir, 'package.json');
const packageJsonOverridePath = path.join(pluginDir, 'fb', 'package.json');
await runBuild(pluginDir, false); await runBuild(pluginDir, false);
const checksum = await computePackageChecksum(pluginDir); const checksum = await computePackageChecksum(pluginDir);
if (previousChecksum !== checksum && argv.version) { if (previousChecksum !== checksum && argv.version) {
@@ -86,7 +87,14 @@ async function buildPlugin() {
const packageJsonBackupPath = path.join(tmpDir, 'package.json'); const packageJsonBackupPath = path.join(tmpDir, 'package.json');
await fs.copy(packageJsonPath, packageJsonBackupPath, {overwrite: true}); await fs.copy(packageJsonPath, packageJsonBackupPath, {overwrite: true});
try { 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 (minFlipperVersion) {
if (!packageJson.engines) { if (!packageJson.engines) {
packageJson.engines = {}; packageJson.engines = {};