Skip to content

Commit

Permalink
Merge pull request #1 from Youssefbaghr/feature/plugin-system
Browse files Browse the repository at this point in the history
Implement plugin system and add documentation
  • Loading branch information
Youssefbaghr authored Aug 31, 2024
2 parents 1123930 + 22f3101 commit 9b144bd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4,814 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,49 @@ Example configuration:
```
3. Add the template using `clixor template --add`

### Creating Plugins

Clixor supports a plugin system that allows you to extend its functionality. Here's how to create a
plugin:

1. Create a new TypeScript file in the `src/plugins` directory.
2. Import the `PluginInterface` from `src/plugins/types/index.ts`.
3. Create a class that implements the `PluginInterface`.
4. Implement the required methods:
- `name`: A string identifier for your plugin.
- `version`: The version of your plugin.
- `initialize`: A method called when the plugin is registered.
- `execute`: The main functionality of your plugin.

Example plugin:

```typescript
import { PluginInterface } from '../types';
export class MyCustomPlugin implements PluginInterface {
name = 'MyCustomPlugin';
version = '1.0.0';
initialize(): void {
console.log('MyCustomPlugin initialized');
}
async execute(context: any): Promise<string> {
return MyCustomPlugin executed with context: ${JSON.stringify(context)};
}
}
```

To use your plugin, register it in the `src/utils/plugins.ts` file:

```typescript
import { MyCustomPlugin } from '../plugins/MyCustomPlugin';
// In the loadPlugins function
const myCustomPlugin = new MyCustomPlugin();
myCustomPlugin.initialize();
// Add logic to use the plugin as needed
```

Plugins allow you to add new commands, modify existing functionality, or integrate with external
services to enhance Clixor's capabilities.

## 🤝 Contributing

We love our contributors! ❤️ Check out the [CONTRIBUTORS.md](CONTRIBUTORS.md) file to see the
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PluginInterface } from './types';

class PluginManager {
private plugins: Map<string, PluginInterface> = new Map();

registerPlugin(plugin: PluginInterface): void {
this.plugins.set(plugin.name, plugin);
plugin.initialize();
}

async executePlugin(name: string, context: any): Promise<any> {
const plugin = this.plugins.get(name);
if (!plugin) {
throw new Error(`Plugin "${name}" not found`);
}
return await plugin.execute(context);
}

getPlugins(): PluginInterface[] {
return Array.from(this.plugins.values());
}
}

export const pluginManager = new PluginManager();
6 changes: 6 additions & 0 deletions src/plugins/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface PluginInterface {
name: string;
version: string;
initialize: () => void;
execute: (context: any) => Promise<any>;
}
Loading

0 comments on commit 9b144bd

Please sign in to comment.