fix(cursor-settings): remove sync fs calls in settings sync

- switch cursor settings file reads/writes to fs.promises

- make settings sync and route handler async with awaited file ops

- restores maintainability baseline for synchronous fs references
This commit is contained in:
Tam Nhu Tran
2026-02-14 17:48:00 +07:00
parent 7ab7a156eb
commit 4f086aa34a
+12 -13
View File
@@ -5,6 +5,7 @@
import type { Request, Response } from 'express'; import type { Request, Response } from 'express';
import { Router } from 'express'; import { Router } from 'express';
import * as fs from 'fs'; import * as fs from 'fs';
import { promises as fsp } from 'fs';
import * as path from 'path'; import * as path from 'path';
import { getCcsDir } from '../../utils/config-manager'; import { getCcsDir } from '../../utils/config-manager';
import { DEFAULT_CURSOR_CONFIG } from '../../config/unified-config-types'; import { DEFAULT_CURSOR_CONFIG } from '../../config/unified-config-types';
@@ -61,19 +62,17 @@ function getDefaultCursorSettings(cursorConfig: CursorConfig): { env: Record<str
}; };
} }
function syncRawSettingsFromCursorConfig(cursorConfig: CursorConfig): void { async function syncRawSettingsFromCursorConfig(cursorConfig: CursorConfig): Promise<void> {
const settingsPath = path.join(getCcsDir(), 'cursor.settings.json'); const settingsPath = path.join(getCcsDir(), 'cursor.settings.json');
let settings: Record<string, unknown> = {}; let settings: Record<string, unknown> = {};
if (fs.existsSync(settingsPath)) { try {
try { const parsed = JSON.parse(await fsp.readFile(settingsPath, 'utf-8'));
const parsed = JSON.parse(fs.readFileSync(settingsPath, 'utf-8')); if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) { settings = parsed as Record<string, unknown>;
settings = parsed as Record<string, unknown>;
}
} catch {
settings = {};
} }
} catch {
settings = {};
} }
const envSource = settings.env; const envSource = settings.env;
@@ -101,8 +100,8 @@ function syncRawSettingsFromCursorConfig(cursorConfig: CursorConfig): void {
}; };
const tempPath = settingsPath + '.tmp'; const tempPath = settingsPath + '.tmp';
fs.writeFileSync(tempPath, JSON.stringify(nextSettings, null, 2) + '\n'); await fsp.writeFile(tempPath, JSON.stringify(nextSettings, null, 2) + '\n');
fs.renameSync(tempPath, settingsPath); await fsp.rename(tempPath, settingsPath);
} }
/** /**
@@ -120,7 +119,7 @@ router.get('/', (_req: Request, res: Response): void => {
/** /**
* PUT /api/cursor/settings - Update cursor config * PUT /api/cursor/settings - Update cursor config
*/ */
router.put('/', (req: Request, res: Response): void => { router.put('/', async (req: Request, res: Response): Promise<void> => {
try { try {
const updates = req.body; const updates = req.body;
@@ -213,7 +212,7 @@ router.put('/', (req: Request, res: Response): void => {
}; };
saveUnifiedConfig(config); saveUnifiedConfig(config);
syncRawSettingsFromCursorConfig(config.cursor); await syncRawSettingsFromCursorConfig(config.cursor);
res.json({ success: true, cursor: config.cursor }); res.json({ success: true, cursor: config.cursor });
} catch (error) { } catch (error) {
res.status(500).json({ error: (error as Error).message }); res.status(500).json({ error: (error as Error).message });