Skip to content

Commit

Permalink
feat: initialVersions option for ElectronVersions (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 1, 2023
1 parent 69d1fd4 commit 31b46b1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
7 changes: 6 additions & 1 deletion etc/fiddle-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface ElectronBinary {
// @public
export class ElectronVersions extends BaseVersions {
// (undocumented)
static create(paths?: Partial<Paths>): Promise<ElectronVersions>;
static create(paths?: Partial<Paths>, options?: ElectronVersionsCreateOptions): Promise<ElectronVersions>;
// (undocumented)
fetch(): Promise<void>;
// (undocumented)
Expand All @@ -89,6 +89,11 @@ export class ElectronVersions extends BaseVersions {
get versions(): SemVer[];
}

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

// @public (undocumented)
export class Fiddle {
constructor(mainPath: string, // /path/to/main.js
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import {
BaseVersions,
ElectronVersions,
ElectronVersionsCreateOptions,
SemOrStr,
SemVer,
Versions,
Expand All @@ -34,6 +35,7 @@ export {
DefaultPaths,
ElectronBinary,
ElectronVersions,
ElectronVersionsCreateOptions,
Fiddle,
FiddleFactory,
FiddleSource,
Expand Down
8 changes: 8 additions & 0 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface Versions {
inRange(a: SemOrStr, b: SemOrStr): SemVer[];
}

export interface ElectronVersionsCreateOptions {
/** Initial versions to use if there is no cache. When provided, no initial fetch is done */
initialVersions?: unknown;
}

export function compareVersions(a: SemVer, b: SemVer): number {
const l = a.compareMain(b);
if (l) return l;
Expand Down Expand Up @@ -217,6 +222,7 @@ export class ElectronVersions extends BaseVersions {

public static async create(
paths: Partial<Paths> = {},
options: ElectronVersionsCreateOptions = {},
): Promise<ElectronVersions> {
const d = debug('fiddle-core:ElectronVersions:create');
const { versionsCache } = { ...DefaultPaths, ...paths };
Expand All @@ -230,6 +236,8 @@ export class ElectronVersions extends BaseVersions {
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 (!versions || staleCache) {
Expand Down
40 changes: 40 additions & 0 deletions tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,46 @@ describe('ElectronVersions', () => {
expect(scope.isDone());
expect(versions.length).toBe(1061);
});

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

it('does not use options.initialVersions if cache available', 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 },
);
expect(versions.length).toBe(1);
});
});

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

0 comments on commit 31b46b1

Please sign in to comment.