@wu529778790/open-im 0.1.3 → 0.2.1

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/cli.js CHANGED
@@ -4,15 +4,15 @@ import { spawn, execFileSync } from 'node:child_process';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import { dirname, join } from 'node:path';
6
6
  import { mkdir, writeFile, rm, readFile } from 'node:fs/promises';
7
- import { existsSync } from 'node:fs';
8
- import { platform } from 'node:os';
9
- import { homedir } from 'node:os';
7
+ import { existsSync, readFileSync } from 'node:fs';
8
+ import { platform, homedir } from 'node:os';
10
9
  const __filename = fileURLToPath(import.meta.url);
11
10
  const __dirname = dirname(__filename);
12
11
  // PID 文件路径
13
12
  const PID_DIR = join(homedir(), '.open-im');
14
13
  const PID_FILE = join(PID_DIR, 'daemon.pid');
15
14
  const STOP_FILE = join(PID_DIR, 'stop.flag');
15
+ const CONFIG_FILE = join(PID_DIR, 'config.json');
16
16
  // 保存 PID 到文件
17
17
  async function savePid(pid) {
18
18
  try {
@@ -47,6 +47,27 @@ async function removePidFile() {
47
47
  // 忽略删除错误
48
48
  }
49
49
  }
50
+ // 更新工作目录到配置文件
51
+ async function updateWorkDir(workDir) {
52
+ try {
53
+ await mkdir(PID_DIR, { recursive: true });
54
+ let config = {};
55
+ if (existsSync(CONFIG_FILE)) {
56
+ try {
57
+ config = JSON.parse(readFileSync(CONFIG_FILE, 'utf-8'));
58
+ }
59
+ catch {
60
+ // 忽略解析错误
61
+ }
62
+ }
63
+ // 更新工作目录
64
+ config.claudeWorkDir = workDir;
65
+ await writeFile(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf-8');
66
+ }
67
+ catch (err) {
68
+ console.error('无法保存工作目录配置:', err);
69
+ }
70
+ }
50
71
  // 检查进程是否在运行
51
72
  async function isProcessRunning(pid) {
52
73
  try {
@@ -115,6 +136,11 @@ if (args[0] === 'stop') {
115
136
  });
116
137
  }
117
138
  else if (args[0] === 'start') {
139
+ // 获取当前工作目录
140
+ const currentDir = process.cwd();
141
+ // 更新配置中的工作目录
142
+ await updateWorkDir(currentDir);
143
+ console.log(`工作目录已设置为: ${currentDir}`);
118
144
  // 后台启动 - 跨平台方案
119
145
  const distPath = join(__dirname, '..', 'dist', 'index.js');
120
146
  // 使用 detached 模式创建独立进程
@@ -142,11 +142,38 @@ export class SessionManager {
142
142
  this.save();
143
143
  }
144
144
  async resolveAndValidate(baseDir, targetDir) {
145
- const resolved = resolve(baseDir, targetDir);
145
+ let resolved;
146
+ // 处理 Windows 驱动器路径 (如 d:, d:/path, d:\path)
147
+ const drivePathMatch = targetDir.match(/^([a-zA-Z]):(.*)$/);
148
+ if (drivePathMatch) {
149
+ const [, drive, rest] = drivePathMatch;
150
+ // 如果 rest 为空或以 \ 或 / 开头,则是驱动器根目录的绝对路径
151
+ // 如果 rest 不为空且不以 \ 或 / 开头,则是驱动器当前目录的相对路径
152
+ if (rest === '' || rest.startsWith('/') || rest.startsWith('\\')) {
153
+ resolved = `${drive}:${rest}`;
154
+ }
155
+ else {
156
+ // 需要获取该驱动器的当前目录,这里简化处理,当作绝对路径
157
+ resolved = `${drive}:${rest}`;
158
+ }
159
+ }
160
+ else if (targetDir === '~' || targetDir.startsWith('~/')) {
161
+ // 处理家目录
162
+ const home = process.env.USERPROFILE || process.env.HOME || '';
163
+ resolved = join(home, targetDir.slice(1));
164
+ }
165
+ else if (targetDir.startsWith('/') || (targetDir.length >= 3 && targetDir[1] === ':' && (targetDir[2] === '\\' || targetDir[2] === '/'))) {
166
+ // 绝对路径
167
+ resolved = targetDir;
168
+ }
169
+ else {
170
+ // 相对路径
171
+ resolved = resolve(baseDir, targetDir);
172
+ }
146
173
  if (!existsSync(resolved))
147
174
  throw new Error(`目录不存在: ${resolved}`);
148
175
  const realPath = await realpath(resolved);
149
- const allowed = this.allowedBaseDirs.some((base) => realPath === base || realPath.startsWith(base + '/'));
176
+ const allowed = this.allowedBaseDirs.some((base) => realPath === base || realPath.startsWith(base + '/') || realPath.startsWith(base + '\\'));
150
177
  if (!allowed)
151
178
  throw new Error(`目录不在允许范围内: ${realPath}`);
152
179
  return realPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wu529778790/open-im",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "Multi-platform IM bridge for AI CLI tools (Claude, Codex, Cursor)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",