Skip to content
Merged
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
24 changes: 19 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ Get the set npm registry URL.
@param scope - Retrieve the registry URL associated with an [npm scope](https://docs.npmjs.com/misc/scope). If the provided scope is not in the user's `.npmrc` file, then `registry-url` will check for the existence of `registry`, or if that's not set, fallback to the default npm registry.

@example

```ini
# .npmrc
registry = 'https://custom-registry.com/'
```
import registryUrl from 'registry-url';

// # .npmrc
// registry = 'https://custom-registry.com/'
```js
import registryUrl, {defaultUrl} from 'registry-url';

console.log(registryUrl());
//=> 'https://custom-registry.com/'

// # .npmrc
// @myco:registry = 'https://custom-registry.com/'
console.log(defaultUrl);
//=> 'https://registry.npmjs.org/'
```

It can also retrieve the registry URL associated with an [npm scope](https://docs.npmjs.com/misc/scope).

```ini
# .npmrc
@myco:registry = 'https://custom-registry.com/'
```

```js
import registryUrl from 'registry-url';

console.log(registryUrl('@myco'));
//=> 'https://custom-registry.com/'
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import process from 'node:process';
import {findUpSync} from 'find-up-simple';
import {parse} from 'ini';

export const defaultUrl = 'https://registry.npmjs.org/';

const normalize = url => url.endsWith('/') ? url : `${url}/`;

export default function registryUrl(scope) {
const defaultRegistry = 'https://registry.npmjs.org/';

const npmRcPath = findUpSync('.npmrc');
if (!npmRcPath) {
return normalize(process.env.npm_config_registry || defaultRegistry);
return normalize(process.env.npm_config_registry || defaultUrl);
}

const content = readFileSync(npmRcPath, 'utf8');
const result = parse(content);
return normalize(result[`${scope}:registry`] || process.env.npm_config_registry || result.registry || defaultRegistry);
return normalize(result[`${scope}:registry`] || process.env.npm_config_registry || result.registry || defaultUrl);
}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ registry = 'https://custom-registry.com/'
```

```js
import registryUrl from 'registry-url';
import registryUrl, {defaultUrl} from 'registry-url';

console.log(registryUrl());
//=> 'https://custom-registry.com/'

console.log(defaultUrl);
//=> 'https://registry.npmjs.org/'
```

It can also retrieve the registry URL associated with an [npm scope](https://docs.npmjs.com/misc/scope).
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ test('add trailing slash to local scope registry URL', async t => {
const {default: registryUrl} = await importFresh('./index.js');
t.is(registryUrl('@myco'), 'http://reg.example.com/');
});

test('default npm registry url is https://registry.npmjs.org/', async t => {
const {defaultUrl} = await importFresh('./index.js');
t.is(defaultUrl, 'https://registry.npmjs.org/');
});