Skip to content

Commit

Permalink
WIP commit, implementing file type plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Oct 7, 2023
1 parent 130e117 commit fb21be0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 41 deletions.
6 changes: 5 additions & 1 deletion src/main/ipc/invokes/getFileTypes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { IpcMainInvokeEvent } from 'electron';
import { PluginConfig } from 'shared/types/plugin';
import project from 'main/project';

// TODO move it to shared types with a better name, Replace similar types with shared one
type FileTypeMap = { [name: string]: PluginConfig };

export default async function getFileTypes(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
event: IpcMainInvokeEvent,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: undefined
): Promise<any[]> {
): Promise<FileTypeMap> {
const { pluginsService } = project;
const fileTypePlugins = pluginsService.getFileTypePlugins();
return fileTypePlugins;
Expand Down
34 changes: 18 additions & 16 deletions src/main/services/pluginsService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { ProjectMessageBroker } from 'main/project/projectMessageBroker';
import project from 'main/project';

import { LogLevel } from 'shared/types';
import { PluginConfig } from 'shared/types/plugin';
import { sanitizePath, tryGetAsync } from 'shared/utils';

import PluginApi, { PluginConfig } from './pluginApi';
import PluginApi from './pluginApi';

import IService from '../IService';

Expand Down Expand Up @@ -56,41 +57,42 @@ class PluginsService implements IService {

async loadPlugin(pluginPath: string): Promise<boolean> {
const directoryName = pluginPath.split('/').pop() || '';
const configPath = `${pluginPath}/package.json`;
if (!(await fsUtils.exists(configPath))) {
const manifestPath = `${pluginPath}/manifest.json`;
if (!(await fsUtils.exists(manifestPath))) {
project.logger.error(
`${directoryName} does not contain a "package.json" file at ${configPath}`,
`${directoryName} does not contain a "manifest.json" file at ${manifestPath}`,
['plugin', directoryName]
);
return false;
}
const configWrapped = await tryGetAsync<string>(() =>
readFile(configPath, 'utf8')
const manifestFileRead = await tryGetAsync<string>(() =>
readFile(manifestPath, 'utf8')
);
if (!configWrapped.success) {
if (!manifestFileRead.success) {
return false;
}
const config = JSON.parse(configWrapped.result as string);
const name = config.name || directoryName;
if (!config.main) {
const manifest = JSON.parse(manifestFileRead.result as string);
const name = manifest.name || directoryName;
if (!manifest.main) {
project.logger.warning(
`${name} plugin, does not have a main file in its "package.json"`,
`${name} plugin, does not have a main file in its "manifest.json"`,
['plugin', name]
);
return false;
}
const mainPath = `${pluginPath}/${config.main}`;
const mainPath = `${pluginPath}/${manifest.main}`;
await this.#runMainScript(name, mainPath);
return true;
}

// TODO: consider better naming
getFileTypePlugins() {
const plugins = this.#plugins;
const result = Object.entries(plugins)
.filter((kv) => kv[1].flowView.fileType !== null)
.map((kv) => kv[0])
.map((key) => plugins[key]);
const result = Object.fromEntries(
Object.entries(plugins)
.filter((kv) => kv[1].flowView.fileType !== null)
.map((kv) => [kv[1].flowView.fileType, kv[1]])
);
return result;
}

Expand Down
24 changes: 6 additions & 18 deletions src/main/services/pluginsService/pluginApi.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
export type ConnectionInfo = {
in: number;
out: number;
};

export type NodeInfo = {
type: string;
connection: ConnectionInfo;
};

export type FlowViewConfig = {
fileType: string;
nodes: NodeInfo[];
};

export type PluginConfig = {
flowView: FlowViewConfig;
};
/* eslint-disable max-classes-per-file */
import { PluginConfig, FlowViewConfig, NodeInfo } from 'shared/types/plugin';

// example node view creation?
// builder.flowView.createNode('node-name', () => {
// react.
// })
// builder.flowView.setFileType('xxx') // *.xxx
// builder.flowView.setNodes(xxx)
// builder.globalShortcut.setShortcutAction(yyy)
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/components/Tab/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export interface TabViewProps {

export default function TabView({ tabId }: TabViewProps) {
const dispatch = useAppDispatch();
const [fileTypes, setFileTypes] = React.useState<string[]>([]);
// TODO use shard type for the state
const [fileTypes, setFileTypes] = React.useState<any>([]);

React.useEffect(() => {
async function getFileTypes() {
Expand All @@ -31,7 +32,7 @@ export default function TabView({ tabId }: TabViewProps) {
);
setFileTypes(result);
}
getFileTypes().catch(e => console.log('eee', e));
getFileTypes().catch((e) => console.log('eee', e));
}, []);

const tabState = useAppSelector(
Expand All @@ -46,7 +47,7 @@ export default function TabView({ tabId }: TabViewProps) {
);
const dispatchFileView = () => {
console.log(fileTypes, 'fileTypes');
console.log(fileTypes[tabData.extension], '??');
console.log('gg', fileTypes[tabData.extension], '??');
if (tabData.extension === 'xflow') {
return <FlowView tabId={tabId} setTabIsDirty={setTabIsDirty} />;
}
Expand Down
2 changes: 0 additions & 2 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { CreateNewProjectResult } from './createNewProjectResult';
import { BrowseFileSystemResult } from './browseFileSystemResult';
import { OpenProjectResult } from './openProjectResult';
import { ProjectTreeNode, ProjectTree } from './projectTree';
import PluginConfig from './pluginConfig';
import Logger from './logger';
import LogLevel from './logLevel';
import LogMessage, { formatLog } from './logMessage';
Expand All @@ -36,7 +35,6 @@ export type {
OpenProjectResult,
ProjectTreeNode,
ProjectTree,
PluginConfig,
Logger,
LogMessage,
};
4 changes: 4 additions & 0 deletions src/shared/types/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import PluginManifest from './pluginManifest';
import { PluginConfig, FlowViewConfig, NodeInfo } from './pluginConfig';

export type { PluginManifest, PluginConfig, FlowViewConfig, NodeInfo };
18 changes: 18 additions & 0 deletions src/shared/types/plugin/pluginConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type ConnectionInfo = {
in: number;
out: number;
};

export type NodeInfo = {
type: string;
connection: ConnectionInfo;
};

export type FlowViewConfig = {
fileType: string;
nodes: NodeInfo[];
};

export type PluginConfig = {
flowView: FlowViewConfig;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface PluginConfig {
export default interface PluginManifest {
name: string;
version: string | undefined;
api: number;
Expand Down

0 comments on commit fb21be0

Please sign in to comment.