Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for importing webapp externally #57

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion webapp/package-lock.json

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

12 changes: 9 additions & 3 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
"build:watch": "webpack --mode=production --watch",
"debug": "webpack --mode=none",
"debug:watch": "webpack --mode=development --watch",
"lint": "eslint --ignore-pattern node_modules --ignore-pattern dist --ext .js --ext .jsx --ext tsx --ext ts . --quiet --cache",
"fix": "eslint --ignore-pattern node_modules --ignore-pattern dist --ext .js --ext .jsx --ext tsx --ext ts . --quiet --fix --cache",
"lint": "eslint --ignore-pattern node_modules --ignore-pattern dist --ignore-pattern postinstall.js --ext .js --ext .jsx --ext tsx --ext ts . --quiet --cache",
"fix": "eslint --ignore-pattern node_modules --ignore-pattern dist --ignore-pattern postinstall.js --ext .js --ext .jsx --ext tsx --ext ts . --quiet --fix --cache",
"test": "jest --forceExit --detectOpenHandles --verbose",
"test:watch": "jest --watch",
"test-ci": "jest --forceExit --detectOpenHandles --maxWorkers=2",
"check-types": "tsc"
"check-types": "tsc",
"postinstall": "node postinstall.js"
},
"customModule": {
"name": "mattermost-plugin-google-calendar",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would actually have this be something like mattermost-plugin-calendar-shared

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the name customModule is arbitrary

"repo": "https://github.com/mattermost/mattermost-plugin-google-calendar.git",
"commit": "webapp-relative-paths"
},
"devDependencies": {
"@babel/cli": "7.16.8",
Expand Down
39 changes: 39 additions & 0 deletions webapp/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {execSync} = require('child_process');
const fs = require('fs');
const path = require('path');

const {chdir} = require('process');

const packageJson = require('./package.json');

const moduleName = packageJson.customModule.name;
const moduleRepo = packageJson.customModule.repo;
const commitHash = packageJson.customModule.commit;
const modulePath = path.join(__dirname, 'node_modules', moduleName);

if (!moduleRepo || !commitHash) {
console.error('Module repo or commit hash not specified in package.json');
process.exit(1);
}

// Clone repo directory if not exists
try {
if (!fs.existsSync(modulePath)) {
execSync(`git clone ${moduleRepo} ${modulePath}`);
console.log(`Successfully cloned ${moduleName} into node_modules.`);
}
} catch (error) {
console.error(`Error cloning repo during postinstall: ${error}`);
process.exit(1);
}

// Checkout the specific commit and npm indtall
try {
chdir(modulePath);
execSync(`git checkout ${commitHash}`);
execSync('cd webapp && npm i');
chdir('../..');
} catch (error) {
console.error(`Error during postinstall: ${error}`);
process.exit(1);
}
60 changes: 3 additions & 57 deletions webapp/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
import React, {useEffect} from 'react';
import Plugin from 'mattermost-plugin-google-calendar/webapp/src/index.tsx';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import the Plugin class from the external repo, rather than defining our own


import {Store, Action} from 'redux';

import {GlobalState} from '@mattermost/types/lib/store';

import {PluginRegistry} from '@/types/mattermost-webapp';

import {PluginId} from './plugin_id';

import Hooks from './plugin_hooks';
import reducer from './reducers';

import CreateEventModal from './components/modals/create_event_modal';
import {getProviderConfiguration, handleConnectChange, openCreateEventModal} from './actions';

// eslint-disable-next-line @typescript-eslint/no-empty-function
export default class Plugin {
public async initialize(registry: PluginRegistry, store: Store<GlobalState, Action<Record<string, unknown>>>) {
registry.registerReducer(reducer);

const hooks = new Hooks(store);
registry.registerSlashCommandWillBePostedHook(hooks.slashCommandWillBePostedHook);

const setup = async () => {
// Retrieve provider configuration so we can access names and other options in messages to use in the frontend.
await store.dispatch(getProviderConfiguration());

registry.registerChannelHeaderMenuAction(
<span>{'Create calendar event'}</span>,
async (channelID) => {
if (await hooks.checkUserIsConnected()) {
store.dispatch(openCreateEventModal(channelID));
}
},
);

registry.registerRootComponent(CreateEventModal);

registry.registerWebSocketEventHandler(`custom_${PluginId}_connected`, handleConnectChange(store));
registry.registerWebSocketEventHandler(`custom_${PluginId}_disconnected`, handleConnectChange(store));
};

registry.registerRootComponent(() => <SetupUI setup={setup}/>);

// reminder to set up site url for any API calls
// and i18n
}
}

const SetupUI = ({setup}) => {
useEffect(() => {
setup();
}, []);

return null;
};
import {id} from '@/manifest';

declare global {
interface Window {
registerPlugin(pluginId: string, plugin: Plugin): void
}
}

window.registerPlugin(PluginId, new Plugin());
window.registerPlugin(id, new Plugin());
2 changes: 1 addition & 1 deletion webapp/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const config = {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
exclude: /node_modules\/(?!mattermost-plugin-google-calendar)/,
use: {
loader: 'babel-loader',
options: {
Expand Down