Skip to content

add --prefer-online flag: use cache only if network is not available #2747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ commander.usage('[command] [flags]');
commander.option('--verbose', 'output verbose messages on internal operations');
commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache');
commander.option('--prefer-offline', 'use network only if dependencies are not available in local cache');
commander.option('--prefer-online', 'use cache only if network is not available');
commander.option('--strict-semver');
commander.option('--json', '');
commander.option('--ignore-scripts', "don't run lifecycle scripts");
Expand Down Expand Up @@ -362,6 +363,7 @@ config.init({
globalFolder: commander.globalFolder,
cacheFolder: commander.cacheFolder,
preferOffline: commander.preferOffline,
preferOnline: commander.preferOnline,
captureHar: commander.har,
ignorePlatform: commander.ignorePlatform,
ignoreEngines: commander.ignoreEngines,
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ConfigOptions = {
linkFolder?: ?string,
offline?: boolean,
preferOffline?: boolean,
preferOnline?: boolean,
captureHar?: boolean,
ignoreScripts?: boolean,
ignorePlatform?: boolean,
Expand Down Expand Up @@ -83,6 +84,7 @@ export default class Config {
looseSemver: boolean;
offline: boolean;
preferOffline: boolean;
preferOnline: boolean;
ignorePlatform: boolean;
binLinks: boolean;

Expand Down Expand Up @@ -261,6 +263,7 @@ export default class Config {
this.commandName = opts.commandName || '';

this.preferOffline = !!opts.preferOffline;
this.preferOnline = !!opts.preferOnline;
this.modulesFolder = opts.modulesFolder;
this.globalFolder = opts.globalFolder || constants.GLOBAL_MODULE_DIRECTORY;
this.linkFolder = opts.linkFolder || constants.LINK_REGISTRY_DIRECTORY;
Expand Down
20 changes: 15 additions & 5 deletions src/resolvers/registries/npm-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,23 @@ export default class NpmResolver extends RegistryResolver {
}
}

const body = await this.config.registries.npm.request(NpmRegistry.escapeName(this.name));
try {
const body = await this.config.registries.npm.request(NpmRegistry.escapeName(this.name));
if (body) {
return await NpmResolver.findVersionInRegistryResponse(this.config, this.range, body, this.request);
}
} catch (err) {
if(this.config.preferOnline) {
this.config.offline = true;

if (body) {
return await NpmResolver.findVersionInRegistryResponse(this.config, this.range, body, this.request);
} else {
return null;
const res = this.resolveRequestOffline();
if (res != null) {
return res;
}
}
}

return null;
}

async resolveRequestOffline(): Promise<?Manifest> {
Expand Down