Skip to content

Commit

Permalink
Add retry option
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut authored and raineorshine committed Jan 20, 2022
1 parent 5bda5fc commit 28aca8e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ As a comparison: without using the --peer option, ncu will suggest the latest ve
arg: 'ms',
description: 'Global timeout in milliseconds. (default: no global timeout and 30 seconds per npm-registry-fetch).',
},
{
long: 'retry',
arg: 'rt',
description: 'Global request retries.',
parse: s => parseInt(s, 10),
default: 2,
},
{
long: 'upgrade',
short: 'u',
Expand Down
1 change: 1 addition & 0 deletions src/lib/queryVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async function queryVersions(packageMap: Index<VersionSpec>, options: Options =
...options,
// upgrade prereleases to newer prereleases by default
pre: options.pre != null ? options.pre : isPre(version),
retry: options.retry ?? 2,
})
versionNew = npmAlias && versionNew ? createNpmAlias(name, versionNew) : versionNew
}
Expand Down
27 changes: 20 additions & 7 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,29 @@ export async function packageAuthorChanged(packageName: string, currentVersion:
* @param currentVersion
* @returns Promised result
*/
export async function viewMany(packageName: string, fields: string[], currentVersion: Version, { registry, timeout }: { registry?: string, timeout?: number } = {}) {
export async function viewMany(packageName: string, fields: string[], currentVersion: Version, { registry, timeout, retry }: { registry?: string, timeout?: number, retry?: number } = {}, retryed = 0) {
if (currentVersion && (!semver.validRange(currentVersion) || versionUtil.isWildCard(currentVersion))) {
return Promise.resolve({} as Packument)
}

const result = await pacote.packument(packageName, {
...npmConfig,
fullMetadata: fields.includes('time'),
...registry ? { registry } : null,
timeout,
})
let result: any
try {
result = await pacote.packument(packageName, {
...npmConfig,
fullMetadata: fields.includes('time'),
...registry ? { registry } : null,
timeout,
})
}
catch (err: any) {
if (retry && ++retryed <= retry) {
console.error(`\nFetchError: Request ${packageName} info failed[${retryed} of ${retry}]: \n${err.message}.`)
const packument: Packument = await viewMany(packageName, fields, currentVersion, { registry, timeout, retry }, retryed)
return packument
}

throw err
}
return fields.reduce((accum, field) => ({
...accum,
[field]: field.startsWith('dist-tags.') && result.versions ?
Expand Down Expand Up @@ -305,6 +317,7 @@ export const latest: GetVersion = async (packageName, currentVersion, options =
const latest = await viewOne(packageName, 'dist-tags.latest', currentVersion, {
registry: options.registry,
timeout: options.timeout,
retry: options.retry,
}) as unknown as Packument // known type based on dist-tags.latest

// latest should not be deprecated
Expand Down
1 change: 1 addition & 0 deletions src/package-managers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const latest: GetVersion = async (packageName: string, currentVersion: Ve
const latest = await viewOne(packageName, 'dist-tags.latest', currentVersion, {
registry: options.registry,
timeout: options.timeout,
retry: options.retry,
}) as unknown as Packument // known type based on dist-tags.latest

// latest should not be deprecated
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ export interface RunOptions {
*/
timeout?: number,

/**
* Global request retries. (default: 3).
*/
retry?: number,

/**
* Overwrite package file with upgraded versions instead of just outputting to console.
*/
Expand Down

0 comments on commit 28aca8e

Please sign in to comment.