Skip to content

Commit 0126704

Browse files
committed
feat: allow to configure the registry via NPM_CONFIG_REGISTRY
1 parent 07247ca commit 0126704

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ Use either `NPM_TOKEN` for token authentication or `NPM_USERNAME`, `NPM_PASSWORD
7272

7373
The plugin uses the [`npm` CLI](https://github.com/npm/cli) which will read the configuration from [`.npmrc`](https://docs.npmjs.com/files/npmrc). See [`npm config`](https://docs.npmjs.com/misc/config) for the option list.
7474

75-
The [`registry`](https://docs.npmjs.com/misc/registry) and [`dist-tag`](https://docs.npmjs.com/cli/dist-tag) can be configured in the `package.json` and will take precedence over the configuration in `.npmrc`:
75+
The [`registry`](https://docs.npmjs.com/misc/registry) can be configured via the npm environment variable `NPM_CONFIG_REGISTRY` and will take precedence over the configuration in `.npmrc`.
76+
77+
The [`registry`](https://docs.npmjs.com/misc/registry) and [`dist-tag`](https://docs.npmjs.com/cli/dist-tag) can be configured in the `package.json` and will take precedence over the configuration in `.npmrc` and `NPM_CONFIG_REGISTRY`:
7678
```json
7779
{
7880
"publishConfig": {

lib/get-registry.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const path = require('path');
22
const rc = require('rc');
33
const getRegistryUrl = require('registry-auth-token/registry-url');
44

5-
module.exports = ({publishConfig: {registry} = {}, name}, {cwd}) =>
6-
registry
7-
? registry
8-
: getRegistryUrl(
9-
name.split('/')[0],
10-
rc('npm', {registry: 'https://registry.npmjs.org/'}, {config: path.resolve(cwd, '.npmrc')})
11-
);
5+
module.exports = ({publishConfig: {registry} = {}, name}, {cwd, env}) =>
6+
registry ||
7+
env.NPM_CONFIG_REGISTRY ||
8+
getRegistryUrl(
9+
name.split('/')[0],
10+
rc('npm', {registry: 'https://registry.npmjs.org/'}, {config: path.resolve(cwd, '.npmrc')})
11+
);

test/get-registry.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,39 @@ import getRegistry from '../lib/get-registry';
66

77
test('Get default registry', t => {
88
const cwd = tempy.directory();
9-
t.is(getRegistry({name: 'package-name'}, {cwd}), 'https://registry.npmjs.org/');
10-
t.is(getRegistry({name: 'package-name', publishConfig: {}}, {cwd}), 'https://registry.npmjs.org/');
9+
t.is(getRegistry({name: 'package-name'}, {cwd, env: {}}), 'https://registry.npmjs.org/');
10+
t.is(getRegistry({name: 'package-name', publishConfig: {}}, {cwd, env: {}}), 'https://registry.npmjs.org/');
1111
});
1212

1313
test('Get the registry configured in ".npmrc" and normalize trailing slash', async t => {
1414
const cwd = tempy.directory();
1515
await appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom1.registry.com');
1616

17-
t.is(getRegistry({name: 'package-name'}, {cwd}), 'https://custom1.registry.com/');
17+
t.is(getRegistry({name: 'package-name'}, {cwd, env: {}}), 'https://custom1.registry.com/');
1818
});
1919

2020
test('Get the registry configured from "publishConfig"', async t => {
2121
const cwd = tempy.directory();
2222
await appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom2.registry.com');
2323

2424
t.is(
25-
getRegistry({name: 'package-name', publishConfig: {registry: 'https://custom3.registry.com/'}}, {cwd}),
25+
getRegistry({name: 'package-name', publishConfig: {registry: 'https://custom3.registry.com/'}}, {cwd, env: {}}),
2626
'https://custom3.registry.com/'
2727
);
2828
});
2929

30+
test('Get the registry configured in "NPM_CONFIG_REGISTRY"', t => {
31+
const cwd = tempy.directory();
32+
33+
t.is(
34+
getRegistry({name: 'package-name'}, {cwd, env: {NPM_CONFIG_REGISTRY: 'https://custom1.registry.com/'}}),
35+
'https://custom1.registry.com/'
36+
);
37+
});
38+
3039
test('Get the registry configured in ".npmrc" for scoped package', async t => {
3140
const cwd = tempy.directory();
3241
await appendFile(path.resolve(cwd, '.npmrc'), '@scope:registry = https://custom3.registry.com');
3342

34-
t.is(getRegistry({name: '@scope/package-name'}, {cwd}), 'https://custom3.registry.com/');
43+
t.is(getRegistry({name: '@scope/package-name'}, {cwd, env: {}}), 'https://custom3.registry.com/');
3544
});

0 commit comments

Comments
 (0)