@mebular/mcp 0.2.4 → 0.3.2

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.
@@ -15,6 +15,7 @@ import { existsSync, readFileSync, statSync } from 'node:fs';
15
15
  import { chmod, copyFile, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
16
16
  import { hostname } from 'node:os';
17
17
  import { dirname, join } from 'node:path';
18
+ import { acquireConfigFileLock } from './config-lock.mjs';
18
19
 
19
20
  /** 旧 config 身份字段的备份名(与 config-apply 的 config.json.bak 区分)。 */
20
21
  export const LEGACY_CONFIG_BACKUP_SUFFIX = '.identity-legacy.bak';
@@ -349,10 +350,18 @@ export async function createIdentityForStorage(input) {
349
350
  * 无论旧值是否与身份一致都移除(身份是唯一权威);文件不存在或无字段时 no-op。
350
351
  * 备份只在首次迁移时创建,保留最早的 legacy 快照。
351
352
  */
352
- export async function cleanLegacyConfig({ configPath: cfgPath, identity = null } = {}) {
353
+ export async function cleanLegacyConfig({ configPath: cfgPath, identity = null, lock = true } = {}) {
353
354
  if (typeof cfgPath !== 'string' || cfgPath.length === 0 || !existsSync(cfgPath)) {
354
355
  return { migrated: false, backupPath: null, warnings: [] };
355
356
  }
357
+ const releaseConfigLock = lock ? await acquireConfigFileLock(cfgPath) : null;
358
+ if (lock && !releaseConfigLock) {
359
+ throw errorWith('CONFIG_BUSY', `配置文件正在被其他进程写入:${cfgPath}`);
360
+ }
361
+ try {
362
+ if (!existsSync(cfgPath)) {
363
+ return { migrated: false, backupPath: null, warnings: [] };
364
+ }
356
365
  let current;
357
366
  try {
358
367
  current = JSON.parse(await readFile(cfgPath, 'utf-8'));
@@ -386,4 +395,7 @@ export async function cleanLegacyConfig({ configPath: cfgPath, identity = null }
386
395
  warnings.push(`已从 config.json 移除旧身份字段,设备身份以 ${identity.deviceId} 为准`);
387
396
  }
388
397
  return { migrated: true, backupPath, warnings };
398
+ } finally {
399
+ if (releaseConfigLock) await releaseConfigLock();
400
+ }
389
401
  }