Skip to content

Commit 6b57563

Browse files
lirantalBYK
authored andcommitted
feat(configuration): add custom registry cli flag (#4238)
**Summary** implements #792 - addresses a command line flag --registry to specify a registry that overides yarn/npm configuration for installing dependencies. **Test plan** Added new tests.
1 parent e3a1fe3 commit 6b57563

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

__tests__/integration.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import makeTemp from './_temp.js';
66
import * as fs from '../src/util/fs.js';
77
import * as misc from '../src/util/misc.js';
88
import * as constants from '../src/constants.js';
9+
import {explodeLockfile} from './commands/_helpers.js';
910

1011
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
1112

@@ -153,6 +154,54 @@ test('--mutex network', async () => {
153154
await Promise.all(promises);
154155
});
155156

157+
describe('--registry option', () => {
158+
test('--registry option with npm registry', async () => {
159+
const cwd = await makeTemp();
160+
161+
const registry = 'https://registry.npmjs.org';
162+
const packageJsonPath = path.join(cwd, 'package.json');
163+
await fs.writeFile(packageJsonPath, JSON.stringify({}));
164+
165+
await runYarn(['add', 'left-pad', '--registry', registry], {cwd});
166+
167+
const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
168+
const lockfile = explodeLockfile(await fs.readFile(path.join(cwd, 'yarn.lock')));
169+
170+
expect(packageJson.dependencies['left-pad']).toBeDefined();
171+
expect(lockfile).toHaveLength(3);
172+
expect(lockfile[2]).toContain(registry);
173+
});
174+
175+
test('--registry option with yarn registry', async () => {
176+
const cwd = await makeTemp();
177+
178+
const registry = 'https://registry.yarnpkg.com';
179+
const packageJsonPath = path.join(cwd, 'package.json');
180+
await fs.writeFile(packageJsonPath, JSON.stringify({}));
181+
182+
await runYarn(['add', 'is-array', '--registry', registry], {cwd});
183+
184+
const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
185+
const lockfile = explodeLockfile(await fs.readFile(path.join(cwd, 'yarn.lock')));
186+
187+
expect(packageJson.dependencies['is-array']).toBeDefined();
188+
expect(lockfile).toHaveLength(3);
189+
expect(lockfile[2]).toContain(registry);
190+
});
191+
192+
test('--registry option with non-exiting registry and show an error', async () => {
193+
const cwd = await makeTemp();
194+
const registry = 'https://example-registry-doesnt-exist.com';
195+
196+
try {
197+
await runYarn(['add', 'is-array', '--registry', registry], {cwd});
198+
} catch (err) {
199+
const stdoutOutput = err.message;
200+
expect(stdoutOutput.toString()).toMatch(/getaddrinfo ENOTFOUND example-registry-doesnt-exist\.com/g);
201+
}
202+
});
203+
});
204+
156205
test('--cwd option', async () => {
157206
const cwd = await makeTemp();
158207

src/cli/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export function main({
9191
commander.option('--cwd <cwd>', 'working directory to use', process.cwd());
9292
commander.option('--proxy <host>', '');
9393
commander.option('--https-proxy <host>', '');
94+
commander.option('--registry <url>', 'override configuration registry');
9495
commander.option('--no-progress', 'disable progress bar');
9596
commander.option('--network-concurrency <number>', 'maximum number of concurrent network requests', parseInt);
9697
commander.option('--network-timeout <milliseconds>', 'TCP timeout for network requests', parseInt);
@@ -477,6 +478,7 @@ export function main({
477478
production: commander.production,
478479
httpProxy: commander.proxy,
479480
httpsProxy: commander.httpsProxy,
481+
registry: commander.registry,
480482
networkConcurrency: commander.networkConcurrency,
481483
networkTimeout: commander.networkTimeout,
482484
nonInteractive: commander.nonInteractive,

src/config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type ConfigOptions = {
5656
httpsProxy?: ?string,
5757

5858
commandName?: ?string,
59+
registry?: ?string,
5960
};
6061

6162
type PackageMetadata = {
@@ -193,7 +194,6 @@ export default class Config {
193194

194195
getOption(key: string, expand: boolean = false): mixed {
195196
const value = this.registries.yarn.getOption(key);
196-
197197
if (expand && typeof value === 'string') {
198198
return expandPath(value);
199199
}
@@ -249,7 +249,9 @@ export default class Config {
249249

250250
// instantiate registry
251251
const registry = new Registry(this.cwd, this.registries, this.requestManager, this.reporter);
252-
await registry.init();
252+
await registry.init({
253+
registry: opts.registry,
254+
});
253255

254256
this.registries[key] = registry;
255257
this.registryFolders.push(registry.folder);

src/registries/base-registry.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ export default class BaseRegistry {
9999
});
100100
}
101101

102-
async init(): Promise<void> {
102+
async init(overrides: Object = {}): Promise<void> {
103103
this.mergeEnv('yarn_');
104104
await this.loadConfig();
105+
Object.assign(this.config, overrides);
105106
this.loc = path.join(this.cwd, this.folder);
106107
}
107108

0 commit comments

Comments
 (0)