@myassis/gateway 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/services/LocalTaskService.js +1 -1
- package/dist/services/NotificationService.js +3 -3
- package/dist/services/TaskSchedulerService.js +4 -4
- package/dist/services/TaskService.js +2 -2
- package/dist/services/agent/Agent.js +1 -1
- package/dist/services/agent/AgentManager.js +2 -2
- package/dist/services/dataService.js +3 -3
- package/dist/services/index.js +9 -9
- package/dist/services/memory/MemoryManager.js +3 -3
- package/dist/services/model/index.js +1 -1
- package/dist/services/session/Session.js +5 -5
- package/dist/services/session/SessionManager.js +2 -2
- package/dist/services/session/SessionStore.js +1 -1
- package/dist/services/session/index.js +2 -2
- package/dist/services/systemPrompt.js +1 -1
- package/dist/services/task/PushTokenStore.js +1 -1
- package/dist/services/task/TaskStore.js +1 -1
- package/dist/services/tools/index.js +15 -15
- package/dist/services/tools/model.js +1 -1
- package/dist/services/tools/sessionsSpawn.js +1 -1
- package/dist/services/tools/skill.js +1 -1
- package/dist/services/tools/task.js +1 -1
- package/dist/stores/index.js +3 -3
- package/package.json +2 -2
- package/scripts/fix-dist-imports.js +95 -0
- package/scripts/fix-imports.js +90 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 使用 SQLite 数据库存储任务
|
|
6
6
|
*/
|
|
7
7
|
import { v4 as uuidv4 } from 'uuid';
|
|
8
|
-
import { TaskStore } from './task/TaskStore';
|
|
8
|
+
import { TaskStore } from './task/TaskStore.js';
|
|
9
9
|
import { getLogger, sharedTaskService, formatUTCForLog } from '@myassis/shared';
|
|
10
10
|
const logger = getLogger('LocalTaskService');
|
|
11
11
|
class LocalTaskService {
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* 负责发送任务通知
|
|
4
4
|
* 优先级:WebSocket > HMS Push (华为) / Expo Push (其他)
|
|
5
5
|
*/
|
|
6
|
-
import { PushTokenStore } from './task/PushTokenStore';
|
|
7
|
-
import { webSocketService } from './WebSocketService';
|
|
8
|
-
import { hmsPushService } from './HMSPushService';
|
|
6
|
+
import { PushTokenStore } from './task/PushTokenStore.js';
|
|
7
|
+
import { webSocketService } from './WebSocketService.js';
|
|
8
|
+
import { hmsPushService } from './HMSPushService.js';
|
|
9
9
|
import { getLogger } from '@myassis/shared';
|
|
10
10
|
const logger = getLogger('NotificationService');
|
|
11
11
|
const log = logger;
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* 基于 SQLite 数据库的任务调度
|
|
4
4
|
* 每分钟检查待执行任务并发送通知
|
|
5
5
|
*/
|
|
6
|
-
import { taskStore } from './task/TaskStore';
|
|
7
|
-
import { webSocketService } from './WebSocketService';
|
|
8
|
-
import { getSessionManager } from './session';
|
|
6
|
+
import { taskStore } from './task/TaskStore.js';
|
|
7
|
+
import { webSocketService } from './WebSocketService.js';
|
|
8
|
+
import { getSessionManager } from './session.js';
|
|
9
9
|
import { getLogger, getUTCTimeKey, formatUTCForLog, holidayService } from '@myassis/shared';
|
|
10
|
-
import { tasksService } from './dataService';
|
|
10
|
+
import { tasksService } from './dataService.js';
|
|
11
11
|
import { authStore } from '@/stores';
|
|
12
12
|
const logger = getLogger('TaskSchedulerService');
|
|
13
13
|
const EXPIRED_THRESHOLD = 60 * 60 * 1000; // 1小时
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* - platformApply !== currentPlatform → 服务器 API(tasksApi)
|
|
9
9
|
*/
|
|
10
10
|
import { authStore } from '@/stores';
|
|
11
|
-
import { tasksApi } from '../api';
|
|
12
|
-
import { localTaskService } from './LocalTaskService';
|
|
11
|
+
import { tasksApi } from '../api.js';
|
|
12
|
+
import { localTaskService } from './LocalTaskService.js';
|
|
13
13
|
import { getLogger } from '@myassis/shared';
|
|
14
14
|
import { getPlatform } from '@myassis/shared/dist/utils/system.js';
|
|
15
15
|
// 当前网关平台标识
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import { getSessionManager } from '../session/SessionManager';
|
|
2
|
+
import { getSessionManager } from '../session/SessionManager.js';
|
|
3
3
|
/**
|
|
4
4
|
* Agent - Business logic entity
|
|
5
5
|
* Represents an AI agent with independent system prompt and multiple sessions
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import { Agent } from './Agent';
|
|
3
|
-
import { getSessionManager } from '../session/SessionManager';
|
|
2
|
+
import { Agent } from './Agent.js';
|
|
3
|
+
import { getSessionManager } from '../session/SessionManager.js';
|
|
4
4
|
import { authStore } from '@/stores/authStore';
|
|
5
5
|
import { getLogger } from '@myassis/shared';
|
|
6
6
|
import appConfig from '@/config';
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* - /api/v1/data/* - 用户数据路由 (models, skills, tasks, settings)
|
|
8
8
|
* - /api/v1/skill-hubs/* - 技能库路由
|
|
9
9
|
*/
|
|
10
|
-
import { authApi, skillsApi, skillHubApi, modelsApi, settingsApi } from '../api';
|
|
11
|
-
import { authStore, memoryStore, persistStore } from '../stores';
|
|
12
|
-
import { taskService } from './TaskService';
|
|
10
|
+
import { authApi, skillsApi, skillHubApi, modelsApi, settingsApi } from '../api/index.js';
|
|
11
|
+
import { authStore, memoryStore, persistStore } from '../stores/index.js';
|
|
12
|
+
import { taskService } from './TaskService.js';
|
|
13
13
|
import { getLogger } from '@myassis/shared';
|
|
14
14
|
const logger = getLogger('DataService');
|
|
15
15
|
// ============ 认证服务 ============
|
package/dist/services/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// Re-export all services from dataService
|
|
2
|
-
export { authService, skillsService, skillHubService, modelsService, settingsService, } from './dataService';
|
|
2
|
+
export { authService, skillsService, skillHubService, modelsService, settingsService, } from './dataService.js';
|
|
3
3
|
// Re-export tasksService from dataService (server-only tasks)
|
|
4
|
-
export { tasksService } from './dataService';
|
|
4
|
+
export { tasksService } from './dataService.js';
|
|
5
5
|
// Re-export session manager
|
|
6
|
-
export { getSessionManager } from './session';
|
|
6
|
+
export { getSessionManager } from './session.js';
|
|
7
7
|
// Re-export task services
|
|
8
|
-
export { taskSchedulerService } from './TaskSchedulerService';
|
|
9
|
-
export { taskService } from './TaskService';
|
|
10
|
-
export { localTaskService } from './LocalTaskService';
|
|
8
|
+
export { taskSchedulerService } from './TaskSchedulerService.js';
|
|
9
|
+
export { taskService } from './TaskService.js';
|
|
10
|
+
export { localTaskService } from './LocalTaskService.js';
|
|
11
11
|
// Re-export notification and holiday services
|
|
12
|
-
export { notificationService } from './NotificationService';
|
|
12
|
+
export { notificationService } from './NotificationService.js';
|
|
13
13
|
// Re-export task stores
|
|
14
|
-
export { TaskStore } from './task/TaskStore';
|
|
15
|
-
export { PushTokenStore } from './task/PushTokenStore';
|
|
14
|
+
export { TaskStore } from './task/TaskStore.js';
|
|
15
|
+
export { PushTokenStore } from './task/PushTokenStore.js';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { getLogger } from '@myassis/shared';
|
|
2
2
|
const logger = getLogger('MemoryManager');
|
|
3
|
-
import { toModel } from '../models';
|
|
4
|
-
import { modelsService } from '../dataService';
|
|
3
|
+
import { toModel } from '../models.js';
|
|
4
|
+
import { modelsService } from '../dataService.js';
|
|
5
5
|
import { authStore } from '@/stores';
|
|
6
|
-
import { LLMClient } from '../llm/LLMClient';
|
|
6
|
+
import { LLMClient } from '../llm/LLMClient.js';
|
|
7
7
|
import { appConfig } from '@/config';
|
|
8
8
|
const DEFAULT_CONFIG = {
|
|
9
9
|
summaryThreshold: 10,
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
2
|
import { getLogger } from '@myassis/shared';
|
|
3
|
-
import { SessionStore } from './SessionStore';
|
|
3
|
+
import { SessionStore } from './SessionStore.js';
|
|
4
4
|
import { AgentStore } from '@/services/agent/AgentStore';
|
|
5
5
|
import { authStore } from '@/stores/authStore';
|
|
6
6
|
import { toModel } from '@/services/models';
|
|
7
7
|
import { executeTool, getToolDefinitions } from '@/services/tools';
|
|
8
|
-
import { getSystemPromptAsync } from '../systemPrompt';
|
|
9
|
-
import { LLMClient, parseAruments } from '../llm/LLMClient';
|
|
8
|
+
import { getSystemPromptAsync } from '../systemPrompt.js';
|
|
9
|
+
import { LLMClient, parseAruments } from '../llm/LLMClient.js';
|
|
10
10
|
import { modelsService, settingsService } from '@/services/dataService';
|
|
11
|
-
import { MemoryManager } from '../memory/MemoryManager';
|
|
11
|
+
import { MemoryManager } from '../memory/MemoryManager.js';
|
|
12
12
|
import { appConfig } from '@/config';
|
|
13
|
-
import { getSessionManager } from './SessionManager';
|
|
13
|
+
import { getSessionManager } from './SessionManager.js';
|
|
14
14
|
const logger = getLogger('Session');
|
|
15
15
|
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
16
16
|
// AgentStore 单例 - 需要通过 SessionStore 获取数据库实例
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
2
|
import { getLogger } from '@myassis/shared';
|
|
3
3
|
const logger = getLogger('SessionManager');
|
|
4
|
-
import { sessionStore } from './SessionStore';
|
|
4
|
+
import { sessionStore } from './SessionStore.js';
|
|
5
5
|
import { authStore } from '@/stores/authStore';
|
|
6
|
-
import { Session } from './Session';
|
|
6
|
+
import { Session } from './Session.js';
|
|
7
7
|
/**
|
|
8
8
|
* SessionManager - Business logic layer
|
|
9
9
|
* Manages sessions in memory
|
|
@@ -2,7 +2,7 @@ import Database from 'better-sqlite3';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
import { getLogger } from '@myassis/shared';
|
|
5
|
-
import { MigrationManager } from './MigrationManager';
|
|
5
|
+
import { MigrationManager } from './MigrationManager.js';
|
|
6
6
|
const logger = getLogger('SessionStore');
|
|
7
7
|
/**
|
|
8
8
|
* SessionStore - Data access layer
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// 导出 SessionManager(业务逻辑层)
|
|
2
|
-
export { SessionManager, getSessionManager } from './SessionManager';
|
|
3
|
-
export { Session } from './Session';
|
|
2
|
+
export { SessionManager, getSessionManager } from './SessionManager.js';
|
|
3
|
+
export { Session } from './Session.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { authStore } from '@/stores';
|
|
2
2
|
import os from 'os';
|
|
3
|
-
import { settingsService, skillsService } from './dataService';
|
|
3
|
+
import { settingsService, skillsService } from './dataService.js';
|
|
4
4
|
import { getLogger } from '@myassis/shared';
|
|
5
5
|
const logger = getLogger('SystemPrompt');
|
|
6
6
|
// 预设的对话风格模板
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { getLogger } from '@myassis/shared';
|
|
6
6
|
const logger = getLogger('PushTokenStore');
|
|
7
|
-
import { sessionStore } from '../session/SessionStore';
|
|
7
|
+
import { sessionStore } from '../session/SessionStore.js';
|
|
8
8
|
export class PushTokenStore {
|
|
9
9
|
db;
|
|
10
10
|
constructor() {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* 操作 SQLite 数据库,与 AgentStore/SessionStore 共享同一个数据库实例
|
|
4
4
|
*/
|
|
5
5
|
import { getLogger } from '@myassis/shared';
|
|
6
|
-
import { sessionStore } from '../session/SessionStore';
|
|
6
|
+
import { sessionStore } from '../session/SessionStore.js';
|
|
7
7
|
const logger = getLogger('TaskStore');
|
|
8
8
|
function rowToTaskData(row) {
|
|
9
9
|
return {
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 工具定义集合
|
|
3
3
|
*/
|
|
4
|
-
export * from './types';
|
|
5
|
-
import { searchTool } from './search';
|
|
6
|
-
import { calculatorTool } from './calculator';
|
|
7
|
-
import { screenshotTool } from './screenshot';
|
|
8
|
-
import { keyboardTool } from './keyboard';
|
|
9
|
-
import { mouseTool } from './mouse';
|
|
10
|
-
import { fetchTool } from './fetch';
|
|
11
|
-
import { skillTool } from './skill';
|
|
12
|
-
import { execTool } from './exec';
|
|
13
|
-
import { fileTool } from './file';
|
|
14
|
-
import { taskTool } from './task';
|
|
15
|
-
import { modelTool } from './model';
|
|
16
|
-
import { editTool } from './edit';
|
|
17
|
-
import { webFetchTool } from './webFetch';
|
|
18
|
-
import { sessionsSpawnTool } from './sessionsSpawn';
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
import { searchTool } from './search.js';
|
|
6
|
+
import { calculatorTool } from './calculator.js';
|
|
7
|
+
import { screenshotTool } from './screenshot.js';
|
|
8
|
+
import { keyboardTool } from './keyboard.js';
|
|
9
|
+
import { mouseTool } from './mouse.js';
|
|
10
|
+
import { fetchTool } from './fetch.js';
|
|
11
|
+
import { skillTool } from './skill.js';
|
|
12
|
+
import { execTool } from './exec.js';
|
|
13
|
+
import { fileTool } from './file.js';
|
|
14
|
+
import { taskTool } from './task.js';
|
|
15
|
+
import { modelTool } from './model.js';
|
|
16
|
+
import { editTool } from './edit.js';
|
|
17
|
+
import { webFetchTool } from './webFetch.js';
|
|
18
|
+
import { sessionsSpawnTool } from './sessionsSpawn.js';
|
|
19
19
|
export const tools = [
|
|
20
20
|
searchTool, calculatorTool, screenshotTool, keyboardTool, mouseTool,
|
|
21
21
|
skillTool, execTool, taskTool, modelTool, fetchTool,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
2
|
import { getLogger } from '@myassis/shared';
|
|
3
|
-
import { getSessionManager } from '../session';
|
|
3
|
+
import { getSessionManager } from '../session.js';
|
|
4
4
|
const logger = getLogger('sessionsSpawn');
|
|
5
5
|
export const sessionsSpawnTool = {
|
|
6
6
|
name: 'sessions_spawn',
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { skillsService, skillHubService } from '../dataService';
|
|
1
|
+
import { skillsService, skillHubService } from '../dataService.js';
|
|
2
2
|
import { authStore, persistStore } from '@/stores';
|
|
3
3
|
import { getLogger } from '@myassis/shared';
|
|
4
4
|
const logger = getLogger('skill');
|
package/dist/stores/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Gateway Stores 统一导出
|
|
3
3
|
*/
|
|
4
|
-
export { memoryStore } from './memoryStore';
|
|
5
|
-
export { persistStore } from './persistStore';
|
|
6
|
-
export { authStore } from './authStore';
|
|
4
|
+
export { memoryStore } from './memoryStore.js';
|
|
5
|
+
export { persistStore } from './persistStore.js';
|
|
6
|
+
export { authStore } from './authStore.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myassis/gateway",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "我的助手 Gateway Service - 本地 AI 网关服务,支持认证、WebSocket 实时通信和任务调度",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"dev": "tsx watch src/index.ts",
|
|
25
25
|
"start": "node dist/index.js",
|
|
26
26
|
"build": "tsc",
|
|
27
|
-
"postbuild": "node scripts/
|
|
27
|
+
"postbuild": "node scripts/fix-dist-imports.js",
|
|
28
28
|
"pkg:win": "pkg . --targets node18-win-x64 --output myassis-gateway-win.exe",
|
|
29
29
|
"pkg:linux": "pkg . --targets node18-linux-x64 --output myassis-gateway-linux",
|
|
30
30
|
"pkg:all": "npm run pkg:win && npm run pkg:linux",
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 修复 dist 目录中的 ES Module 导入
|
|
4
|
+
* 为所有本地导入添加 .js 扩展名
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = path.dirname(__filename);
|
|
13
|
+
|
|
14
|
+
const distDir = path.join(__dirname, '..', 'dist');
|
|
15
|
+
|
|
16
|
+
// 递归处理所有 .js 文件
|
|
17
|
+
function processFile(filePath) {
|
|
18
|
+
let content = fs.readFileSync(filePath, 'utf8');
|
|
19
|
+
let modified = false;
|
|
20
|
+
|
|
21
|
+
// 匹配 import 和 export ... from 语句
|
|
22
|
+
// 匹配模式:from './xxx' 或 from '../xxx'(不包含协议、不为 / 开头、不包含 .js)
|
|
23
|
+
const importRegex = /(?:import|export)\s+(?:[\w*{}\s,]+from\s+)?['"](\.\.?\/[^'"]+)['"]/g;
|
|
24
|
+
|
|
25
|
+
let match;
|
|
26
|
+
while ((match = importRegex.exec(content)) !== null) {
|
|
27
|
+
const importPath = match[1];
|
|
28
|
+
|
|
29
|
+
// 跳过已经有扩展名的
|
|
30
|
+
if (importPath.endsWith('.js') || importPath.endsWith('.json') || importPath.endsWith('.ts')) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 跳过 node_modules 中的包(不包含 . 或 .. 开头的)
|
|
35
|
+
if (!importPath.startsWith('./') && !importPath.startsWith('../')) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 添加 .js 扩展名
|
|
40
|
+
const newImportPath = importPath + '.js';
|
|
41
|
+
content = content.replace(
|
|
42
|
+
new RegExp(`['"]${importPath}['"]`, 'g'),
|
|
43
|
+
`'${newImportPath}'`
|
|
44
|
+
);
|
|
45
|
+
modified = true;
|
|
46
|
+
|
|
47
|
+
console.log(` Fixed: ${importPath} -> ${newImportPath}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (modified) {
|
|
51
|
+
fs.writeFileSync(filePath, content, 'utf8');
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 递归查找并处理所有 .js 文件
|
|
59
|
+
function processDirectory(dir) {
|
|
60
|
+
let fixedCount = 0;
|
|
61
|
+
|
|
62
|
+
const items = fs.readdirSync(dir);
|
|
63
|
+
|
|
64
|
+
items.forEach(item => {
|
|
65
|
+
const itemPath = path.join(dir, item);
|
|
66
|
+
const stat = fs.statSync(itemPath);
|
|
67
|
+
|
|
68
|
+
if (stat.isDirectory()) {
|
|
69
|
+
fixedCount += processDirectory(itemPath);
|
|
70
|
+
} else if (item.endsWith('.js')) {
|
|
71
|
+
if (processFile(itemPath)) {
|
|
72
|
+
fixedCount++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return fixedCount;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 主函数
|
|
81
|
+
function main() {
|
|
82
|
+
console.log('🔧 修复 dist 目录中的 ES Module 导入...');
|
|
83
|
+
console.log(`目录: ${distDir}\n`);
|
|
84
|
+
|
|
85
|
+
if (!fs.existsSync(distDir)) {
|
|
86
|
+
console.error('❌ dist 目录不存在!请先运行 npm run build');
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fixedCount = processDirectory(distDir);
|
|
91
|
+
|
|
92
|
+
console.log(`\n✅ 完成!修复了 ${fixedCount} 个文件`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
main();
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 自动修复 ES Module 导入的扩展名
|
|
4
|
+
* 在 TypeScript 文件中,所有本地导入必须包含 .js 扩展名
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = path.dirname(__filename);
|
|
13
|
+
|
|
14
|
+
// 递归查找所有 .ts 文件(排除 .d.ts)
|
|
15
|
+
function findTsFiles(dir) {
|
|
16
|
+
let results = [];
|
|
17
|
+
const list = fs.readdirSync(dir);
|
|
18
|
+
|
|
19
|
+
list.forEach(file => {
|
|
20
|
+
const filePath = path.join(dir, file);
|
|
21
|
+
const stat = fs.statSync(filePath);
|
|
22
|
+
|
|
23
|
+
if (stat && stat.isDirectory()) {
|
|
24
|
+
// 跳过 node_modules 和 dist
|
|
25
|
+
if (file !== 'node_modules' && file !== 'dist' && file !== '.git') {
|
|
26
|
+
results = results.concat(findTsFiles(filePath));
|
|
27
|
+
}
|
|
28
|
+
} else if (file.endsWith('.ts') && !file.endsWith('.d.ts')) {
|
|
29
|
+
results.push(filePath);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return results;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 修复文件中的导入语句
|
|
37
|
+
function fixImports(filePath) {
|
|
38
|
+
let content = fs.readFileSync(filePath, 'utf8');
|
|
39
|
+
let modified = false;
|
|
40
|
+
|
|
41
|
+
// 匹配 import 语句中的本地文件路径(以 ./ 或 ../ 开头,且不以 .js 或 .ts 结尾)
|
|
42
|
+
const importRegex = /from\s+['"](\.\.?\/[^'"]+)['"]/g;
|
|
43
|
+
|
|
44
|
+
content = content.replace(importRegex, (match, importPath) => {
|
|
45
|
+
// 如果已经以 .js 或 .ts 结尾,跳过
|
|
46
|
+
if (importPath.endsWith('.js') || importPath.endsWith('.ts')) {
|
|
47
|
+
return match;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 添加 .js 扩展名
|
|
51
|
+
modified = true;
|
|
52
|
+
return `from '${importPath}.js'`;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (modified) {
|
|
56
|
+
fs.writeFileSync(filePath, content, 'utf8');
|
|
57
|
+
console.log(`✓ Fixed: ${path.relative(process.cwd(), filePath)}`);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 主函数
|
|
65
|
+
function main() {
|
|
66
|
+
const srcDir = path.join(__dirname, '..', 'src');
|
|
67
|
+
|
|
68
|
+
console.log('🔍 查找 TypeScript 文件...');
|
|
69
|
+
const tsFiles = findTsFiles(srcDir);
|
|
70
|
+
console.log(`找到 ${tsFiles.length} 个文件\n`);
|
|
71
|
+
|
|
72
|
+
console.log('🔧 修复导入语句...');
|
|
73
|
+
let fixedCount = 0;
|
|
74
|
+
|
|
75
|
+
tsFiles.forEach(file => {
|
|
76
|
+
if (fixImports(file)) {
|
|
77
|
+
fixedCount++;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
console.log(`\n✅ 完成!修复了 ${fixedCount} 个文件`);
|
|
82
|
+
|
|
83
|
+
if (fixedCount > 0) {
|
|
84
|
+
console.log('\n⚠️ 请运行以下命令验证修改:');
|
|
85
|
+
console.log(' npm run build');
|
|
86
|
+
console.log(' node dist/index.js');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
main();
|