mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-07-04 06:09:19 +00:00
插件框架
This commit is contained in:
parent
ab6a2ade7d
commit
97b171fe4a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/node_modules/
|
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
12
.idea/crystelf-plugin.iml
generated
Normal file
12
.idea/crystelf-plugin.iml
generated
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.idea/jsLibraryMappings.xml
generated
Normal file
6
.idea/jsLibraryMappings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="JavaScriptLibraryMappings">
|
||||||
|
<includedPredefinedLibrary name="Node.js Core" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/crystelf-plugin.iml" filepath="$PROJECT_DIR$/.idea/crystelf-plugin.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
7
.idea/prettier.xml
generated
Normal file
7
.idea/prettier.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="PrettierConfiguration">
|
||||||
|
<option name="myConfigurationMode" value="AUTOMATIC" />
|
||||||
|
<option name="myRunOnSave" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
7
.prettierrc
Normal file
7
.prettierrc
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"singleQuote": true,
|
||||||
|
"semi": true,
|
||||||
|
"printWidth": 100,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"trailingComma": "es5"
|
||||||
|
}
|
44
components/date.js
Normal file
44
components/date.js
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
let date = {
|
||||||
|
/**
|
||||||
|
* 格式化日期时间
|
||||||
|
* @param {Date|number|string} [date=new Date()] - 可接收Date对象、时间戳或日期字符串
|
||||||
|
* @param {string} [format='YYYY-MM-DD HH:mm:ss'] - 格式模板,支持:
|
||||||
|
* YYYY-年, MM-月, DD-日,
|
||||||
|
* HH-时, mm-分, ss-秒
|
||||||
|
* @returns {string} 格式化后的日期字符串
|
||||||
|
* @example
|
||||||
|
* fc.formatDate(new Date(), 'YYYY年MM月DD日') // "2023年08月15日"
|
||||||
|
*/
|
||||||
|
formatDate(date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') {
|
||||||
|
const d = new Date(date);
|
||||||
|
const pad = (n) => n.toString().padStart(2, '0');
|
||||||
|
|
||||||
|
return format
|
||||||
|
.replace(/YYYY/g, pad(d.getFullYear()))
|
||||||
|
.replace(/MM/g, pad(d.getMonth() + 1))
|
||||||
|
.replace(/DD/g, pad(d.getDate()))
|
||||||
|
.replace(/HH/g, pad(d.getHours()))
|
||||||
|
.replace(/mm/g, pad(d.getMinutes()))
|
||||||
|
.replace(/ss/g, pad(d.getSeconds()));
|
||||||
|
},
|
||||||
|
|
||||||
|
formatDuration(seconds) {
|
||||||
|
const days = Math.floor(seconds / 86400);
|
||||||
|
const hours = Math.floor((seconds % 86400) / 3600);
|
||||||
|
const mins = Math.floor((seconds % 3600) / 60);
|
||||||
|
const secs = seconds % 60;
|
||||||
|
|
||||||
|
return (
|
||||||
|
[
|
||||||
|
days > 0 ? `${days}天` : '',
|
||||||
|
hours > 0 ? `${hours}小时` : '',
|
||||||
|
mins > 0 ? `${mins}分钟` : '',
|
||||||
|
secs > 0 ? `${secs}秒` : '',
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(' ') || '0秒'
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default date;
|
247
components/json.js
Normal file
247
components/json.js
Normal file
@ -0,0 +1,247 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import Version from '../lib/system/version.js';
|
||||||
|
|
||||||
|
const Plugin_Name = Version.name;
|
||||||
|
|
||||||
|
const _path = process.cwd();
|
||||||
|
const getRoot = (root = '') => {
|
||||||
|
if (root === 'root' || root === 'yunzai') {
|
||||||
|
root = `${_path}/`;
|
||||||
|
} else if (!root) {
|
||||||
|
root = `${_path}/plugins/${Plugin_Name}/`;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
|
||||||
|
let fc = {
|
||||||
|
/**
|
||||||
|
* 递归创建目录结构
|
||||||
|
* @param {string} [path=""] - 要创建的相对路径,支持多级目录(如 "dir1/dir2")
|
||||||
|
* @param {string} [root=""] - 基础根目录,可选值:
|
||||||
|
* - "root" 或 "yunzai": 使用 Yunzai 根目录
|
||||||
|
* - 空值: 使用插件目录
|
||||||
|
* @param {boolean} [includeFile=false] - 是否包含最后一级作为文件名
|
||||||
|
* @example
|
||||||
|
* fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录
|
||||||
|
*/
|
||||||
|
createDir(path = '', root = '', includeFile = false) {
|
||||||
|
root = getRoot(root);
|
||||||
|
let pathList = path.split('/');
|
||||||
|
let nowPath = root;
|
||||||
|
pathList.forEach((name, idx) => {
|
||||||
|
name = name.trim();
|
||||||
|
if (!includeFile && idx <= pathList.length - 1) {
|
||||||
|
nowPath += name + '/';
|
||||||
|
if (name) {
|
||||||
|
if (!fs.existsSync(nowPath)) {
|
||||||
|
fs.mkdirSync(nowPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取JSON文件
|
||||||
|
* @param {string} [file=""] - JSON文件路径(相对路径)
|
||||||
|
* @param {string} [root=""] - 基础根目录(同 createDir)
|
||||||
|
* @returns {object} 解析后的JSON对象,如文件不存在或解析失败返回空对象
|
||||||
|
* @example
|
||||||
|
* const config = fc.readJSON("config.json", "root")
|
||||||
|
*/
|
||||||
|
readJSON(file = '', root = '') {
|
||||||
|
root = getRoot(root);
|
||||||
|
if (fs.existsSync(`${root}/${file}`)) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(fs.readFileSync(`${root}/${file}`, 'utf8'));
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
|
||||||
|
statSync(file = '', root = '') {
|
||||||
|
root = getRoot(root);
|
||||||
|
try {
|
||||||
|
return fs.statSync(`${root}/${file}`);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入JSON文件(完全覆盖)
|
||||||
|
* @param {string} file - 目标文件路径
|
||||||
|
* @param {object} data - 要写入的JSON数据
|
||||||
|
* @param {string} [root=""] - 基础根目录(同 createDir)
|
||||||
|
* @param {number} [space=4] - JSON格式化缩进空格数
|
||||||
|
* @returns {boolean} 是否写入成功
|
||||||
|
* @warning 此方法会完全覆盖目标文件原有内容
|
||||||
|
* @example
|
||||||
|
* fc.writeJSON("config.json", {key: "value"}, "root", 4)
|
||||||
|
*/
|
||||||
|
writeJSON(file, data, root = '', space = 4) {
|
||||||
|
fc.createDir(file, root, true);
|
||||||
|
root = getRoot(root);
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space));
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 安全写入JSON文件(合并模式)
|
||||||
|
* @param {string} file - 目标文件路径
|
||||||
|
* @param {object} data - 要合并的数据
|
||||||
|
* @param {string} [root=""] - 基础根目录(同 createDir)
|
||||||
|
* @param {number} [space=4] - JSON格式化缩进空格数
|
||||||
|
* @returns {boolean} 是否写入成功
|
||||||
|
* @description
|
||||||
|
* - 如果目标文件不存在,创建新文件
|
||||||
|
* - 如果目标文件存在,深度合并新旧数据
|
||||||
|
* - 如果目标文件损坏,会创建新文件并记录警告
|
||||||
|
* @example
|
||||||
|
* fc.safewriteJSON("config.json", {newKey: "value"})
|
||||||
|
*/
|
||||||
|
safeWriteJSON(file, data, root = '', space = 4) {
|
||||||
|
fc.createDir(file, root, true);
|
||||||
|
root = getRoot(root);
|
||||||
|
const filePath = `${root}/${file}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let existingData = {};
|
||||||
|
if (fs.existsSync(filePath)) {
|
||||||
|
try {
|
||||||
|
existingData = JSON.parse(fs.readFileSync(filePath, 'utf8')) || {};
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn(`无法解析现有JSON文件 ${filePath},将创建新文件`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mergedData = this.deepMerge(existingData, data);
|
||||||
|
|
||||||
|
fs.writeFileSync(filePath, JSON.stringify(mergedData, null, space));
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`写入JSON文件失败 ${filePath}:`, err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 深度合并两个对象
|
||||||
|
* @param {object} target - 目标对象(将被修改)
|
||||||
|
* @param {object} source - 源对象
|
||||||
|
* @returns {object} 合并后的目标对象
|
||||||
|
* @description
|
||||||
|
* - 递归合并嵌套对象
|
||||||
|
* - 对于非对象属性直接覆盖
|
||||||
|
* - 不会合并数组(数组会被直接覆盖)
|
||||||
|
* @example
|
||||||
|
* const merged = fc.deepMerge({a: 1}, {b: {c: 2}})
|
||||||
|
* // 返回 {a: 1, b: {c: 2}}
|
||||||
|
*/
|
||||||
|
deepMerge(target, source) {
|
||||||
|
for (const key in source) {
|
||||||
|
if (source.hasOwnProperty(key)) {
|
||||||
|
if (
|
||||||
|
source[key] &&
|
||||||
|
typeof source[key] === 'object' &&
|
||||||
|
target[key] &&
|
||||||
|
typeof target[key] === 'object'
|
||||||
|
) {
|
||||||
|
this.deepMerge(target[key], source[key]);
|
||||||
|
} else {
|
||||||
|
target[key] = source[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归读取目录中的特定扩展名文件
|
||||||
|
* @param {string} directory - 要搜索的目录路径
|
||||||
|
* @param {string} extension - 文件扩展名(不带点)
|
||||||
|
* @param {string} [excludeDir] - 要排除的目录名
|
||||||
|
* @returns {string[]} 匹配的文件相对路径数组
|
||||||
|
* @description
|
||||||
|
* - 自动跳过以下划线开头的文件
|
||||||
|
* - 结果包含子目录中的文件
|
||||||
|
* @example
|
||||||
|
* const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules")
|
||||||
|
*/
|
||||||
|
readDirRecursive(directory, extension, excludeDir) {
|
||||||
|
let files = fs.readdirSync(directory);
|
||||||
|
|
||||||
|
let jsFiles = files.filter(
|
||||||
|
(file) => path.extname(file) === `.${extension}` && !file.startsWith('_')
|
||||||
|
);
|
||||||
|
|
||||||
|
files
|
||||||
|
.filter((file) => fs.statSync(path.join(directory, file)).isDirectory())
|
||||||
|
.forEach((subdirectory) => {
|
||||||
|
if (subdirectory === excludeDir) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const subdirectoryPath = path.join(directory, subdirectory);
|
||||||
|
jsFiles.push(
|
||||||
|
...fc
|
||||||
|
.readDirRecursive(subdirectoryPath, extension, excludeDir)
|
||||||
|
.map((fileName) => path.join(subdirectory, fileName))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return jsFiles;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 深度克隆对象(支持基本类型/数组/对象/Date/RegExp)
|
||||||
|
* @param {*} source - 要克隆的数据
|
||||||
|
* @returns {*} 深度克隆后的副本
|
||||||
|
* @description
|
||||||
|
* - 处理循环引用
|
||||||
|
* - 保持原型链
|
||||||
|
* - 支持特殊对象类型(Date/RegExp等)
|
||||||
|
* @example
|
||||||
|
* const obj = { a: 1, b: [2, 3] };
|
||||||
|
* const cloned = fc.deepClone(obj);
|
||||||
|
*/
|
||||||
|
deepClone(source) {
|
||||||
|
const cache = new WeakMap();
|
||||||
|
|
||||||
|
const clone = (value) => {
|
||||||
|
if (value === null || typeof value !== 'object') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cache.has(value)) {
|
||||||
|
return cache.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof Date) return new Date(value);
|
||||||
|
if (value instanceof RegExp) return new RegExp(value);
|
||||||
|
|
||||||
|
const target = new value.constructor();
|
||||||
|
cache.set(value, target);
|
||||||
|
|
||||||
|
for (const key in value) {
|
||||||
|
if (value.hasOwnProperty(key)) {
|
||||||
|
target[key] = clone(value[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return target;
|
||||||
|
};
|
||||||
|
|
||||||
|
return clone(source);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default fc;
|
57
components/module.js
Normal file
57
components/module.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import Version from '../lib/system/version.js';
|
||||||
|
|
||||||
|
const Plugin_Name = Version.name;
|
||||||
|
|
||||||
|
const _path = process.cwd();
|
||||||
|
const getRoot = (root = '') => {
|
||||||
|
if (root === 'root' || root === 'yunzai') {
|
||||||
|
root = `${_path}/`;
|
||||||
|
} else if (!root) {
|
||||||
|
root = `${_path}/plugins/${Plugin_Name}/`;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mc = {
|
||||||
|
/**
|
||||||
|
* 动态导入JS模块
|
||||||
|
* @param {string} file - 模块文件路径(可省略.js后缀)
|
||||||
|
* @param {string} [root=""] - 基础根目录(同 createDir)
|
||||||
|
* @returns {Promise<object>} 模块导出对象,如导入失败返回空对象
|
||||||
|
* @description
|
||||||
|
* - 自动添加时间戳参数防止缓存
|
||||||
|
* - 自动补全.js后缀
|
||||||
|
* @example
|
||||||
|
* const module = await fc.importModule("utils/helper")
|
||||||
|
*/
|
||||||
|
async importModule(file, root = '') {
|
||||||
|
root = getRoot(root);
|
||||||
|
if (!/\.js$/.test(file)) {
|
||||||
|
file = file + '.js';
|
||||||
|
}
|
||||||
|
if (fs.existsSync(`${root}/${file}`)) {
|
||||||
|
try {
|
||||||
|
let data = await import(`file://${root}/${file}?t=${new Date() * 1}`);
|
||||||
|
return data || {};
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态导入JS模块的默认导出
|
||||||
|
* @param {string} file - 模块文件路径
|
||||||
|
* @param {string} [root=""] - 基础根目录(同 createDir)
|
||||||
|
* @returns {Promise<object>} 模块的默认导出,如失败返回空对象
|
||||||
|
* @example
|
||||||
|
* const defaultExport = await fc.importDefault("components/Header")
|
||||||
|
*/
|
||||||
|
async importDefault(file, root) {
|
||||||
|
let ret = await fc.importModule(file, root);
|
||||||
|
return ret.default || {};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default mc;
|
125
components/tool.js
Normal file
125
components/tool.js
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
let tools = {
|
||||||
|
/**
|
||||||
|
* 异步延时函数
|
||||||
|
* @param {number} ms - 等待的毫秒数
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @example
|
||||||
|
* await fc.sleep(1000) // 等待1秒
|
||||||
|
*/
|
||||||
|
sleep(ms) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的随机整数
|
||||||
|
* @param {number} min - 最小值(包含)
|
||||||
|
* @param {number} max - 最大值(包含)
|
||||||
|
* @returns {number} 范围内的随机整数
|
||||||
|
* @example
|
||||||
|
* const randomNum = fc.randomInt(1, 10) // 可能返回 5
|
||||||
|
*/
|
||||||
|
randomInt(min, max) {
|
||||||
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 防抖函数
|
||||||
|
* @param {Function} fn - 要执行的函数
|
||||||
|
* @param {number} [delay=300] - 延迟时间(毫秒)
|
||||||
|
* @param {boolean} [immediate=false] - 是否立即执行
|
||||||
|
* @returns {Function} 防抖处理后的函数
|
||||||
|
* @description
|
||||||
|
* 1. immediate=true时:先立即执行,后续调用在delay时间内被忽略
|
||||||
|
* 2. immediate=false时:延迟执行,重复调用会重置计时器
|
||||||
|
* @example
|
||||||
|
* window.addEventListener('resize', fc.debounce(() => {
|
||||||
|
* console.log('resize end');
|
||||||
|
* }, 500));
|
||||||
|
*/
|
||||||
|
debounce(fn, delay = 300, immediate = false) {
|
||||||
|
let timer = null;
|
||||||
|
return function (...args) {
|
||||||
|
if (immediate && !timer) {
|
||||||
|
fn.apply(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
if (!immediate) {
|
||||||
|
fn.apply(this, args);
|
||||||
|
}
|
||||||
|
timer = null;
|
||||||
|
}, delay);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步重试机制
|
||||||
|
* @param {Function} asyncFn - 返回Promise的异步函数
|
||||||
|
* @param {number} [maxRetries=3] - 最大重试次数
|
||||||
|
* @param {number} [delay=1000] - 重试间隔(毫秒)
|
||||||
|
* @param {Function} [retryCondition] - 重试条件函数(err => boolean)
|
||||||
|
* @returns {Promise} 最终成功或失败的结果
|
||||||
|
* @example
|
||||||
|
* await fc.retry(fetchData, 5, 2000, err => err.status !== 404);
|
||||||
|
*/
|
||||||
|
async retry(asyncFn, maxRetries = 3, delay = 1000, retryCondition = () => true) {
|
||||||
|
let attempt = 0;
|
||||||
|
let lastError;
|
||||||
|
|
||||||
|
while (attempt <= maxRetries) {
|
||||||
|
try {
|
||||||
|
return await asyncFn();
|
||||||
|
} catch (err) {
|
||||||
|
lastError = err;
|
||||||
|
if (attempt === maxRetries || !retryCondition(err)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
attempt++;
|
||||||
|
await this.sleep(delay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw lastError;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象转换为URL查询字符串
|
||||||
|
* @param {object} params - 参数对象
|
||||||
|
* @param {boolean} [encode=true] - 是否进行URL编码
|
||||||
|
* @returns {string} 查询字符串(不带问号)
|
||||||
|
* @example
|
||||||
|
* fc.objectToQuery({a: 1, b: 'test'}) // "a=1&b=test"
|
||||||
|
*/
|
||||||
|
objectToQuery(params, encode = true) {
|
||||||
|
return Object.entries(params)
|
||||||
|
.map(([key, val]) => {
|
||||||
|
const value = val === null || val === undefined ? '' : val;
|
||||||
|
return `${key}=${encode ? encodeURIComponent(value) : value}`;
|
||||||
|
})
|
||||||
|
.join('&');
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从错误堆栈中提取简洁的错误信息
|
||||||
|
* @param {Error} error - 错误对象
|
||||||
|
* @param {number} [depth=3] - 保留的堆栈深度
|
||||||
|
* @returns {string} 格式化后的错误信息
|
||||||
|
* @example
|
||||||
|
* try { ... } catch(err) {
|
||||||
|
* logger.error(fc.formatError(err));
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
formatError(error, depth = 3) {
|
||||||
|
if (!(error instanceof Error)) return String(error);
|
||||||
|
|
||||||
|
const stack = error.stack?.split('\n') || [];
|
||||||
|
const message = `${error.name}: ${error.message}`;
|
||||||
|
|
||||||
|
if (stack.length <= 1) return message;
|
||||||
|
|
||||||
|
return [message, ...stack.slice(1, depth + 1).map((line) => line.trim())].join('\n at ');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default tools;
|
21
constants/path.js
Normal file
21
constants/path.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import url from 'url';
|
||||||
|
|
||||||
|
const __filename = url.fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
const rootDir = path.join(__dirname, '..');
|
||||||
|
|
||||||
|
const Path = {
|
||||||
|
root: rootDir,
|
||||||
|
apps: path.join(rootDir, 'apps'),
|
||||||
|
components: path.join(rootDir, 'components'),
|
||||||
|
config: path.join(rootDir, 'config'),
|
||||||
|
constants: path.join(rootDir, 'constants'),
|
||||||
|
lib: path.join(rootDir, 'lib'),
|
||||||
|
models: path.join(rootDir, 'models'),
|
||||||
|
index: path.join(rootDir, 'index.js'),
|
||||||
|
pkg: path.join(rootDir, 'package.json'),
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Path;
|
10
eslint.config.js
Normal file
10
eslint.config.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { defineConfig } from "eslint/config";
|
||||||
|
import globals from "globals";
|
||||||
|
import js from "@eslint/js";
|
||||||
|
|
||||||
|
|
||||||
|
export default defineConfig([
|
||||||
|
{ files: ["**/*.{js,mjs,cjs}"] },
|
||||||
|
{ files: ["**/*.{js,mjs,cjs}"], languageOptions: { globals: globals.browser } },
|
||||||
|
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"] },
|
||||||
|
]);
|
35
index.js
Normal file
35
index.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import chalk from 'chalk';
|
||||||
|
import Version from './lib/system/version.js';
|
||||||
|
import fc from './components/json.js';
|
||||||
|
import Path from './constants/path.js';
|
||||||
|
import { crystelfInit } from './lib/system/init.js';
|
||||||
|
import updater from './lib/system/updater.js';
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
chalk.rgb(134, 142, 204)(`crystelf-plugin ${Version.ver} 初始化 ~ by ${Version.author}`)
|
||||||
|
);
|
||||||
|
|
||||||
|
updater.checkAndUpdate();
|
||||||
|
crystelfInit.CSH();
|
||||||
|
|
||||||
|
const appPath = Path.apps;
|
||||||
|
const jsFiles = fc.readDirRecursive(appPath, 'js');
|
||||||
|
|
||||||
|
let ret = jsFiles.map((file) => {
|
||||||
|
return import(`./apps/${file}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
ret = await Promise.allSettled(ret);
|
||||||
|
|
||||||
|
let apps = {};
|
||||||
|
for (let i in jsFiles) {
|
||||||
|
let name = jsFiles[i].replace('.js', '');
|
||||||
|
|
||||||
|
if (ret[i].status !== 'fulfilled') {
|
||||||
|
logger.error(name, ret[i].reason);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
apps[name] = ret[i].value[Object.keys(ret[i].value)[0]];
|
||||||
|
}
|
||||||
|
|
||||||
|
export { apps };
|
8
lib/system/init.js
Normal file
8
lib/system/init.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import Path from '../../constants/path.js';
|
||||||
|
|
||||||
|
export const crystelfInit = {
|
||||||
|
CSH: () => {
|
||||||
|
logger.info(Path.root);
|
||||||
|
logger.mark('crystelf 完成初始化');
|
||||||
|
},
|
||||||
|
};
|
73
lib/system/updater.js
Normal file
73
lib/system/updater.js
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import child_process from 'child_process';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
import Path from '../../constants/path.js';
|
||||||
|
|
||||||
|
const GIT_DIR = path.join(Path.root, '.git');
|
||||||
|
|
||||||
|
const execStr = (cmd) => child_process.execSync(cmd, { cwd: Path.root }).toString().trim();
|
||||||
|
|
||||||
|
const Updater = {
|
||||||
|
isGitRepo() {
|
||||||
|
return fs.existsSync(GIT_DIR);
|
||||||
|
},
|
||||||
|
|
||||||
|
getBranch() {
|
||||||
|
return execStr('git symbolic-ref --short HEAD');
|
||||||
|
},
|
||||||
|
|
||||||
|
getLocalHash() {
|
||||||
|
return execStr('git rev-parse HEAD');
|
||||||
|
},
|
||||||
|
|
||||||
|
getRemoteHash(branch = 'main') {
|
||||||
|
return execStr(`git rev-parse origin/${branch}`);
|
||||||
|
},
|
||||||
|
|
||||||
|
async hasUpdate() {
|
||||||
|
try {
|
||||||
|
const branch = this.getBranch();
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
child_process.exec('git fetch', { cwd: Path.root }, (err) => {
|
||||||
|
if (err) reject(err);
|
||||||
|
else resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const local = this.getLocalHash();
|
||||||
|
const remote = this.getRemoteHash(branch);
|
||||||
|
|
||||||
|
return local !== remote;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('[crystelf-plugin] 检查更新失败:', err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async update() {
|
||||||
|
logger.mark(chalk.cyan('[crystelf-plugin] 检测到插件有更新,自动执行 git pull'));
|
||||||
|
child_process.execSync('git pull', { cwd: Path.root, stdio: 'inherit' });
|
||||||
|
logger.mark(chalk.green('[crystelf-plugin] 插件已自动更新完成'));
|
||||||
|
},
|
||||||
|
|
||||||
|
async checkAndUpdate() {
|
||||||
|
if (!this.isGitRepo()) {
|
||||||
|
logger.warn('[crystelf-plugin] 当前目录不是 Git 仓库,自动更新功能已禁用');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (await this.hasUpdate()) {
|
||||||
|
await this.update();
|
||||||
|
} else {
|
||||||
|
logger.info('[crystelf-plugin] 当前已是最新版本,无需更新');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('[crystelf-plugin] 自动更新失败:', err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Updater;
|
26
lib/system/version.js
Normal file
26
lib/system/version.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import url from 'url';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
const __filename = url.fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
const pkgPath = path.join(__dirname, '../..', 'package.json');
|
||||||
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
||||||
|
|
||||||
|
const Version = {
|
||||||
|
get ver() {
|
||||||
|
return pkg.version;
|
||||||
|
},
|
||||||
|
get author() {
|
||||||
|
return pkg.author;
|
||||||
|
},
|
||||||
|
get name() {
|
||||||
|
return pkg.name;
|
||||||
|
},
|
||||||
|
get description() {
|
||||||
|
return pkg.description;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Version;
|
29
package.json
Normal file
29
package.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "crystelf-plugin",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "适配crystelf-core的Yunzai插件",
|
||||||
|
"main": "index.js",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": ""
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"TRSS-Yunzai",
|
||||||
|
"crystelf-plugin"
|
||||||
|
],
|
||||||
|
"author": "Jerry",
|
||||||
|
"License": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"chalk": "^5.4.1",
|
||||||
|
"openai": "^4.89.0"
|
||||||
|
},
|
||||||
|
"imports": {},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/js": "^9.23.0",
|
||||||
|
"eslint": "^8.57.1",
|
||||||
|
"globals": "^16.0.0",
|
||||||
|
"prettier": "^3.5.3"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user