Some checks failed
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
96 lines
2.5 KiB
JavaScript
96 lines
2.5 KiB
JavaScript
"use strict";
|
||
|
||
/**
|
||
* 采集器注册表
|
||
*
|
||
* 自动注册 adapters 目录下的所有采集器,供调度器使用。
|
||
*
|
||
* 使用方式:
|
||
* const adapterRegistry = require("./adapterRegistry.cjs");
|
||
* const adapter = adapterRegistry.createAdapter("LinuxServerAdapter", connInfo, options);
|
||
*/
|
||
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
const BaseAdapter = require("./adapters/baseAdapter.cjs");
|
||
|
||
const ADAPTERS_DIR = path.join(__dirname, "adapters");
|
||
const registry = {};
|
||
|
||
/**
|
||
* 扫描 adapters 目录,自动注册所有采集器类
|
||
* (只注册已实现的文件,不存在的适配器不报错,留作后续扩展)
|
||
*/
|
||
function loadAdapters() {
|
||
if (!fs.existsSync(ADAPTERS_DIR)) return;
|
||
|
||
const files = fs.readdirSync(ADAPTERS_DIR);
|
||
|
||
for (const file of files) {
|
||
if (!file.endsWith(".cjs")) continue;
|
||
if (file === "baseAdapter.cjs") continue;
|
||
|
||
try {
|
||
const modulePath = path.join(ADAPTERS_DIR, file);
|
||
const AdapterClass = require(modulePath);
|
||
|
||
// 验证是否继承自 BaseAdapter
|
||
if (
|
||
typeof AdapterClass === "function" &&
|
||
AdapterClass.prototype instanceof BaseAdapter
|
||
) {
|
||
console.log(`[Inspection] 注册采集器: ${AdapterClass.name} (${file})`);
|
||
registry[AdapterClass.name] = AdapterClass;
|
||
} else {
|
||
console.warn(`[Inspection] ${file} 不是 BaseAdapter 子类,导出类型: ${typeof AdapterClass}`);
|
||
}
|
||
} catch (e) {
|
||
// 加载失败跳过(比如引用了不存在的模块)
|
||
console.error(`[Inspection] 加载采集器失败: ${file}`, e.message, e.stack);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 启动时自动加载
|
||
loadAdapters();
|
||
|
||
/**
|
||
* 获取已注册的采集器列表
|
||
* @returns {string[]} 采集器类名列表
|
||
*/
|
||
function getRegisteredAdapters() {
|
||
return Object.keys(registry);
|
||
}
|
||
|
||
/**
|
||
* 检查采集器是否已注册
|
||
* @param {string} className
|
||
* @returns {boolean}
|
||
*/
|
||
function hasAdapter(className) {
|
||
return !!registry[className];
|
||
}
|
||
|
||
/**
|
||
* 创建采集器实例
|
||
* @param {string} className - 采集器类名(如 "LinuxServerAdapter")
|
||
* @param {object} connectionInfo - SSH 连接信息
|
||
* @param {object} [options] - 配置项
|
||
* @returns {BaseAdapter|null}
|
||
*/
|
||
function createAdapter(className, connectionInfo, options = {}) {
|
||
const AdapterClass = registry[className];
|
||
if (!AdapterClass) {
|
||
console.warn(`[Inspection] 未找到采集器: ${className}`);
|
||
return null;
|
||
}
|
||
return new AdapterClass(connectionInfo, options);
|
||
}
|
||
|
||
module.exports = {
|
||
loadAdapters,
|
||
getRegisteredAdapters,
|
||
hasAdapter,
|
||
createAdapter,
|
||
};
|