Skip to content

Commit

Permalink
Simplify Registries
Browse files Browse the repository at this point in the history
  • Loading branch information
otaku committed May 14, 2018
1 parent acc231c commit 0b0e97a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
1 change: 0 additions & 1 deletion __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@ describe('request', () => {
};
const registry = createRegistry({});
registry.registries.yarn = createRegistry(testCase.config, 'yarn');
registry.registries.yarn.registries.npm = registry;
testCase.requests.forEach(req => {
const desc =
`with request url ${req.url}${req.pkg ? ` in context of package ${req.pkg}` : ''} ` +
Expand Down
34 changes: 32 additions & 2 deletions src/registries/base-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export type CheckOutdatedReturn = Promise<{
url: string,
}>;

const aliases = {
'version-git-sign': 'sign-git-tag',
'version-tag-prefix': 'tag-version-prefix',
'version-git-tag': 'git-tag-version',
'version-commit-hooks': 'commit-hooks',
'version-git-message': 'message',
};

export default class BaseRegistry {
constructor(cwd: string, registries: ConfigRegistries, requestManager: RequestManager, reporter: Reporter) {
this.reporter = reporter;
Expand Down Expand Up @@ -68,8 +76,30 @@ export default class BaseRegistry {
this.token = token;
}

getOption(key: string): mixed {
return this.config[key];
getOption(key: string, searchRegistries: boolean = true): mixed {
let val = this.config[key];

if (typeof val === 'undefined') {
for (const registryName of Object.keys(this.registries)) {
const registry = this.registries[registryName];

if (!registry.getOption || registry.constructor.name === this.constructor.name) {
continue;
}

val = registry.getOption(key);

if (typeof val !== 'undefined') {
break;
}
}
}

if (typeof val === 'undefined' && aliases[key]) {
val = this.getOption(aliases[key]);
}

return val;
}

getAvailableRegistries(): Array<string> {
Expand Down
19 changes: 1 addition & 18 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,7 @@ export default class NpmRegistry extends Registry {
}

getScopedOption(scope: string, option: string): mixed {
const key = scope + (scope ? ':' : '') + option;

let val = this.getOption(key);

if (typeof val === 'undefined') {
const registries = Object.keys(this.registries);
for (const registryName of registries) {
if (this.registries[registryName].getOption) {
val = this.registries[registryName].getOption(key);
}

if (typeof val !== 'undefined') {
break;
}
}
}

return val;
return this.getOption(scope + (scope ? ':' : '') + option);
}

getRegistryOption(registry: string, option: string): mixed {
Expand Down
16 changes: 2 additions & 14 deletions src/registries/yarn-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export const DEFAULTS = {
'user-agent': [`yarn/${version}`, 'npm/?', `node/${process.version}`, process.platform, process.arch].join(' '),
};

const npmMap = {
'version-git-sign': 'sign-git-tag',
'version-tag-prefix': 'tag-version-prefix',
'version-git-tag': 'git-tag-version',
'version-commit-hooks': 'commit-hooks',
'version-git-message': 'message',
};

export default class YarnRegistry extends NpmRegistry {
constructor(cwd: string, registries: ConfigRegistries, requestManager: RequestManager, reporter: Reporter) {
super(cwd, registries, requestManager, reporter);
Expand All @@ -55,13 +47,9 @@ export default class YarnRegistry extends NpmRegistry {
getOption(key: string): mixed {
let val = this.config[key];

// if this isn't set in a yarn config, then use npm
if (typeof val === 'undefined') {
val = this.registries.npm.getOption(npmMap[key]);
}

// if this isn't set in a yarn config, look elsewhere
if (typeof val === 'undefined') {
val = this.registries.npm.getOption(key);
val = super.getOption(key, false);
}

// if this isn't set in a yarn config or npm config, then use the default (or undefined)
Expand Down

0 comments on commit 0b0e97a

Please sign in to comment.