From 6bee8faf30c39ab4757a85d52525177a6a1aef1d Mon Sep 17 00:00:00 2001 From: zanminkian Date: Thu, 10 Apr 2025 08:20:53 +0000 Subject: [PATCH 1/4] Expose default url --- index.js | 8 ++++---- readme.md | 4 +++- test.js | 5 +++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3c5bb80..2acd4f9 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/readme.md b/readme.md index bcbdf67..0887493 100644 --- a/readme.md +++ b/readme.md @@ -20,10 +20,12 @@ 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). diff --git a/test.js b/test.js index 6c36245..089e465 100644 --- a/test.js +++ b/test.js @@ -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/'); +}); From 46bfe299e82f4fb749063ab81cfd36a935bb5665 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 10 Apr 2025 15:57:15 +0700 Subject: [PATCH 2/4] Update readme.md --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 0887493..326e95f 100644 --- a/readme.md +++ b/readme.md @@ -24,6 +24,7 @@ import registryUrl, {defaultUrl} from 'registry-url'; console.log(registryUrl()); //=> 'https://custom-registry.com/' + console.log(defaultUrl); // => 'https://registry.npmjs.org/' ``` From cfc869200cddfa1d9c375dd5d4e598edd171d243 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 10 Apr 2025 15:59:08 +0700 Subject: [PATCH 3/4] Update index.d.ts --- index.d.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2c9f2bd..fcd3f10 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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/' From 5c31c5a935fd529d5139c382b617a276a00121a0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 10 Apr 2025 15:59:34 +0700 Subject: [PATCH 4/4] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 326e95f..fe060c9 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ console.log(registryUrl()); //=> 'https://custom-registry.com/' console.log(defaultUrl); -// => 'https://registry.npmjs.org/' +//=> 'https://registry.npmjs.org/' ``` It can also retrieve the registry URL associated with an [npm scope](https://docs.npmjs.com/misc/scope).