Skip to content

Commit

Permalink
chore(compass-shell): convert to new plugin interface COMPASS-7399 (#…
Browse files Browse the repository at this point in the history
…5086)

Co-authored-by: Sergey Petushkov <[email protected]>
  • Loading branch information
addaleax and gribnoysup authored Nov 10, 2023
1 parent d61268a commit 072d079
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 159 deletions.
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/compass-home/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@mongodb-js/compass-databases-collections": "^1.19.1",
"@mongodb-js/compass-find-in-page": "^4.19.1",
"@mongodb-js/compass-import-export": "^7.19.1",
"@mongodb-js/compass-shell": "^3.19.1",
"compass-preferences-model": "^2.15.6",
"hadron-app-registry": "^9.0.14",
"hadron-ipc": "^3.2.4",
Expand All @@ -62,6 +63,7 @@
"@mongodb-js/compass-databases-collections": "^1.19.1",
"@mongodb-js/compass-find-in-page": "^4.19.1",
"@mongodb-js/compass-import-export": "^7.19.1",
"@mongodb-js/compass-shell": "^3.19.1",
"compass-preferences-model": "^2.15.6",
"hadron-app-registry": "^9.0.14",
"hadron-ipc": "^3.2.4",
Expand Down
22 changes: 21 additions & 1 deletion packages/compass-home/src/components/home.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ const getComponent = (name: string) => {
return TestComponent;
};

const createDataService = () => ({});
const createDataService = () => ({
getConnectionString() {
return { hosts: ['localhost:27020'] };
},
getConnectionOptions() {
return {};
},
getMongoClientConnectionOptions() {
return {};
},
getLastSeenTopology() {
return {
type: 'Unknown',
servers: [],
setName: 'foo',
};
},
on() {},
off() {},
});

describe('Home [Component]', function () {
before(function () {
Expand Down Expand Up @@ -112,6 +131,7 @@ describe('Home [Component]', function () {
);
dataServiceDisconnectedSpy = sinon.fake.resolves(true);
const dataService = {
...createDataService(),
disconnect: dataServiceDisconnectedSpy,
addReauthenticationHandler: sinon.stub(),
};
Expand Down
4 changes: 2 additions & 2 deletions packages/compass-home/src/components/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from '@mongodb-js/compass-components';
import WorkspaceContent from './workspace-content';
import type Namespace from '../types/namespace';
import { useAppRegistryComponent } from 'hadron-app-registry';
import { CompassShellPlugin } from '@mongodb-js/compass-shell';

const verticalSplitStyles = css({
width: '100vw',
Expand Down Expand Up @@ -40,7 +41,6 @@ export default function Workspace({
namespace: Namespace;
}): React.ReactElement {
const SidebarComponent = useAppRegistryComponent('Sidebar.Component');
const GlobalShellComponent = useAppRegistryComponent('Global.Shell');

return (
<>
Expand All @@ -54,7 +54,7 @@ export default function Workspace({
</div>
</div>
<div className={shellContainerStyles}>
{GlobalShellComponent && <GlobalShellComponent />}
<CompassShellPlugin />
</div>
</div>
</>
Expand Down
13 changes: 8 additions & 5 deletions packages/compass-shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"main": "dist/index.js",
"compass:main": "src/index.ts",
"types": "dist/src/index.d.ts",
"exports": {
"browser": "./dist/browser.js",
"require": "./dist/index.js"
Expand All @@ -33,9 +34,11 @@
".": "./src/index.ts"
},
"scripts": {
"bootstrap": "npm run postcompile",
"prepublishOnly": "npm run compile && compass-scripts check-exports-exist",
"compile": "npm run webpack -- --mode production",
"webpack": "webpack-compass",
"postcompile": "tsc --emitDeclarationOnly",
"start": "npm run webpack serve -- --mode development",
"analyze": "npm run webpack -- --mode production --analyze",
"typecheck": "tsc --noEmit",
Expand All @@ -58,18 +61,20 @@
"@mongodb-js/compass-components": "^1.19.0",
"@mongodb-js/compass-logging": "^1.2.6",
"@mongodb-js/compass-utils": "^0.5.5",
"@mongodb-js/mongodb-redux-common": "^2.0.15",
"@mongosh/node-runtime-worker-thread": "^2.0.2",
"compass-preferences-model": "^2.15.6"
"compass-preferences-model": "^2.15.6",
"hadron-app-registry": "^9.0.14",
"mongodb-data-service": "^22.15.1"
},
"peerDependencies": {
"@mongodb-js/compass-user-data": "^0.1.9",
"@mongodb-js/compass-components": "^1.19.0",
"@mongodb-js/compass-logging": "^1.2.6",
"@mongodb-js/compass-utils": "^0.5.5",
"@mongodb-js/mongodb-redux-common": "^2.0.15",
"@mongosh/node-runtime-worker-thread": "^2.0.2",
"compass-preferences-model": "^2.15.6",
"hadron-app-registry": "^9.0.14",
"mongodb-data-service": "^22.15.1",
"react": "^17.0.2"
},
"devDependencies": {
Expand All @@ -85,9 +90,7 @@
"electron": "^25.9.3",
"enzyme": "^3.11.0",
"eslint": "^7.25.0",
"hadron-app-registry": "^9.0.14",
"mocha": "^10.2.0",
"mongodb-data-service": "^22.15.0",
"nyc": "^15.1.0",
"prop-types": "^15.7.2",
"react-dom": "^17.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ export class CompassShell extends Component<

export default connect((state: RootState) => ({
emitShellPluginOpened: () => {
if (state.appRegistry && state.appRegistry.globalAppRegistry) {
state.appRegistry.globalAppRegistry.emit('compass:compass-shell:opened');
}
state.runtime.appRegistry?.emit('compass:compass-shell:opened');
},
runtime: state.runtime ? state.runtime.runtime : null,
}))(withPreferences(CompassShell, ['enableShell'], React));
46 changes: 25 additions & 21 deletions packages/compass-shell/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import type AppRegistry from 'hadron-app-registry';
import type { LoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
import { createLoggerAndTelemetryLocator } from '@mongodb-js/compass-logging/provider';
import { ShellPlugin, onActivated } from './plugin';
import { registerHadronPlugin } from 'hadron-app-registry';
import { dataServiceLocator } from 'mongodb-data-service/provider';
import type { DataService } from 'mongodb-data-service';

import createPlugin from './plugin';

const { store, Plugin } = createPlugin();

/**
* Activate all the components in the Compass Shell package.
* @param {Object} appRegistry - The appRegistry to activate this plugin with.
**/
function activate(appRegistry: AppRegistry): void {
// Register the shell plugin's role in Compass.
appRegistry.registerComponent('Global.Shell', Plugin);
appRegistry.registerStore('CompassShell.Store', store);
function activate(): void {
// noop
}

/**
* Deactivate all the components in the Compass Shell package.
* @param {Object} appRegistry - The appRegistry to deactivate this plugin with.
**/
function deactivate(appRegistry: AppRegistry): void {
appRegistry.registerComponent('Global.Shell', Plugin);
appRegistry.deregisterStore('CompassShell.Store');
function deactivate(): void {
// noop
}

export default Plugin;
export const CompassShellPlugin = registerHadronPlugin<
unknown,
{ logger: () => LoggerAndTelemetry; dataService: () => DataService }
>(
{
name: 'CompassShell',
component: ShellPlugin,
activate: onActivated,
},
{
logger: createLoggerAndTelemetryLocator('COMPASS-SHELL'),
dataService: dataServiceLocator,
}
);

export { activate, deactivate };
export { default as metadata } from '../package.json';
7 changes: 0 additions & 7 deletions packages/compass-shell/src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { combineReducers } from 'redux';
import appRegistry from '@mongodb-js/mongodb-redux-common/app-registry';
import type { AppRegistry } from 'hadron-app-registry';
import type { RuntimeAction, RuntimeState } from './runtime';
import runtime from './runtime';

export interface RootState {
appRegistry: {
globalAppRegistry: AppRegistry | null;
localAppRegistry: AppRegistry | null;
};
runtime: RuntimeState;
}

export type RootAction = RuntimeAction;

const reducer = combineReducers({
appRegistry,
runtime,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import type { ReactWrapper } from 'enzyme';
import { mount } from 'enzyme';
import { EventEmitter } from 'events';
import { expect } from 'chai';

import { CompassShell } from './components/compass-shell';
import createPlugin from './plugin';
import CompassShellStore from './stores';
import { CompassShellPlugin } from './index';
import { AppRegistryProvider, globalAppRegistry } from 'hadron-app-registry';
import { DataServiceProvider } from 'mongodb-data-service/provider';

// Wait until a component is present that is rendered in a limited number
// of microtask queue iterations. In particular, this does *not* wait for the
Expand All @@ -26,35 +27,51 @@ async function waitForAsyncComponent(wrapper, Component, attempts = 10) {
}

describe('CompassShellPlugin', function () {
it('returns a renderable plugin', async function () {
const { Plugin } = createPlugin();
const fakeDataService = {
getMongoClientConnectionOptions() {},
} as any;
let wrapper: ReactWrapper | null;

const wrapper = mount(<Plugin />);
afterEach(() => {
wrapper?.unmount();
wrapper = null;
});
it('returns a renderable plugin', async function () {
wrapper = mount(
<AppRegistryProvider>
{/* global */}
<AppRegistryProvider>
{/* local */}
<DataServiceProvider value={fakeDataService}>
<CompassShellPlugin />
</DataServiceProvider>
</AppRegistryProvider>
</AppRegistryProvider>
);

const component = await waitForAsyncComponent(wrapper, CompassShell);

expect(component.exists()).to.equal(true);
});

it('returns a CompassShellStore store', function () {
const { store } = createPlugin();
const appRegistry = new EventEmitter();
store.onActivated(appRegistry);
expect(store).to.be.instanceOf(CompassShellStore);
});

it('emits an event on the app registry when it is expanded', async function () {
const { store, Plugin } = createPlugin();

const appRegistry = new EventEmitter();
let eventOccured = false;
appRegistry.on('compass:compass-shell:opened', () => {
globalAppRegistry.on('compass:compass-shell:opened', () => {
eventOccured = true;
});

store.onActivated(appRegistry);
wrapper = mount(
<AppRegistryProvider>
{/* global */}
<AppRegistryProvider>
{/* local */}
<DataServiceProvider value={fakeDataService}>
<CompassShellPlugin />
</DataServiceProvider>
</AppRegistryProvider>
</AppRegistryProvider>
);

const wrapper = mount(<Plugin />);
const shellComponentWrapper = await waitForAsyncComponent(
wrapper,
CompassShell
Expand Down
Loading

0 comments on commit 072d079

Please sign in to comment.