Skip to content

Commit

Permalink
use a plan-text version file instead of json
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiwat10 committed Sep 20, 2024
1 parent 7e4ee26 commit a727b1a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
20 changes: 7 additions & 13 deletions packages/fuels/src/cli/utils/fuelsVersionCache.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import fs from 'fs';
import path from 'path';

export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, '.fuels-cache.json');
export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, 'FUELS_VERSION');

export type FuelsVersionCache = {
version: string;
timestamp: number;
};
export type FuelsVersionCache = string;

export const saveToCache = (cache: FuelsVersionCache) => {
fs.writeFileSync(FUELS_VERSION_CACHE_FILE, JSON.stringify(cache), 'utf-8');
fs.writeFileSync(FUELS_VERSION_CACHE_FILE, cache, 'utf-8');
};

export const FUELS_VERSION_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours in milliseconds

export const checkAndLoadCache = (): FuelsVersionCache | null => {
if (fs.existsSync(FUELS_VERSION_CACHE_FILE)) {
const savedCache = JSON.parse(fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8'));
if (
savedCache &&
savedCache.data &&
Date.now() - savedCache.timestamp < FUELS_VERSION_CACHE_TTL
) {
return savedCache.data.version;
const cachedVersion = fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8');
const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE);
if (cachedVersion && Date.now() - cacheTimestamp < FUELS_VERSION_CACHE_TTL) {
return cachedVersion;
}
}
return null;
Expand Down
12 changes: 3 additions & 9 deletions packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ describe('getLatestFuelsVersion', () => {
});

it('should return cached version if it exists', async () => {
const cache = {
version: '1.0.0',
timestamp: Date.now(),
};
vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache);
const cachedVersion = '1.0.0';
vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cachedVersion);
const result = await getLatestFuelsVersion();
expect(result).toEqual('1.0.0');
});
Expand All @@ -40,9 +37,6 @@ describe('getLatestFuelsVersion', () => {
const version = await getLatestFuelsVersion();
expect(fetchSpy).toHaveBeenCalled();
expect(version).toEqual('1.0.0');
expect(saveCacheSpy).toHaveBeenCalledWith({
version: '1.0.0',
timestamp: expect.any(Number),
});
expect(saveCacheSpy).toHaveBeenCalledWith('1.0.0');
});
});
13 changes: 4 additions & 9 deletions packages/fuels/src/cli/utils/getLatestFuelsVersion.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { checkAndLoadCache, saveToCache } from './fuelsVersionCache';

export const getLatestFuelsVersion = async (): Promise<string | undefined> => {
const now = Date.now();

const cache = checkAndLoadCache();
if (cache) {
return cache.version;
const cachedVersion = checkAndLoadCache();
if (cachedVersion) {
return cachedVersion;
}

const data: { version: string } | null = await Promise.race([
Expand All @@ -21,10 +19,7 @@ export const getLatestFuelsVersion = async (): Promise<string | undefined> => {

const version = data.version as string;

saveToCache({
version,
timestamp: now,
});
saveToCache(version);

return version;
};

0 comments on commit a727b1a

Please sign in to comment.