diff --git a/lib/get-registry.js b/lib/get-registry.js index 4cb42dea..c38b1ca8 100644 --- a/lib/get-registry.js +++ b/lib/get-registry.js @@ -2,12 +2,15 @@ import path from "path"; import rc from "rc"; import getRegistryUrl from "registry-auth-token/registry-url.js"; -export default function ({ publishConfig: { registry } = {}, name }, { cwd, env }) { +export default function ({ publishConfig = {}, name }, { cwd, env }) { + const scope = name.split("/")[0]; + const publishRegistry = publishConfig[`${scope}:registry`] ?? publishConfig.registry; + return ( - registry || + publishRegistry || env.NPM_CONFIG_REGISTRY || getRegistryUrl( - name.split("/")[0], + scope, rc( "npm", { registry: "https://registry.npmjs.org/" }, diff --git a/test/get-registry.test.js b/test/get-registry.test.js index 2cad03f7..27b0469a 100644 --- a/test/get-registry.test.js +++ b/test/get-registry.test.js @@ -39,6 +39,26 @@ test('Get the registry configured in "NPM_CONFIG_REGISTRY"', (t) => { ); }); +test('Get the registry configured in "publishConfig" for scoped package', async (t) => { + const cwd = temporaryDirectory(); + await fs.appendFile(path.resolve(cwd, ".npmrc"), "@scope:registry = https://custom3.registry.com"); + await fs.appendFile(path.resolve(cwd, ".npmrc"), "registry = https://custom4.registry.com"); + + t.is( + getRegistry( + { + name: "@scope/package-name", + publishConfig: { + registry: "https://custom6.registry.com", + "@scope:registry": "https://custom5.registry.com", + }, + }, + { cwd, env: { NPM_CONFIG_REGISTRY: "https://custom1.registry.com/" } } + ), + "https://custom5.registry.com" + ); +}); + test('Get the registry configured in ".npmrc" for scoped package', async (t) => { const cwd = temporaryDirectory(); await fs.appendFile(path.resolve(cwd, ".npmrc"), "@scope:registry = https://custom3.registry.com");