@pikecode/api-key-manager 2.1.8 → 2.2.0
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/README.md +52 -0
- package/package.json +1 -1
- package/src/commands/BaseCommand.js +4 -1
- package/src/commands/switch.js +13 -2
- package/src/utils/codex-files.js +24 -11
- package/src/utils/codex-launcher.js +81 -63
- package/src/utils/codex-lock.js +28 -0
- package/src/utils/env-launcher.js +3 -1
- package/src/utils/update-checker.js +18 -1
package/README.md
CHANGED
|
@@ -344,6 +344,58 @@ API Key (OPENAI_API_KEY): sk-xxxxxxxx
|
|
|
344
344
|
akm openai-work --relogin
|
|
345
345
|
```
|
|
346
346
|
|
|
347
|
+
### Codex 官方多账号
|
|
348
|
+
|
|
349
|
+
每个官方账号都应创建一个独立的 Codex 官方网页登录供应商,供应商名称就是 Session 隔离键。
|
|
350
|
+
|
|
351
|
+
```bash
|
|
352
|
+
# 添加个人 OpenAI 官方账号
|
|
353
|
+
akm add --codex
|
|
354
|
+
# 认证方式选择: 官方网页登录
|
|
355
|
+
# 供应商名称填写: openai-personal
|
|
356
|
+
|
|
357
|
+
# 添加工作 OpenAI 官方账号
|
|
358
|
+
akm add --codex
|
|
359
|
+
# 认证方式选择: 官方网页登录
|
|
360
|
+
# 供应商名称填写: openai-work
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
首次启动某个官方供应商时,如果该供应商还没有自己的 Session,Codex 会打开浏览器授权;授权完成后 AKM 会按供应商名称缓存 Session。
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
# 首次启动个人账号,会绑定 openai-personal 的 Session
|
|
367
|
+
akm openai-personal
|
|
368
|
+
|
|
369
|
+
# 首次启动工作账号,会重新网页授权,不复用 openai-personal
|
|
370
|
+
akm openai-work
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
日常切换时直接使用供应商名称即可:
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
akm openai-personal
|
|
377
|
+
akm openai-work
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
需要换号、Session 异常或希望重新网页登录时,使用 `--relogin` 清理当前供应商的 Session:
|
|
381
|
+
|
|
382
|
+
```bash
|
|
383
|
+
akm openai-work --relogin
|
|
384
|
+
akm switch openai-work --relogin
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### 会话恢复与运行保护
|
|
388
|
+
|
|
389
|
+
Codex 的不同账号共享历史目录,但 AKM 会对同一 `CODEX_HOME`(默认 `~/.codex`)限制同时运行一个 Codex 会话,防止认证信息互相覆盖。退出当前会话后再切换账号。该保护仅覆盖通过 AKM 启动的进程,请勿同时用原生 `codex` 命令切换同一目录的认证。
|
|
390
|
+
|
|
391
|
+
正常退出会释放目录锁;强制杀死 AKM 后可能留下 `.akm-launch.lock`。只有确认该目录相关的 AKM 和 Codex 进程都已退出,才可手动删除此锁文件后重试。
|
|
392
|
+
|
|
393
|
+
`--relogin` 只在首次启动时清理本地认证;历史恢复失败后的自动重试会保留新登录态。该选项不会撤销服务端 Token 或清理浏览器登录状态。
|
|
394
|
+
|
|
395
|
+
恢复历史时,明确的历史缺失或锁定错误会自动改为新会话;其他退出码为 1 的错误会保留原因,在交互终端询问是否新开会话,默认不重试。非交互环境直接返回错误。
|
|
396
|
+
|
|
397
|
+
更新检查结果(包括失败结果)缓存在 `~/.akm-update-check.json`,24 小时内不重复请求 npm。可通过 `CC_NO_UPDATE_CHECK=1` 禁用检查。
|
|
398
|
+
|
|
347
399
|
### 多账号配置
|
|
348
400
|
|
|
349
401
|
```bash
|
package/package.json
CHANGED
|
@@ -211,7 +211,10 @@ class BaseCommand {
|
|
|
211
211
|
if (this.isEscCancelled(error)) {
|
|
212
212
|
return;
|
|
213
213
|
}
|
|
214
|
-
|
|
214
|
+
if (!error.commandContext) {
|
|
215
|
+
error.commandContext = context;
|
|
216
|
+
error.message = `${context}失败: ${error.message}`;
|
|
217
|
+
}
|
|
215
218
|
throw error;
|
|
216
219
|
}
|
|
217
220
|
|
package/src/commands/switch.js
CHANGED
|
@@ -195,6 +195,17 @@ class EnvSwitcher extends BaseCommand {
|
|
|
195
195
|
|
|
196
196
|
} catch (error) {
|
|
197
197
|
UIHelper.clearLoadingAnimation(loadingInterval);
|
|
198
|
+
if (error.code === 'RESUME_FAILED') {
|
|
199
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) throw error;
|
|
200
|
+
const { startNew } = await this.prompt({
|
|
201
|
+
type: 'confirm',
|
|
202
|
+
name: 'startNew',
|
|
203
|
+
message: `${error.message}\n是否不恢复历史,改为启动新会话?`,
|
|
204
|
+
default: false
|
|
205
|
+
});
|
|
206
|
+
if (!startNew) throw error;
|
|
207
|
+
error.code = isCodex ? 'SESSION_LOCKED' : 'NO_CONVERSATION_FOUND';
|
|
208
|
+
}
|
|
198
209
|
|
|
199
210
|
// Claude Code: 如果是"没有可恢复的会话"错误,自动移除 --continue 参数并重试
|
|
200
211
|
if (!isCodex &&
|
|
@@ -213,7 +224,7 @@ class EnvSwitcher extends BaseCommand {
|
|
|
213
224
|
console.log(UIHelper.createCard('准备就绪', `环境配置完成,正在启动 🚀 ${ideDisplayName}(新会话)...`, UIHelper.icons.success));
|
|
214
225
|
console.log();
|
|
215
226
|
await launchProviderProcess(retryProvider, retryArgs, {
|
|
216
|
-
forceRelogin:
|
|
227
|
+
forceRelogin: false
|
|
217
228
|
});
|
|
218
229
|
return;
|
|
219
230
|
} catch (retryError) {
|
|
@@ -239,7 +250,7 @@ class EnvSwitcher extends BaseCommand {
|
|
|
239
250
|
console.log(UIHelper.createCard('准备就绪', `环境配置完成,正在启动 🚀 ${ideDisplayName}(新会话)...`, UIHelper.icons.success));
|
|
240
251
|
console.log();
|
|
241
252
|
await launchProviderProcess(retryProvider, retryArgs, {
|
|
242
|
-
forceRelogin:
|
|
253
|
+
forceRelogin: false
|
|
243
254
|
});
|
|
244
255
|
return;
|
|
245
256
|
} catch (retryError) {
|
package/src/utils/codex-files.js
CHANGED
|
@@ -598,22 +598,35 @@ async function applyCodexConfig(config, options = {}) {
|
|
|
598
598
|
* @returns {Promise<{codexHome: string}>}
|
|
599
599
|
*/
|
|
600
600
|
async function clearCodexAkmConfig(options = {}) {
|
|
601
|
+
let snapshots;
|
|
601
602
|
try {
|
|
602
603
|
const codexHome = await ensureCodexHome(options.codexHome);
|
|
603
604
|
const { configTomlPath, authJsonPath } = buildCodexPaths(codexHome);
|
|
604
605
|
const sessionKey = normalizeSessionKey(options.sessionKey);
|
|
605
606
|
const forceRelogin = options.forceRelogin === true;
|
|
606
607
|
const cachedAuthPath = forceRelogin ? null : await findChatGptLoginAuthSource(codexHome, sessionKey);
|
|
608
|
+
const changedPaths = [...new Set([
|
|
609
|
+
authJsonPath, configTomlPath,
|
|
610
|
+
buildSessionCacheAuthPath(codexHome, sessionKey),
|
|
611
|
+
buildSessionCacheAuthPath(codexHome)
|
|
612
|
+
])];
|
|
613
|
+
const originals = await Promise.all(changedPaths.map(readFileSnapshot));
|
|
614
|
+
await readExistingAuthJson(authJsonPath);
|
|
615
|
+
const existingToml = originals[1].exists ? originals[1].content.toString('utf8') : '';
|
|
616
|
+
const cleanedToml = removeAkmModelProvider(removeTopLevelApiBaseUrl(existingToml));
|
|
617
|
+
const cachedAuth = cachedAuthPath ? await fs.readFile(cachedAuthPath) : null;
|
|
618
|
+
const writeFile = options.writeFile || writeFileAtomic;
|
|
607
619
|
|
|
608
620
|
// 备份当前配置
|
|
609
621
|
await backupCodexFiles(codexHome);
|
|
622
|
+
snapshots = changedPaths.map((filePath, index) => [filePath, originals[index]]);
|
|
610
623
|
if (forceRelogin) {
|
|
611
624
|
await clearCachedChatGptLoginAuth(codexHome, sessionKey);
|
|
612
625
|
} else if (!sessionKey) {
|
|
613
626
|
await cacheCurrentChatGptLoginAuth(codexHome, authJsonPath, sessionKey);
|
|
614
627
|
}
|
|
615
628
|
|
|
616
|
-
//
|
|
629
|
+
// 指定账号时替换当前认证,未指定账号时保留现有官方认证。
|
|
617
630
|
const shouldRemoveAuthJson = forceRelogin
|
|
618
631
|
? await shouldRemoveAuthJsonForRelogin(authJsonPath)
|
|
619
632
|
: sessionKey
|
|
@@ -624,8 +637,7 @@ async function clearCodexAkmConfig(options = {}) {
|
|
|
624
637
|
|
|
625
638
|
if (shouldRemoveAuthJson || shouldRestoreCachedAuth) {
|
|
626
639
|
if (shouldRestoreCachedAuth) {
|
|
627
|
-
await
|
|
628
|
-
await setSecurePermissions(authJsonPath);
|
|
640
|
+
await writeFile(authJsonPath, cachedAuth, { mode: 0o600 });
|
|
629
641
|
await cacheCurrentChatGptLoginAuth(codexHome, authJsonPath, sessionKey);
|
|
630
642
|
} else {
|
|
631
643
|
await fs.remove(authJsonPath);
|
|
@@ -633,18 +645,19 @@ async function clearCodexAkmConfig(options = {}) {
|
|
|
633
645
|
}
|
|
634
646
|
|
|
635
647
|
// 清理 config.toml 中的 AKM 配置
|
|
636
|
-
if (
|
|
637
|
-
|
|
638
|
-
const cleanedToml = removeAkmModelProvider(removeTopLevelApiBaseUrl(existingToml));
|
|
639
|
-
|
|
640
|
-
if (cleanedToml !== existingToml) {
|
|
641
|
-
await writeFileAtomic(configTomlPath, cleanedToml, { encoding: 'utf8', mode: 0o600 });
|
|
642
|
-
await setSecurePermissions(configTomlPath);
|
|
643
|
-
}
|
|
648
|
+
if (cleanedToml !== existingToml) {
|
|
649
|
+
await writeFile(configTomlPath, cleanedToml, { encoding: 'utf8', mode: 0o600 });
|
|
644
650
|
}
|
|
645
651
|
|
|
646
652
|
return { codexHome };
|
|
647
653
|
} catch (error) {
|
|
654
|
+
if (snapshots) {
|
|
655
|
+
try {
|
|
656
|
+
await Promise.all(snapshots.map(([filePath, snapshot]) => restoreFileSnapshot(filePath, snapshot)));
|
|
657
|
+
} catch (rollbackError) {
|
|
658
|
+
throw new Error(`清理 Codex AKM 配置失败且回滚失败: ${error.message}; ${rollbackError.message}`, { cause: error });
|
|
659
|
+
}
|
|
660
|
+
}
|
|
648
661
|
throw new Error(`清理 Codex AKM 配置失败: ${error.message}`, { cause: error });
|
|
649
662
|
}
|
|
650
663
|
}
|
|
@@ -7,6 +7,7 @@ const {
|
|
|
7
7
|
} = require('./codex-files');
|
|
8
8
|
const { assertSupportedLaunchArgs } = require('./launch-args');
|
|
9
9
|
const { validator } = require('./validator');
|
|
10
|
+
const { acquireCodexLock } = require('./codex-lock');
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* 构建 Codex CLI 环境变量
|
|
@@ -78,52 +79,62 @@ async function executeCodexWithEnv(config, launchArgs = [], launchOptions = {})
|
|
|
78
79
|
|
|
79
80
|
// 在写入 Codex 配置文件前完成全部输入校验。
|
|
80
81
|
const env = buildCodexEnvVariables(config);
|
|
82
|
+
const releaseLock = await acquireCodexLock();
|
|
83
|
+
try {
|
|
84
|
+
// 根据认证模式处理配置文件
|
|
85
|
+
if (authMode === 'api_key') {
|
|
86
|
+
// 写入 ~/.codex/config.toml 和 ~/.codex/auth.json。
|
|
87
|
+
await applyCodexConfig(config);
|
|
88
|
+
} else if (authMode === 'chatgpt_login') {
|
|
89
|
+
// 清理 AKM 的配置,让 Codex 使用官方登录。
|
|
90
|
+
await clearCodexAkmConfig({
|
|
91
|
+
sessionKey: config.name,
|
|
92
|
+
forceRelogin: launchOptions.forceRelogin === true
|
|
93
|
+
});
|
|
94
|
+
}
|
|
81
95
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
} else if (authMode === 'chatgpt_login') {
|
|
88
|
-
// chatgpt_login 模式:清理 AKM 的配置,让 Codex 使用官方登录
|
|
89
|
-
await clearCodexAkmConfig({
|
|
90
|
-
sessionKey: config.name,
|
|
91
|
-
forceRelogin: launchOptions.forceRelogin === true
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// 处理参数:子命令放前面,选项放后面
|
|
96
|
-
const rawArgs = Array.isArray(launchArgs) ? [...launchArgs] : [];
|
|
97
|
-
const subcommands = rawArgs.filter(arg => !arg.startsWith('-'));
|
|
98
|
-
const options = rawArgs.filter(arg => arg.startsWith('-'));
|
|
99
|
-
const args = [...subcommands, ...options];
|
|
100
|
-
|
|
101
|
-
clearTerminal();
|
|
102
|
-
|
|
103
|
-
if (authMode === 'chatgpt_login') {
|
|
104
|
-
console.log('\n启动 Codex CLI (官方网页登录)...\n');
|
|
105
|
-
} else {
|
|
106
|
-
console.log('\n启动 Codex CLI...\n');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return new Promise((resolve, reject) => {
|
|
110
|
-
const child = spawn('codex', args, {
|
|
111
|
-
stdio: ['inherit', 'inherit', 'pipe'],
|
|
112
|
-
env,
|
|
113
|
-
shell: false
|
|
114
|
-
});
|
|
96
|
+
// 处理参数:子命令放前面,选项放后面
|
|
97
|
+
const rawArgs = Array.isArray(launchArgs) ? [...launchArgs] : [];
|
|
98
|
+
const subcommands = rawArgs.filter(arg => !arg.startsWith('-'));
|
|
99
|
+
const options = rawArgs.filter(arg => arg.startsWith('-'));
|
|
100
|
+
const args = [...subcommands, ...options];
|
|
115
101
|
|
|
116
|
-
|
|
102
|
+
clearTerminal();
|
|
117
103
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
104
|
+
if (authMode === 'chatgpt_login') {
|
|
105
|
+
console.log('\n启动 Codex CLI (官方网页登录)...\n');
|
|
106
|
+
} else {
|
|
107
|
+
console.log('\n启动 Codex CLI...\n');
|
|
108
|
+
}
|
|
124
109
|
|
|
125
|
-
|
|
126
|
-
|
|
110
|
+
return await new Promise((resolve, reject) => {
|
|
111
|
+
const child = spawn('codex', args, {
|
|
112
|
+
stdio: ['inherit', 'inherit', 'pipe'],
|
|
113
|
+
env,
|
|
114
|
+
shell: false
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
let stderrOutput = '';
|
|
118
|
+
let spawnFailed = false;
|
|
119
|
+
const forwardInterrupt = () => child.kill('SIGINT');
|
|
120
|
+
const forwardTerminate = () => child.kill('SIGTERM');
|
|
121
|
+
process.on('SIGINT', forwardInterrupt);
|
|
122
|
+
process.on('SIGTERM', forwardTerminate);
|
|
123
|
+
const cleanupSignals = () => {
|
|
124
|
+
process.removeListener('SIGINT', forwardInterrupt);
|
|
125
|
+
process.removeListener('SIGTERM', forwardTerminate);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// 捕获 stderr 用于检查错误信息
|
|
129
|
+
child.stderr.on('data', (data) => {
|
|
130
|
+
const output = data.toString();
|
|
131
|
+
stderrOutput += output;
|
|
132
|
+
process.stderr.write(data);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
child.on('close', async (code, signal) => {
|
|
136
|
+
cleanupSignals();
|
|
137
|
+
if (spawnFailed) return;
|
|
127
138
|
try {
|
|
128
139
|
if (authMode === 'chatgpt_login') {
|
|
129
140
|
await cacheCodexOfficialSession({ sessionKey: config.name });
|
|
@@ -132,34 +143,41 @@ async function executeCodexWithEnv(config, launchArgs = [], launchOptions = {})
|
|
|
132
143
|
reject(new Error(`保存 Codex 官方登录 Session 失败: ${error.message}`));
|
|
133
144
|
return;
|
|
134
145
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
146
|
+
if (code === 0 || code === 130 || signal === 'SIGINT') {
|
|
147
|
+
resolve();
|
|
148
|
+
} else {
|
|
149
|
+
// 只对明确的历史错误自动回退,未知错误交给用户决定。
|
|
150
|
+
const errorOutput = stderrOutput.toLowerCase();
|
|
151
|
+
const isSessionLockedError =
|
|
141
152
|
errorOutput.includes('already has an active writer') ||
|
|
142
153
|
errorOutput.includes('failed to resume session') ||
|
|
143
154
|
errorOutput.includes('thread already has an active');
|
|
144
155
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
156
|
+
if (isSessionLockedError) {
|
|
157
|
+
const error = new Error('Codex 会话被锁定或无法恢复');
|
|
158
|
+
error.code = 'SESSION_LOCKED';
|
|
159
|
+
reject(error);
|
|
160
|
+
} else {
|
|
161
|
+
const error = new Error(`Codex CLI 异常退出,退出代码: ${code}${stderrOutput.trim() ? `\n${stderrOutput.trim()}` : ''}`);
|
|
162
|
+
if (rawArgs.includes('resume') && code === 1) error.code = 'RESUME_FAILED';
|
|
163
|
+
reject(error);
|
|
164
|
+
}
|
|
151
165
|
}
|
|
152
|
-
}
|
|
153
|
-
});
|
|
166
|
+
});
|
|
154
167
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
168
|
+
child.on('error', (error) => {
|
|
169
|
+
spawnFailed = true;
|
|
170
|
+
cleanupSignals();
|
|
171
|
+
if (error.code === 'ENOENT') {
|
|
172
|
+
reject(new Error('找不到 codex 命令\n请先安装 Codex CLI: npm i -g @openai/codex 或 brew install --cask codex'));
|
|
173
|
+
} else {
|
|
174
|
+
reject(new Error(`启动 Codex CLI 失败: ${error.message}`));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
161
177
|
});
|
|
162
|
-
}
|
|
178
|
+
} finally {
|
|
179
|
+
await releaseLock();
|
|
180
|
+
}
|
|
163
181
|
}
|
|
164
182
|
|
|
165
183
|
module.exports = { executeCodexWithEnv, buildCodexEnvVariables };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { resolveCodexHome } = require('./codex-files');
|
|
4
|
+
|
|
5
|
+
async function acquireCodexLock() {
|
|
6
|
+
const home = resolveCodexHome();
|
|
7
|
+
await fs.ensureDir(home);
|
|
8
|
+
const lockPath = path.join(home, '.akm-launch.lock');
|
|
9
|
+
let handle;
|
|
10
|
+
try {
|
|
11
|
+
handle = await fs.open(lockPath, 'wx', 0o600);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (error.code !== 'EEXIST') throw error;
|
|
14
|
+
throw new Error(`此 Codex 目录已有 AKM 会话运行,请先退出该会话。若进程异常终止,请确认相关 Codex 进程均已退出后手动删除锁文件:${lockPath}`);
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
await fs.write(handle, String(process.pid));
|
|
18
|
+
} catch (error) {
|
|
19
|
+
await fs.remove(lockPath);
|
|
20
|
+
throw error;
|
|
21
|
+
} finally {
|
|
22
|
+
await fs.close(handle);
|
|
23
|
+
}
|
|
24
|
+
// shortcut: 异常强杀后保留锁,避免遗留子进程仍在写认证文件;需要自动恢复时再跟踪子进程身份。
|
|
25
|
+
return () => fs.remove(lockPath);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = { acquireCodexLock };
|
|
@@ -90,7 +90,9 @@ async function executeWithEnv(config, launchArgs = []) {
|
|
|
90
90
|
error.code = 'NO_CONVERSATION_FOUND';
|
|
91
91
|
reject(error);
|
|
92
92
|
} else {
|
|
93
|
-
|
|
93
|
+
const error = new Error(`Claude Code 异常退出,退出代码: ${code}${stderrOutput.trim() ? `\n${stderrOutput.trim()}` : ''}`);
|
|
94
|
+
if (args.includes('--continue') && code === 1) error.code = 'RESUME_FAILED';
|
|
95
|
+
reject(error);
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
});
|
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
const chalk = require('chalk');
|
|
8
8
|
const inquirer = require('inquirer');
|
|
9
9
|
const spawn = require('cross-spawn');
|
|
10
|
+
const fs = require('fs-extra');
|
|
11
|
+
const os = require('os');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const { writeJsonAtomic } = require('./atomic-file');
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* 比较两个版本号
|
|
@@ -91,7 +95,20 @@ async function checkForUpdates({ packageName, currentVersion }) {
|
|
|
91
95
|
return;
|
|
92
96
|
}
|
|
93
97
|
|
|
94
|
-
const
|
|
98
|
+
const cachePath = path.join(os.homedir(), '.akm-update-check.json');
|
|
99
|
+
let cache;
|
|
100
|
+
try {
|
|
101
|
+
cache = await fs.readJson(cachePath);
|
|
102
|
+
} catch {
|
|
103
|
+
// 缓存缺失或损坏时重新检查。
|
|
104
|
+
}
|
|
105
|
+
const age = Date.now() - cache?.checkedAt;
|
|
106
|
+
const cached = cache?.packageName === packageName && Number.isFinite(age) && age >= 0 && age < 86400000;
|
|
107
|
+
const latest = cached ? cache.latest : await getLatestVersion(packageName);
|
|
108
|
+
if (!cached) {
|
|
109
|
+
// 失败也记录检查时间,离线时不重复阻塞每次启动。
|
|
110
|
+
await writeJsonAtomic(cachePath, { packageName, checkedAt: Date.now(), latest }).catch(() => {});
|
|
111
|
+
}
|
|
95
112
|
if (!latest) return;
|
|
96
113
|
|
|
97
114
|
if (compareVersions(latest, currentVersion) > 0) {
|