Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
feat: Add "latest" switch (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Dec 11, 2020
1 parent 87f39ec commit 2edac2d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ Allowed version argument inputs:
Options:
-d, --debug Enable debug logging
-f, --force Force downloading the latest release file
-a, --latest List only the latest release (alias for --limit 1,
disables limit)
-l, --limit <number> Limit output of releases
-r, --raw Output raw JSON
-s, --source <url> Use a custom releases source URL or path
-t, --timeout <number> Use a custom HTTP timeout
-v, --version output the version number
--no-colors Don't use colors for displaying
--no-prereleases Don't include Electron prereleases
-h, --help output usage information
-h, --help display help for command
Commands:
electron|e [version] Get informations about an Electron release
Expand All @@ -42,6 +44,7 @@ Commands:
v8|v [version] Get informations about V8 releases
zlib|z [version] Get informations about zlib releases
all|a Get informations about all releases
help [command] display help for command
```

### Examples
Expand Down
11 changes: 11 additions & 0 deletions spec/ElectronInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,16 @@ describe('ElectronInfo', () => {
tempDirectory: tempDirDownload,
}).getDependencyReleases('chrome', 'all');
});

it('Uses latest as alias for limit=1', async () => {
const result = await new ElectronInfo({
latest: true,
releasesUrl: mockUrl,
tempDirectory: tempDir,
}).getElectronReleases('all');

expect(result.length).toBe(1);
expect(result[0].version).toBe('8.0.0-nightly.20190820');
});
});
});
16 changes: 12 additions & 4 deletions src/ElectronInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ export interface Options {
electronPrereleases?: boolean;
/** Force downloading the latest release file. Default: `false`. */
forceUpdate?: boolean;
/**
* Include only the latest release. Alias for `limit=1`. Disables `limit`.
* Default: `false`.
*/
latest?: boolean;
/**
* Limit output of releases. Everything below 1 will be treated as no limit.
* Disables `latest`.
* Default: 0.
*/
limit?: number;
Expand All @@ -59,6 +65,7 @@ const defaultOptions: Required<Options> = {
debug: false,
electronPrereleases: true,
forceUpdate: false,
latest: false,
limit: 0,
releasesUrl: 'https://raw.githubusercontent.com/electron/releases/master/lite.json',
tempDirectory: '',
Expand Down Expand Up @@ -99,7 +106,7 @@ export class ElectronInfo {
async getAllReleases(formatted?: boolean, colored?: boolean): Promise<RawReleaseInfo[] | string> {
this.logger.log('Getting all releases:', {colored, formatted});
const allReleases = await this.fileService.getReleases();
const limitedReleases = this.limitReleases(allReleases, this.options.limit);
const limitedReleases = this.limitReleases(allReleases);
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases;
}

Expand All @@ -123,7 +130,7 @@ export class ElectronInfo {
release => release.deps && dependencyVersions.includes(release.deps[dependency])
);

const limitedReleases = this.limitReleases(filteredReleases, this.options.limit);
const limitedReleases = this.limitReleases(filteredReleases);
return formatted ? this.formatDependencyReleases(limitedReleases, colored) : limitedReleases;
}

Expand All @@ -140,7 +147,7 @@ export class ElectronInfo {
const electronVersions = await this.getVersions(allReleases, 'electron', version);
const filteredReleases = allReleases.filter(release => electronVersions.includes(release.version));

const limitedReleases = this.limitReleases(filteredReleases, this.options.limit);
const limitedReleases = this.limitReleases(filteredReleases);
return formatted ? this.formatReleases(limitedReleases, colored) : limitedReleases;
}

Expand Down Expand Up @@ -255,7 +262,8 @@ export class ElectronInfo {
return dependencyVersions;
}

private limitReleases(releases: RawReleaseInfo[], limit?: number): RawReleaseInfo[] {
private limitReleases(releases: RawReleaseInfo[]): RawReleaseInfo[] {
const limit = this.options.limit || (this.options.latest ? 1 : undefined);
if (limit) {
const slicedArray = releases.slice(0, limit);
this.logger.log('Limiting found versions', {
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Allowed version argument inputs:
)
.option('-d, --debug', 'Enable debug logging')
.option('-f, --force', 'Force downloading the latest release file')
.option('-a, --latest', 'List only the latest release (alias for --limit 1, disables limit)')
.option('-l, --limit <number>', 'Limit output of releases')
.option('-r, --raw', 'Output raw JSON')
.option('-s, --source <url>', 'Use a custom releases source URL or path')
Expand Down

0 comments on commit 2edac2d

Please sign in to comment.