Skip to content

Commit 790ac3c

Browse files
committed
fix(cache): use ~/.ccs/cache/ for usage and update-check files
- usage-disk-cache.ts: usage-cache.json → cache/usage.json - update-checker.ts: update-check.json → cache/update-check.json - Both ensure cache directory exists before writing - Update tests to use new cache paths
1 parent 0aa9131 commit 790ac3c

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/utils/update-checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as os from 'os';
88
import * as https from 'https';
99
import { colored } from './helpers';
1010

11-
const UPDATE_CHECK_FILE = path.join(os.homedir(), '.ccs', 'update-check.json');
11+
const CACHE_DIR = path.join(os.homedir(), '.ccs', 'cache');
12+
const UPDATE_CHECK_FILE = path.join(CACHE_DIR, 'update-check.json');
1213
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
1314
const GITHUB_API_URL = 'https://api.github.com/repos/kaitranntt/ccs/releases/latest';
1415
const NPM_REGISTRY_BASE = 'https://registry.npmjs.org/@kaitranntt/ccs';
@@ -208,9 +209,8 @@ export function readCache(): UpdateCache {
208209
*/
209210
export function writeCache(cache: UpdateCache): void {
210211
try {
211-
const ccsDir = path.join(os.homedir(), '.ccs');
212-
if (!fs.existsSync(ccsDir)) {
213-
fs.mkdirSync(ccsDir, { recursive: true, mode: 0o700 });
212+
if (!fs.existsSync(CACHE_DIR)) {
213+
fs.mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 });
214214
}
215215

216216
fs.writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache, null, 2), 'utf8');

src/web-server/usage-disk-cache.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Caches aggregated usage data to disk to avoid re-parsing 6000+ JSONL files
55
* on every dashboard startup. Uses TTL-based invalidation with stale-while-revalidate.
66
*
7-
* Cache location: ~/.ccs/usage-cache.json
7+
* Cache location: ~/.ccs/cache/usage.json
88
* Default TTL: 5 minutes (configurable)
99
*/
1010

@@ -15,7 +15,8 @@ import type { DailyUsage, MonthlyUsage, SessionUsage } from './usage-types';
1515

1616
// Cache configuration
1717
const CCS_DIR = path.join(os.homedir(), '.ccs');
18-
const CACHE_FILE = path.join(CCS_DIR, 'usage-cache.json');
18+
const CACHE_DIR = path.join(CCS_DIR, 'cache');
19+
const CACHE_FILE = path.join(CACHE_DIR, 'usage.json');
1920
const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
2021
const STALE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days (max age for stale data)
2122

@@ -33,11 +34,11 @@ export interface UsageDiskCache {
3334
const CACHE_VERSION = 2;
3435

3536
/**
36-
* Ensure ~/.ccs directory exists
37+
* Ensure ~/.ccs/cache directory exists
3738
*/
38-
function ensureCcsDir(): void {
39-
if (!fs.existsSync(CCS_DIR)) {
40-
fs.mkdirSync(CCS_DIR, { recursive: true });
39+
function ensureCacheDir(): void {
40+
if (!fs.existsSync(CACHE_DIR)) {
41+
fs.mkdirSync(CACHE_DIR, { recursive: true });
4142
}
4243
}
4344

@@ -97,7 +98,7 @@ export function writeDiskCache(
9798
session: SessionUsage[]
9899
): void {
99100
try {
100-
ensureCcsDir();
101+
ensureCacheDir();
101102

102103
const cache: UsageDiskCache = {
103104
version: CACHE_VERSION,

tests/unit/utils/update-checker-beta-channel.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
275275
latest_version: null,
276276
dismissed_version: null
277277
};
278-
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
278+
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
279279
});
280280

281281
it('should use latest tag when targetTag is "latest"', async function () {
@@ -362,7 +362,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
362362
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'dev');
363363

364364
// Check cache was updated with dev version
365-
const cachePath = path.join(os.homedir(), '.ccs', 'update-check.json');
365+
const cachePath = path.join(os.homedir(), '.ccs', 'cache', 'update-check.json');
366366
const cacheData = JSON.parse(mockFileSystem[cachePath]);
367367

368368
assert.strictEqual(cacheData.latest_version, '5.5.0');
@@ -376,7 +376,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
376376
latest_version: '5.5.0',
377377
dismissed_version: null
378378
};
379-
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
379+
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
380380

381381
// Call with force=false to use cache
382382
const result = await updateCheckerModule.checkForUpdates('5.4.0', false, 'npm', 'dev');
@@ -465,7 +465,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
465465
describe('Cache functionality', function () {
466466
it('should create cache directory if not exists', async function () {
467467
// Ensure no cache exists
468-
const cacheDir = path.join(os.homedir(), '.ccs');
468+
const cacheDir = path.join(os.homedir(), '.ccs', 'cache');
469469
delete mockFileSystem[cacheDir];
470470

471471
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');
@@ -481,7 +481,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
481481
latest_version: '5.5.0',
482482
dismissed_version: '5.5.0'
483483
};
484-
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
484+
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
485485

486486
const result = await updateCheckerModule.checkForUpdates('5.4.1', false, 'npm', 'dev');
487487

@@ -492,7 +492,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
492492

493493
it('should handle corrupted cache gracefully', async function () {
494494
// Set up corrupted cache
495-
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = 'invalid json';
495+
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = 'invalid json';
496496

497497
// Should not throw error
498498
const result = await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');

0 commit comments

Comments
 (0)