Updated to fetch from git-based directory with index.json

This commit is contained in:
2026-04-17 02:02:26 +10:00
parent f423c50d68
commit e486dbbbf8
3 changed files with 151 additions and 12 deletions

View File

@@ -1,9 +1,13 @@
import { EventEmitter } from 'events';
import { readdir, readFile, mkdir } from 'fs/promises';
import { join, resolve } from 'path';
import { join, resolve, dirname } from 'path';
import { existsSync } from 'fs';
import { fileURLToPath } from 'url';
import fetch from 'node-fetch';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Plugin Host - Manages plugin lifecycle and provides plugin API
*/
@@ -11,7 +15,10 @@ export class PluginHost extends EventEmitter {
constructor(options = {}) {
super();
this.directoryUrl = options.directoryUrl;
this.pluginsDir = resolve(options.pluginsDir || './plugins');
// Resolve relative to the PluginHost.js file location (src/), then go up one level
this.pluginsDir = options.pluginsDir ?
(options.pluginsDir.startsWith('.') ? resolve(__dirname, '..', options.pluginsDir) : resolve(options.pluginsDir))
: resolve(__dirname, '../plugins');
this.autoUpdate = options.autoUpdate ?? true;
this.updateInterval = options.updateInterval || 300000;
@@ -42,18 +49,32 @@ export class PluginHost extends EventEmitter {
*/
async updateRegistry() {
try {
const response = await fetch(this.directoryUrl);
if (!response.ok) {
console.warn('Failed to fetch plugin registry:', response.statusText);
// First fetch the index to get the list of available plugins
const indexUrl = `${this.directoryUrl}/index.json`;
const indexResponse = await fetch(indexUrl);
if (!indexResponse.ok) {
console.warn('Failed to fetch plugin index:', indexResponse.statusText);
return;
}
const html = await response.text();
const pluginFiles = this._parseDirectoryListing(html);
const index = await indexResponse.json();
const pluginIds = index.plugins || [];
console.log(`Found ${pluginIds.length} plugins in directory`);
for (const file of pluginFiles) {
if (file.endsWith('.json')) {
await this._fetchPluginDefinition(file);
// Fetch each plugin definition
for (const pluginId of pluginIds) {
const pluginUrl = `${this.directoryUrl}/${pluginId}.json`;
try {
const response = await fetch(pluginUrl);
if (response.ok) {
const plugin = await response.json();
this.pluginRegistry.set(plugin.id, plugin);
console.log(`${plugin.name} v${plugin.version}`);
}
} catch (error) {
console.warn(` ✗ Could not fetch ${pluginId}:`, error.message);
}
}