build(desktop): 升级 electron-shell 版本至 1.6.3 并调整插件执行超时配置
- 插件执行超时默认延长至 6 小时,支持插件单独配置超时时间 - 新增插件根目录迁移逻辑,解决旧版权限问题 - 新增多个服务端实体、DTO、配置模块等基础代码 - 新增插件 UI 模板及相关静态资源
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import * as md5 from 'md5';
|
||||
import * as fs from 'fs';
|
||||
|
||||
/**
|
||||
* 获得配置文件中的 keys
|
||||
* @returns
|
||||
*/
|
||||
const getKeys = () => {
|
||||
const configFile = path.join(__dirname, '../config/config.default.js');
|
||||
const configContent = fs.readFileSync(configFile, 'utf8');
|
||||
const keys = configContent.match(/keys: '([^']+)'/)?.[1];
|
||||
return keys;
|
||||
};
|
||||
|
||||
/**
|
||||
* 项目数据目录
|
||||
* @returns
|
||||
*/
|
||||
export const pDataPath = () => {
|
||||
const dirPath = path.join(os.homedir(), '.cool-admin', md5(getKeys()));
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
return dirPath;
|
||||
};
|
||||
|
||||
/**
|
||||
* 上传目录
|
||||
* @returns
|
||||
*/
|
||||
export const pUploadPath = () => {
|
||||
const uploadPath = path.join(pDataPath(), 'upload');
|
||||
if (!fs.existsSync(uploadPath)) {
|
||||
fs.mkdirSync(uploadPath, { recursive: true });
|
||||
}
|
||||
return uploadPath;
|
||||
};
|
||||
|
||||
/**
|
||||
* 插件目录
|
||||
* @returns
|
||||
*/
|
||||
export const pPluginPath = () => {
|
||||
const pluginPath = path.join(pDataPath(), 'plugin');
|
||||
if (!fs.existsSync(pluginPath)) {
|
||||
fs.mkdirSync(pluginPath, { recursive: true });
|
||||
}
|
||||
return pluginPath;
|
||||
};
|
||||
|
||||
/**
|
||||
* sqlite 数据库文件
|
||||
*/
|
||||
export const pSqlitePath = () => {
|
||||
return path.join(pDataPath(), 'cool.sqlite');
|
||||
};
|
||||
|
||||
/**
|
||||
* 缓存目录
|
||||
* @returns
|
||||
*/
|
||||
export const pCachePath = () => {
|
||||
return path.join(pDataPath(), 'cache');
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
/**
|
||||
* 同步检查端口是否可用(通过系统命令)
|
||||
* @param {number} port - 要检查的端口
|
||||
* @returns {boolean} - 是否可用
|
||||
*/
|
||||
function isPortAvailableSync(port: number): boolean {
|
||||
try {
|
||||
if (process.platform === 'win32') {
|
||||
// Windows 使用 netstat 检查端口,排除 TIME_WAIT 状态
|
||||
const result = execSync(`netstat -ano | findstr :${port}`, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
// 如果端口只处于 TIME_WAIT 状态,则认为端口可用
|
||||
return !result || result.toLowerCase().includes('time_wait');
|
||||
} else {
|
||||
// Linux/Mac 使用 lsof 检查端口,只检查 LISTEN 状态
|
||||
const result = execSync(`lsof -i :${port} -sTCP:LISTEN`, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
return !result;
|
||||
}
|
||||
} catch (error) {
|
||||
// 命令执行失败,端口可用
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找可用端口(同步)
|
||||
* @param {number} startPort - 起始端口
|
||||
* @returns {number} - 可用的端口
|
||||
*/
|
||||
export function availablePort(startPort: number): number {
|
||||
if (!process['pkg']) return startPort;
|
||||
let port = startPort;
|
||||
while (port <= 8010) {
|
||||
if (isPortAvailableSync(port)) {
|
||||
if (port !== startPort) {
|
||||
console.warn(
|
||||
'\x1b[33m%s\x1b[0m',
|
||||
`Port ${startPort} is occupied, using port ${port}`
|
||||
);
|
||||
}
|
||||
return port;
|
||||
}
|
||||
port++;
|
||||
}
|
||||
return 8001;
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import * as moment from 'moment';
|
||||
import * as path from 'path';
|
||||
|
||||
/**
|
||||
* 帮助类
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class Utils {
|
||||
@Inject()
|
||||
baseDir;
|
||||
|
||||
/**
|
||||
* 获得dist路径
|
||||
*/
|
||||
getDistPath() {
|
||||
const runPath = __dirname;
|
||||
const distIndex =
|
||||
runPath.lastIndexOf('/dist/') !== -1
|
||||
? runPath.lastIndexOf('/dist/')
|
||||
: runPath.lastIndexOf('\\dist\\');
|
||||
if (distIndex !== -1) {
|
||||
return path.join(runPath.substring(0, distIndex), 'dist');
|
||||
}
|
||||
return path.join(runPath, 'dist');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得请求IP
|
||||
*/
|
||||
async getReqIP(ctx: Context) {
|
||||
const req = ctx.req;
|
||||
return (
|
||||
req.headers['x-forwarded-for'] ||
|
||||
req.socket.remoteAddress?.replace('::ffff:', '') ||
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除对象的空值属性
|
||||
* @param obj
|
||||
*/
|
||||
async removeEmptyP(obj) {
|
||||
Object.keys(obj).forEach(key => {
|
||||
if (obj[key] === null || obj[key] === '' || obj[key] === 'undefined') {
|
||||
delete obj[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 线程阻塞毫秒数
|
||||
* @param ms
|
||||
*/
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得最近几天的日期集合
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyDates(recently, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment().subtract(i, 'days').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
/**
|
||||
* 获得最近几个月的月数
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyMonths(recently, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const date = moment(Date.now()).format('YYYY-MM');
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment(date).subtract(i, 'months').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的日期集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenDays(start, end, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const days = moment(endTime).diff(moment(startTime), 'days');
|
||||
for (let i = 0; i <= days; i++) {
|
||||
dates.push(moment(startTime).add(i, 'days').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的月份集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenMonths(start, end, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const months = moment(endTime).diff(moment(startTime), 'months');
|
||||
for (let i = 0; i <= months; i++) {
|
||||
dates.push(moment(startTime).add(i, 'months').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的小时集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenHours(start, end, format = 'YYYY-MM-DD HH') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const hours = moment(endTime).diff(moment(startTime), 'hours');
|
||||
for (let i = 0; i <= hours; i++) {
|
||||
dates.push(moment(startTime).add(i, 'hours').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段转驼峰法
|
||||
* @param obj
|
||||
* @returns
|
||||
*/
|
||||
toCamelCase(obj) {
|
||||
const camelCaseObject = {};
|
||||
for (const i in obj) {
|
||||
const camelCase = i.replace(/([-_][a-z])/gi, $1 => {
|
||||
return $1.toUpperCase().replace('-', '').replace('_', '');
|
||||
});
|
||||
camelCaseObject[camelCase] = obj[i];
|
||||
}
|
||||
return camelCaseObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配URL
|
||||
* @param pattern
|
||||
* @param url
|
||||
* @returns
|
||||
*/
|
||||
matchUrl(pattern, url) {
|
||||
// 将 pattern 和 url 按 `/` 分割
|
||||
const patternParts = pattern.split('/').filter(Boolean);
|
||||
const urlParts = url.split('/').filter(Boolean);
|
||||
// 如果长度不匹配且 pattern 不包含 **,直接返回 false
|
||||
if (patternParts.length !== urlParts.length && !pattern.includes('**')) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < patternParts.length; i++) {
|
||||
const patternPart = patternParts[i];
|
||||
const urlPart = urlParts[i];
|
||||
// 如果 patternPart 是 **,匹配剩余的所有部分
|
||||
if (patternPart === '**') {
|
||||
return true;
|
||||
}
|
||||
// 如果 patternPart 以 : 开头,说明是参数,直接匹配任意非空值
|
||||
if (patternPart.startsWith(':')) {
|
||||
if (!urlPart) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// 如果 patternPart 是 *,匹配任意非空部分
|
||||
if (patternPart === '*') {
|
||||
if (!urlPart) {
|
||||
return false;
|
||||
}
|
||||
} else if (patternPart !== urlPart) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 如果 pattern 和 url 的部分数量一致,则匹配成功
|
||||
return patternParts.length === urlParts.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文本中提取 JSON 字符串并转换为对象
|
||||
* @param {string} text - 可能包含 JSON 的文本
|
||||
* @returns {Object|Array|null} - 解析出的 JSON 对象,如果没有找到有效 JSON 则返回 null
|
||||
*/
|
||||
extractJSONFromText(text) {
|
||||
if (!text || typeof text !== 'string') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 尝试直接解析整个文本
|
||||
return JSON.parse(text);
|
||||
} catch (e) {
|
||||
// 整个文本不是有效的 JSON,尝试提取 JSON 部分
|
||||
}
|
||||
|
||||
// 查找可能的 JSON 开始位置({ 或 [)
|
||||
const possibleStarts = [];
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
if (text[i] === '{' || text[i] === '[') {
|
||||
possibleStarts.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
// 从每个可能的起始位置尝试提取 JSON
|
||||
for (const startIndex of possibleStarts) {
|
||||
let openBraces = 0;
|
||||
let openBrackets = 0;
|
||||
let inString = false;
|
||||
let escapeNext = false;
|
||||
|
||||
for (let i = startIndex; i < text.length; i++) {
|
||||
const char = text[i];
|
||||
|
||||
// 处理转义字符
|
||||
if (inString && !escapeNext && char === '\\') {
|
||||
escapeNext = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理字符串边界
|
||||
if (!escapeNext && char === '"') {
|
||||
inString = !inString;
|
||||
}
|
||||
|
||||
if (!inString) {
|
||||
// 只在不在字符串内部时才计算括号
|
||||
if (char === '{') openBraces++;
|
||||
else if (char === '}') openBraces--;
|
||||
else if (char === '[') openBrackets++;
|
||||
else if (char === ']') openBrackets--;
|
||||
}
|
||||
|
||||
escapeNext = false;
|
||||
|
||||
// 检查是否找到了完整的 JSON 结构
|
||||
if (
|
||||
(openBraces === 0 && text[startIndex] === '{' && char === '}') ||
|
||||
(openBrackets === 0 && text[startIndex] === '[' && char === ']')
|
||||
) {
|
||||
const jsonStr = text.substring(startIndex, i + 1);
|
||||
try {
|
||||
const result = JSON.parse(jsonStr);
|
||||
return result;
|
||||
} catch (e) {
|
||||
// 这个候选 JSON 无效,继续尝试下一个
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null; // 没有找到有效的 JSON
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user