Move the code related to plugin loading / installation to "flipper-plugin-lib"

Summary:
Sorry for so long diff, but actually there are no functional changes, just refactoring to make further changes of Plugin Manager easier to understand.

I've de-coupled the code related to plugin management from UI code and moved it from PluginInstaller UI component (which will be replaced soon by new UI) to "flipper-plugin-lib".  So pretty much everything related to plugin discovery and installation now consolidated in this package.

Additionally, this refactoring enables re-using of plugin management code in "flipper-pkg", e.g. to create CLI command for plugin installation from NPM, e.g.: `flipper-pkg install flipper-plugin-reactotron`.

Reviewed By: passy

Differential Revision: D23679346

fbshipit-source-id: 82e7b9de9afa08c508c1b228c2038b4ba423571c
This commit is contained in:
Anton Nikolaev
2020-09-16 06:30:20 -07:00
committed by Facebook GitHub Bot
parent 72ff87d7cd
commit e48707151a
18 changed files with 1274 additions and 483 deletions

View File

@@ -0,0 +1,131 @@
/**
* 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
*/
jest.mock('../getInstalledPlugins');
jest.mock('../getNpmHostedPlugins');
import {getUpdatablePlugins} from '../getUpdatablePlugins';
import {
getNpmHostedPlugins,
NpmPackageDescriptor,
} from '../getNpmHostedPlugins';
import type {InstalledPluginDetails} from '../getInstalledPlugins';
import {getInstalledPlugins} from '../getInstalledPlugins';
import {mocked} from 'ts-jest/utils';
import type {Package} from 'npm-api';
jest.mock('npm-api', () => {
return jest.fn().mockImplementation(() => {
return {
repo: jest.fn().mockImplementation((name: string) => {
let pkg: Package | undefined;
if (name === 'flipper-plugin-hello') {
pkg = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-hello',
title: 'Hello',
version: '0.1.0',
main: 'dist/bundle.js',
flipperBundlerEntry: 'src/index.js',
description: 'World?',
};
} else if (name === 'flipper-plugin-world') {
pkg = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-world',
title: 'World',
version: '0.3.0',
main: 'dist/bundle.js',
flipperBundlerEntry: 'src/index.js',
description: 'World?',
};
}
return {
package: jest.fn().mockImplementation(() => Promise.resolve(pkg)),
};
}),
};
});
});
const installedPlugins: InstalledPluginDetails[] = [
{
name: 'flipper-plugin-hello',
entry: './test/index.js',
version: '0.1.0',
specVersion: 2,
main: 'dist/bundle.js',
dir: '/Users/mock/.flipper/thirdparty/flipper-plugin-sample1',
source: 'src/index.js',
id: 'Hello',
title: 'Hello',
description: 'World?',
isDefault: false,
installationStatus: 'installed',
},
{
name: 'flipper-plugin-world',
entry: './test/index.js',
version: '0.2.0',
specVersion: 2,
main: 'dist/bundle.js',
dir: '/Users/mock/.flipper/thirdparty/flipper-plugin-sample2',
source: 'src/index.js',
id: 'World',
title: 'World',
description: 'Hello?',
isDefault: false,
installationStatus: 'pending',
},
];
const updates: NpmPackageDescriptor[] = [
{name: 'flipper-plugin-hello', version: '0.1.0'},
{name: 'flipper-plugin-world', version: '0.3.0'},
];
test('annotatePluginsWithUpdates', async () => {
const getInstalledPluginsMock = mocked(getInstalledPlugins);
getInstalledPluginsMock.mockReturnValue(Promise.resolve(installedPlugins));
const getNpmHostedPluginsMock = mocked(getNpmHostedPlugins);
getNpmHostedPluginsMock.mockReturnValue(Promise.resolve(updates));
const res = await getUpdatablePlugins();
expect(res.length).toBe(2);
expect({
name: res[0].name,
version: res[0].version,
updateStatus: res[0].updateStatus,
}).toMatchInlineSnapshot(`
Object {
"name": "flipper-plugin-hello",
"updateStatus": Object {
"kind": "up-to-date",
},
"version": "0.1.0",
}
`);
expect({
name: res[1].name,
version: res[1].version,
updateStatus: res[1].updateStatus,
}).toMatchInlineSnapshot(`
Object {
"name": "flipper-plugin-world",
"updateStatus": Object {
"kind": "update-available",
"version": "0.3.0",
},
"version": "0.3.0",
}
`);
});