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

Commit

Permalink
feat: Add limit and source options (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Aug 20, 2019
1 parent 51a1633 commit b0ca6bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Allowed version argument inputs:
Options:
-f, --force Force downloading the latest release file
-l, --limit <number> Limit output of releases
-r, --raw Output raw JSON
-s, --source <url> Use a custom releases source URL
--no-colors Don't use colors for displaying
--no-prereleases Don't include Electron prereleases
-v, --version output the version number
Expand Down
28 changes: 22 additions & 6 deletions src/ElectronInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ export interface RawReleaseInfo {
}

export interface Options {
/** Default is `true`. */
/** If Electron prereleases should be included. Default is `true`. */
electronPrereleases?: boolean;
/** Default is `false`. */
/** Force downloading the latest release file. Default is `false`. */
forceUpdate?: boolean;
/** Limit output of releases. Everything below 1 will be treated as no limit. Default: no limit */
limit?: number;
/** Default is https://unpkg.com/electron-releases@latest/lite.json. */
releasesUrl?: string;
/** Will be created if not defined. */
/**
* If a certain temporary directory should be used.
* The system's temporary directory will be used if not defined.
*/
tempDirectory?: string;
}

Expand All @@ -47,6 +52,7 @@ const {promises: fsAsync} = fs;
const defaultOptions: Required<Options> = {
electronPrereleases: true,
forceUpdate: false,
limit: 0,
releasesUrl: 'https://unpkg.com/electron-releases@latest/lite.json',
tempDirectory: '',
};
Expand All @@ -66,6 +72,7 @@ export class ElectronInfo {

constructor(options?: Options) {
this.options = {...defaultOptions, ...options};
this.options.limit = Math.max(0, this.options.limit);
}

async getAllReleases(formatted?: false): Promise<RawReleaseInfo[]>;
Expand Down Expand Up @@ -159,9 +166,14 @@ export class ElectronInfo {
return JSON.parse(rawData);
}

const {data} = await axios.get(this.options.releasesUrl);
await fsAsync.writeFile(tempFile, JSON.stringify(data));
return data;
const {data: releases} = await axios.get<RawReleaseInfo[]>(this.options.releasesUrl);

if (!Array.isArray(releases)) {
throw new Error('Invalid data received from server');
}

await fsAsync.writeFile(tempFile, JSON.stringify(releases));
return releases;
}

private async fileIsReadable(filePath: string): Promise<boolean> {
Expand Down Expand Up @@ -239,6 +251,10 @@ export class ElectronInfo {
})
.map(release => (key === 'electron' ? release.version : release.deps![key]));

if (this.options.limit) {
dependencyVersions = dependencyVersions.slice(0, this.options.limit);
}

return dependencyVersions;
}
}
32 changes: 17 additions & 15 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ program
`${description}\n\nAllowed version argument inputs:\n - SemVer versions (e.g. "~7")\n - npm dist tags (e.g. "5-0-x", only Electron)\n - "all"`
)
.option('-f, --force', 'Force downloading the latest release file')
.option('-l, --limit <number>', 'Limit output of releases')
.option('-r, --raw', 'Output raw JSON')
.option('-s, --source <url>', 'Use a custom releases source URL')
.option('--no-colors', `Don't use colors for displaying`)
.option('--no-prereleases', `Don't include Electron prereleases`)
.version(version, '-v, --version');
Expand All @@ -31,11 +33,11 @@ program
process.exit();
}
try {
const releases = await new ElectronInfo({electronPrereleases: parent.prereleases}).getElectronReleases(
version,
!parent.raw as any,
parent.colors
);
const releases = await new ElectronInfo({
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
}).getElectronReleases(version, !parent.raw as any, parent.colors);
console.log(releases);
} catch (error) {
console.error(error);
Expand All @@ -54,12 +56,11 @@ for (const dependency in SupportedDependencies) {
process.exit();
}
try {
const releases = await new ElectronInfo({electronPrereleases: parent.prereleases}).getDependencyReleases(
dependency as keyof RawDeps,
version,
!parent.raw as any,
parent.colors
);
const releases = await new ElectronInfo({
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
}).getDependencyReleases(dependency as keyof RawDeps, version, !parent.raw as any, parent.colors);
console.log(releases);
} catch (error) {
console.error(error);
Expand All @@ -72,10 +73,11 @@ program
.description('Get informations about all releases')
.action(async ({parent}) => {
try {
const releases = await new ElectronInfo({electronPrereleases: parent.prereleases}).getAllReleases(
!parent.raw as any,
parent.colors
);
const releases = await new ElectronInfo({
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
}).getAllReleases(!parent.raw as any, parent.colors);
console.log(releases);
} catch (error) {
console.error(error);
Expand Down

0 comments on commit b0ca6bf

Please sign in to comment.