Skip to content

Commit

Permalink
feat: expose release info from BaseVersions (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 20, 2023
1 parent 31b46b1 commit 5be0d31
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
18 changes: 18 additions & 0 deletions etc/fiddle-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Writable } from 'stream';
export class BaseVersions implements Versions {
constructor(versions: unknown);
// (undocumented)
getReleaseInfo(ver: SemOrStr): ReleaseInfo | undefined;
// (undocumented)
inMajor(major: number): SemVer[];
// (undocumented)
inRange(a: SemOrStr, b: SemOrStr): SemVer[];
Expand Down Expand Up @@ -196,6 +198,20 @@ export type ProgressObject = {
percent: number;
};

// @public (undocumented)
export interface ReleaseInfo {
chrome: string;
date: string;
files: Array<string>;
modules: string;
node: string;
openssl: string;
uv: string;
v8: string;
version: string;
zlib: string;
}

// @public (undocumented)
export function runFromCommandLine(argv: string[]): Promise<void>;

Expand Down Expand Up @@ -246,6 +262,8 @@ export interface TestResult {

// @public
export interface Versions {
// (undocumented)
getReleaseInfo(version: SemOrStr): ReleaseInfo | undefined;
// (undocumented)
inMajor(major: number): SemVer[];
// (undocumented)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BaseVersions,
ElectronVersions,
ElectronVersionsCreateOptions,
ReleaseInfo,
SemOrStr,
SemVer,
Versions,
Expand All @@ -46,6 +47,7 @@ export {
Mirrors,
Paths,
ProgressObject,
ReleaseInfo,
Runner,
RunnerOptions,
RunnerSpawnOptions,
Expand Down
79 changes: 79 additions & 0 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import { DefaultPaths, Paths } from './paths';

export type SemOrStr = SemVer | string;

export interface ReleaseInfo {
/** Electron version */
version: string;
/** Release date */
date: string;
/** Node.js version */
node: string;
/** V8 version */
v8: string;
/** uv version */
uv: string;
/** zlib version */
zlib: string;
/** OpenSSL version */
openssl: string;
/** Node.js modules version */
modules: string;
/** Chromium version */
chrome: string;
/** Files included in the release */
files: Array<string>;
}

/**
* Interface for an object that manages a list of Electron releases.
*
Expand Down Expand Up @@ -42,6 +65,9 @@ export interface Versions {

/** @returns all versions in a range, inclusive. Sorted in branch order. */
inRange(a: SemOrStr, b: SemOrStr): SemVer[];

/** @returns {@link ReleaseInfo} iff `version` is a release that this object knows about */
getReleaseInfo(version: SemOrStr): ReleaseInfo | undefined;
}

export interface ElectronVersionsCreateOptions {
Expand All @@ -67,6 +93,33 @@ function hasVersion(val: unknown): val is { version: unknown } {
return typeof val === 'object' && val !== null && 'version' in val;
}

function isReleaseInfo(val: unknown): val is ReleaseInfo {
return (
typeof val === 'object' &&
val !== null &&
'version' in val &&
typeof (val as { version: unknown }).version === 'string' &&
'date' in val &&
typeof (val as { date: unknown }).date === 'string' &&
'node' in val &&
typeof (val as { node: unknown }).node === 'string' &&
'v8' in val &&
typeof (val as { v8: unknown }).v8 === 'string' &&
'uv' in val &&
typeof (val as { uv: unknown }).uv === 'string' &&
'zlib' in val &&
typeof (val as { zlib: unknown }).zlib === 'string' &&
'openssl' in val &&
typeof (val as { openssl: unknown }).openssl === 'string' &&
'modules' in val &&
typeof (val as { modules: unknown }).modules === 'string' &&
'chrome' in val &&
typeof (val as { chrome: unknown }).chrome === 'string' &&
'files' in val &&
isArrayOfStrings((val as { files: unknown }).files)
);
}

function isArrayOfVersionObjects(
val: unknown,
): val is Array<{ version: string }> {
Expand All @@ -92,12 +145,34 @@ const NUM_SUPPORTED_MAJORS = 4;
*/
export class BaseVersions implements Versions {
private readonly map = new Map<string, SemVer>();
private readonly releaseInfo = new Map<string, ReleaseInfo>();

protected setVersions(val: unknown): void {
// release info doesn't need to be in sorted order
this.releaseInfo.clear();

// build the array
let parsed: Array<SemVer | null> = [];
if (isArrayOfVersionObjects(val)) {
parsed = val.map(({ version }) => semverParse(version));

// build release info
for (const entry of val) {
if (isReleaseInfo(entry)) {
this.releaseInfo.set(entry.version, {
version: entry.version,
date: entry.date,
node: entry.node,
v8: entry.v8,
uv: entry.uv,
zlib: entry.zlib,
openssl: entry.openssl,
modules: entry.modules,
chrome: entry.chrome,
files: [...entry.files],
});
}
}
} else if (isArrayOfStrings(val)) {
parsed = val.map((version) => semverParse(version));
} else {
Expand Down Expand Up @@ -188,6 +263,10 @@ export class BaseVersions implements Versions {
if (first > last) [first, last] = [last, first];
return versions.slice(first, last + 1);
}

public getReleaseInfo(ver: SemOrStr): ReleaseInfo | undefined {
return this.releaseInfo.get(typeof ver === 'string' ? ver : ver.version);
}
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,51 @@ describe('BaseVersions', () => {
expect(sems.map((sem) => sem.version)).toEqual(expected);
});
});

describe('getReleaseInfo()', () => {
it('returns release info for a known version', () => {
const version = '16.0.0-nightly.20210726';
const releaseInfo = testVersions.getReleaseInfo(version);
expect(releaseInfo).not.toBe(undefined);
expect(releaseInfo).toMatchObject({
version,
chrome: '93.0.4566.0',
date: '2021-07-26',
files: [
'darwin-x64',
'darwin-x64-symbols',
'linux-ia32',
'linux-ia32-symbols',
'linux-x64',
'linux-x64-symbols',
'win32-ia32',
'win32-ia32-symbols',
'win32-x64',
'win32-x64-symbols',
],
modules: '89',
node: '16.5.0',
openssl: '1.1.1',
uv: '1.41.0',
v8: '9.3.278-electron.0',
zlib: '1.2.11',
});
});

it('does not return release info for an unknown version', () => {
const releaseInfo = testVersions.getReleaseInfo('0.0.0');
expect(releaseInfo).toBe(undefined);
});

it('does not return release info if partial info', () => {
const version = '16.0.0-nightly.20210726';
const partialVersions = new BaseVersions([
{ version, node: '16.5.0', openssl: '1.1.1' },
]);
const releaseInfo = partialVersions.getReleaseInfo(version);
expect(releaseInfo).toBe(undefined);
});
});
});

describe('ElectronVersions', () => {
Expand Down

0 comments on commit 5be0d31

Please sign in to comment.