mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-12 22:14:22 +00:00
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
This commit is contained in:
1 parent
0aa9131642
commit
790ac3c862
3 files changed
+18
-17
No files matched your search
@@ -8,7 +8,8 @@ import * as os from 'os';
|
|||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import { colored } from './helpers';
|
import { colored } from './helpers';
|
||||||
|
|
||||||
const UPDATE_CHECK_FILE = path.join(os.homedir(), '.ccs', 'update-check.json');
|
const CACHE_DIR = path.join(os.homedir(), '.ccs', 'cache');
|
||||||
|
const UPDATE_CHECK_FILE = path.join(CACHE_DIR, 'update-check.json');
|
||||||
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
|
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
|
||||||
const GITHUB_API_URL = 'https://api.github.com/repos/kaitranntt/ccs/releases/latest';
|
const GITHUB_API_URL = 'https://api.github.com/repos/kaitranntt/ccs/releases/latest';
|
||||||
const NPM_REGISTRY_BASE = 'https://registry.npmjs.org/@kaitranntt/ccs';
|
const NPM_REGISTRY_BASE = 'https://registry.npmjs.org/@kaitranntt/ccs';
|
||||||
@@ -208,9 +209,8 @@ export function readCache(): UpdateCache {
|
|||||||
*/
|
*/
|
||||||
export function writeCache(cache: UpdateCache): void {
|
export function writeCache(cache: UpdateCache): void {
|
||||||
try {
|
try {
|
||||||
const ccsDir = path.join(os.homedir(), '.ccs');
|
if (!fs.existsSync(CACHE_DIR)) {
|
||||||
if (!fs.existsSync(ccsDir)) {
|
fs.mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 });
|
||||||
fs.mkdirSync(ccsDir, { recursive: true, mode: 0o700 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache, null, 2), 'utf8');
|
fs.writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache, null, 2), 'utf8');
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
* Caches aggregated usage data to disk to avoid re-parsing 6000+ JSONL files
|
* Caches aggregated usage data to disk to avoid re-parsing 6000+ JSONL files
|
||||||
* on every dashboard startup. Uses TTL-based invalidation with stale-while-revalidate.
|
* on every dashboard startup. Uses TTL-based invalidation with stale-while-revalidate.
|
||||||
*
|
*
|
||||||
* Cache location: ~/.ccs/usage-cache.json
|
* Cache location: ~/.ccs/cache/usage.json
|
||||||
* Default TTL: 5 minutes (configurable)
|
* Default TTL: 5 minutes (configurable)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -15,7 +15,8 @@ import type { DailyUsage, MonthlyUsage, SessionUsage } from './usage-types';
|
|||||||
|
|
||||||
// Cache configuration
|
// Cache configuration
|
||||||
const CCS_DIR = path.join(os.homedir(), '.ccs');
|
const CCS_DIR = path.join(os.homedir(), '.ccs');
|
||||||
const CACHE_FILE = path.join(CCS_DIR, 'usage-cache.json');
|
const CACHE_DIR = path.join(CCS_DIR, 'cache');
|
||||||
|
const CACHE_FILE = path.join(CACHE_DIR, 'usage.json');
|
||||||
const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||||
const STALE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days (max age for stale data)
|
const STALE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days (max age for stale data)
|
||||||
|
|
||||||
@@ -33,11 +34,11 @@ export interface UsageDiskCache {
|
|||||||
const CACHE_VERSION = 2;
|
const CACHE_VERSION = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure ~/.ccs directory exists
|
* Ensure ~/.ccs/cache directory exists
|
||||||
*/
|
*/
|
||||||
function ensureCcsDir(): void {
|
function ensureCacheDir(): void {
|
||||||
if (!fs.existsSync(CCS_DIR)) {
|
if (!fs.existsSync(CACHE_DIR)) {
|
||||||
fs.mkdirSync(CCS_DIR, { recursive: true });
|
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +98,7 @@ export function writeDiskCache(
|
|||||||
session: SessionUsage[]
|
session: SessionUsage[]
|
||||||
): void {
|
): void {
|
||||||
try {
|
try {
|
||||||
ensureCcsDir();
|
ensureCacheDir();
|
||||||
|
|
||||||
const cache: UsageDiskCache = {
|
const cache: UsageDiskCache = {
|
||||||
version: CACHE_VERSION,
|
version: CACHE_VERSION,
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
latest_version: null,
|
latest_version: null,
|
||||||
dismissed_version: null
|
dismissed_version: null
|
||||||
};
|
};
|
||||||
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
|
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use latest tag when targetTag is "latest"', async function () {
|
it('should use latest tag when targetTag is "latest"', async function () {
|
||||||
@@ -362,7 +362,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'dev');
|
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'dev');
|
||||||
|
|
||||||
// Check cache was updated with dev version
|
// Check cache was updated with dev version
|
||||||
const cachePath = path.join(os.homedir(), '.ccs', 'update-check.json');
|
const cachePath = path.join(os.homedir(), '.ccs', 'cache', 'update-check.json');
|
||||||
const cacheData = JSON.parse(mockFileSystem[cachePath]);
|
const cacheData = JSON.parse(mockFileSystem[cachePath]);
|
||||||
|
|
||||||
assert.strictEqual(cacheData.latest_version, '5.5.0');
|
assert.strictEqual(cacheData.latest_version, '5.5.0');
|
||||||
@@ -376,7 +376,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
latest_version: '5.5.0',
|
latest_version: '5.5.0',
|
||||||
dismissed_version: null
|
dismissed_version: null
|
||||||
};
|
};
|
||||||
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
|
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
|
||||||
|
|
||||||
// Call with force=false to use cache
|
// Call with force=false to use cache
|
||||||
const result = await updateCheckerModule.checkForUpdates('5.4.0', false, 'npm', 'dev');
|
const result = await updateCheckerModule.checkForUpdates('5.4.0', false, 'npm', 'dev');
|
||||||
@@ -465,7 +465,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
describe('Cache functionality', function () {
|
describe('Cache functionality', function () {
|
||||||
it('should create cache directory if not exists', async function () {
|
it('should create cache directory if not exists', async function () {
|
||||||
// Ensure no cache exists
|
// Ensure no cache exists
|
||||||
const cacheDir = path.join(os.homedir(), '.ccs');
|
const cacheDir = path.join(os.homedir(), '.ccs', 'cache');
|
||||||
delete mockFileSystem[cacheDir];
|
delete mockFileSystem[cacheDir];
|
||||||
|
|
||||||
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');
|
await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');
|
||||||
@@ -481,7 +481,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
latest_version: '5.5.0',
|
latest_version: '5.5.0',
|
||||||
dismissed_version: '5.5.0'
|
dismissed_version: '5.5.0'
|
||||||
};
|
};
|
||||||
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = JSON.stringify(cacheData);
|
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = JSON.stringify(cacheData);
|
||||||
|
|
||||||
const result = await updateCheckerModule.checkForUpdates('5.4.1', false, 'npm', 'dev');
|
const result = await updateCheckerModule.checkForUpdates('5.4.1', false, 'npm', 'dev');
|
||||||
|
|
||||||
@@ -492,7 +492,7 @@ describe('Beta Channel Implementation (Phase 3)', function () {
|
|||||||
|
|
||||||
it('should handle corrupted cache gracefully', async function () {
|
it('should handle corrupted cache gracefully', async function () {
|
||||||
// Set up corrupted cache
|
// Set up corrupted cache
|
||||||
mockFileSystem[path.join(os.homedir(), '.ccs', 'update-check.json')] = 'invalid json';
|
mockFileSystem[path.join(os.homedir(), '.ccs', 'cache', 'update-check.json')] = 'invalid json';
|
||||||
|
|
||||||
// Should not throw error
|
// Should not throw error
|
||||||
const result = await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');
|
const result = await updateCheckerModule.checkForUpdates('5.4.0', true, 'npm', 'latest');
|
||||||
|
|||||||
Reference in new issue
Block a user