Skip to content

Commit

Permalink
feat: option to ignore cache in ElectronVersions.create (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Apr 27, 2023
1 parent e440735 commit 3b61627
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions etc/fiddle-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class ElectronVersions extends BaseVersions {

// @public (undocumented)
export interface ElectronVersionsCreateOptions {
ignoreCache?: boolean;
initialVersions?: unknown;
}

Expand Down
23 changes: 14 additions & 9 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export interface Versions {
export interface ElectronVersionsCreateOptions {
/** Initial versions to use if there is no cache. When provided, no initial fetch is done */
initialVersions?: unknown;

/** Ignore the cache even if it exists and is fresh */
ignoreCache?: boolean;
}

export function compareVersions(a: SemVer, b: SemVer): number {
Expand Down Expand Up @@ -306,17 +309,19 @@ export class ElectronVersions extends BaseVersions {
const d = debug('fiddle-core:ElectronVersions:create');
const { versionsCache } = { ...DefaultPaths, ...paths };

let versions: unknown;
// Use initialVersions instead if provided, and don't fetch if so
let versions = options.initialVersions;
let staleCache = false;
const now = Date.now();
try {
const st = await fs.stat(versionsCache);
versions = await fs.readJson(versionsCache);
staleCache = !ElectronVersions.isCacheFresh(st.mtimeMs, now);
} catch (err) {
d('cache file missing or cannot be read', err);
// Use initialVersions instead if provided, and don't fetch
versions = options.initialVersions;

if (!options.ignoreCache) {
try {
const st = await fs.stat(versionsCache);
versions = await fs.readJson(versionsCache);
staleCache = !ElectronVersions.isCacheFresh(st.mtimeMs, now);
} catch (err) {
d('cache file missing or cannot be read', err);
}
}

if (!versions || staleCache) {
Expand Down
53 changes: 53 additions & 0 deletions tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,59 @@ describe('ElectronVersions', () => {
);
expect(versions.length).toBe(1);
});

it('does not use cache if options.ignoreCache is true', async () => {
await fs.outputJSON(versionsCache, [
{
version: '0.23.0',
},
]);
const scope = nockScope.get('/releases.json').reply(
200,
JSON.stringify([
{
version: '0.23.0',
},
{
version: '0.23.1',
},
{
version: '0.23.2',
},
]),
{
'Content-Type': 'application/json',
},
);
const { versions } = await ElectronVersions.create(
{ versionsCache },
{ ignoreCache: true },
);
expect(scope.isDone());
expect(versions.length).toBe(3);
});

it('uses options.initialVersions if cache available but options.ignoreCache is true', async () => {
await fs.outputJSON(versionsCache, [
{
version: '0.23.0',
},
]);
expect(nockScope.isDone()); // No mocks
const initialVersions = [
{
version: '0.23.0',
},
{
version: '0.23.1',
},
];
const { versions } = await ElectronVersions.create(
{ versionsCache },
{ initialVersions, ignoreCache: true },
);
expect(versions.length).toBe(2);
});
});

describe('.fetch', () => {
Expand Down

0 comments on commit 3b61627

Please sign in to comment.