Skip to content
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

Read config from ~/.yarnrc #5812

Closed
wants to merge 6 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 107 additions & 9 deletions __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import {resolve, join as pathJoin} from 'path';

import type {RegistryNames} from '../../src/registries/index.js';
import {registries} from '../../src/registries/index.js';
import NpmRegistry from '../../src/registries/npm-registry.js';
import {BufferReporter} from '../../src/reporters/index.js';
import homeDir, {home} from '../../src/util/user-home-dir.js';
Expand Down Expand Up @@ -74,19 +76,20 @@ function createMocks(): Object {
describe('request', () => {
// a helper function for creating an instance of npm registry,
// making requests and inspecting request parameters
function createRegistry(config: Object): Object {
function createRegistry(config: Object, registry: RegistryNames = 'npm'): Object {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter);
const npmRegistry = new registries[registry](testCwd, mockRegistries, mockRequestManager, mockReporter);
npmRegistry.config = config;
return {
request(url: string, options: Object, packageName: string): Object {
npmRegistry.request(url, options, packageName);
const lastIndex = mockRequestManager.request.mock.calls.length - 1;
const requestParams = mockRequestManager.request.mock.calls[lastIndex][0];
return requestParams;
},
const request = npmRegistry.request.bind(npmRegistry);
npmRegistry.request = (url: string, options: Object, packageName: string): Object => {
request(url, options, packageName);
const lastIndex = mockRequestManager.request.mock.calls.length - 1;
const requestParams = mockRequestManager.request.mock.calls[lastIndex][0];
return requestParams;
};

return npmRegistry;
}

test('should call requestManager.request with url', () => {
Expand Down Expand Up @@ -571,6 +574,101 @@ describe('request', () => {
});
});
});

describe('Private scoped registries with authentication details in YarnRegistry', () => {
const testCase = {
config: {
'//registry.myorg.com/:_authToken': 'scopedPrivateAuthToken',
'@private:registry': 'https://registry.myorg.com/',
'//registry.npmjs.org/:_authToken': 'scopedNPMAuthToken',
'@scoped:registry': 'https://registry.npmjs.org/',
},
requests: [
{
url: 'yarn',
pkg: 'yarn',
expect: {root: 'https://registry.npmjs.org', auth: false},
},
{
url: '@yarn%2fcore',
pkg: '@yarn/core',
expect: {root: 'https://registry.npmjs.org', auth: 'scopedNPMAuthToken'},
},
{
url: '-/package/yarn/dist-tags',
pkg: 'yarn',
expect: {root: 'https://registry.npmjs.org', auth: false},
},
{
url: '-/package/@yarn%2fcore/dist-tags',
pkg: '@yarn/core',
expect: {root: 'https://registry.npmjs.org', auth: 'scopedNPMAuthToken'},
},
{
url: '-/user/token/abcdef',
pkg: null,
expect: {root: 'https://registry.npmjs.org', auth: false},
},
{
url: 'https://registry.npmjs.org/dist/-/@yarn-core-1.0.0.tgz',
pkg: '@yarn/core',
expect: {root: 'https://registry.npmjs.org', auth: 'scopedNPMAuthToken'},
},
{
url: 'https://registry.yarnpkg.com/dist/-/@yarn-core-1.0.0.tgz',
pkg: '@yarn/core',
expect: {root: 'https://registry.yarnpkg.com', auth: 'scopedNPMAuthToken'},
},
{
url: 'https://registry.yarnpkg.com/dist/-/@yarn-core-1.0.0.tgz',
pkg: null,
expect: {root: 'https://registry.yarnpkg.com', auth: false},
},
{
url: 'https://some.cdn.com/@yarn/core.tgz',
pkg: '@yarn/core',
expect: {root: 'https://some.cdn.com', auth: false},
},
{
url: 'https://some.cdn.com/@yarn/core.tgz',
pkg: null,
expect: {root: 'https://some.cdn.com', auth: false},
},
{
url: '@private/pkg',
pkg: '@private/pkg',
expect: {root: 'https://registry.myorg.com', auth: 'scopedPrivateAuthToken'},
},
{
url: 'https://some.cdn.com/some-hash/@private-pkg-1.0.0.tar.gz',
pkg: '@private/pkg',
expect: {root: 'https://some.cdn.com', auth: false},
},
{
url: 'https://some.cdn.com/@private/pkg',
pkg: null,
expect: {root: 'https://some.cdn.com', auth: false},
},
{
url: '@scoped/pkg',
pkg: '@scoped/pkg',
expect: {root: 'https://registry.npmjs.org', auth: 'scopedNPMAuthToken'},
},
],
};
const registry = createRegistry({});
registry.registries.yarn = createRegistry(testCase.config, 'yarn');
testCase.requests.forEach(req => {
const desc =
`with request url ${req.url}${req.pkg ? ` in context of package ${req.pkg}` : ''} ` +
`auth is ${req.expect.auth ? req.expect.auth : 'not sent'}`;
(req.skip ? it.skip : req.only ? it.only : it)(desc, () => {
const requestParams = registry.request(req.url, {}, req.pkg);
expect(requestParams.url.substr(0, req.expect.root.length)).toBe(req.expect.root);
expect(requestParams.headers.authorization).toBe(req.expect.auth ? `Bearer ${req.expect.auth}` : undefined);
});
});
});
});

describe('isRequestToRegistry functional test', () => {
Expand Down
34 changes: 33 additions & 1 deletion 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 @@ -69,7 +77,31 @@ export default class BaseRegistry {
}

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

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

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

if (registry.config && registry.config[key]) {
val = registry.config[key];

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

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

return val;
}

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

for (const scope of [this.getScope(packageIdent), '']) {
const registry =
this.getScopedOption(scope, 'registry') || this.registries.yarn.getScopedOption(scope, 'registry');
Copy link
Author

@otaku otaku May 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YarnRegistry.getScopedOption does not exist.

const registry = this.getScopedOption(scope, 'registry');
if (registry) {
return String(registry);
}
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);
}

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