From 31b46b1237fdf08e044cfee249ee236407ca378f Mon Sep 17 00:00:00 2001 From: David Sanders Date: Tue, 28 Feb 2023 19:17:31 -0800 Subject: [PATCH] feat: initialVersions option for ElectronVersions (#72) --- etc/fiddle-core.api.md | 7 ++++++- src/index.ts | 2 ++ src/versions.ts | 8 ++++++++ tests/versions.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/etc/fiddle-core.api.md b/etc/fiddle-core.api.md index 1e5aedf..368766c 100644 --- a/etc/fiddle-core.api.md +++ b/etc/fiddle-core.api.md @@ -64,7 +64,7 @@ export interface ElectronBinary { // @public export class ElectronVersions extends BaseVersions { // (undocumented) - static create(paths?: Partial): Promise; + static create(paths?: Partial, options?: ElectronVersionsCreateOptions): Promise; // (undocumented) fetch(): Promise; // (undocumented) @@ -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 diff --git a/src/index.ts b/src/index.ts index 207ad1c..d15a417 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ import { import { BaseVersions, ElectronVersions, + ElectronVersionsCreateOptions, SemOrStr, SemVer, Versions, @@ -34,6 +35,7 @@ export { DefaultPaths, ElectronBinary, ElectronVersions, + ElectronVersionsCreateOptions, Fiddle, FiddleFactory, FiddleSource, diff --git a/src/versions.ts b/src/versions.ts index 1cde3c7..b6e65ad 100644 --- a/src/versions.ts +++ b/src/versions.ts @@ -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; @@ -217,6 +222,7 @@ export class ElectronVersions extends BaseVersions { public static async create( paths: Partial = {}, + options: ElectronVersionsCreateOptions = {}, ): Promise { const d = debug('fiddle-core:ElectronVersions:create'); const { versionsCache } = { ...DefaultPaths, ...paths }; @@ -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) { diff --git a/tests/versions.test.ts b/tests/versions.test.ts index 73fa5e5..dda9726 100644 --- a/tests/versions.test.ts +++ b/tests/versions.test.ts @@ -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', () => {