Skip to content

Commit 15040c8

Browse files
committed
Fix getNpmPackageAccess to respect publishConfig.registry
Fixes #763
1 parent 380fd24 commit 15040c8

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

source/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ const ui = async ({packageManager, ...options}, {package_, rootDirectory}) => {
228228
&& !options.tag
229229
);
230230

231-
const alreadyPublicScoped = packageManager.id === 'yarn-berry' && options.runPublish && await util.getNpmPackageAccess(package_.name) === 'public';
231+
const alreadyPublicScoped = packageManager.id === 'yarn-berry' && options.runPublish && await util.getNpmPackageAccess(package_) === 'public';
232232

233233
// Note that inquirer question.when is a bit confusing. Only `false` will cause the question to be skipped.
234234
// Any other value like `true` and `undefined` means ask the question.

source/util.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,13 @@ export const validateEngineVersionSatisfies = (engine, version) => {
155155
}
156156
};
157157

158-
export async function getNpmPackageAccess(name) {
159-
const {stdout} = await execa('npm', ['access', 'get', 'status', name, '--json']);
160-
return JSON.parse(stdout)[name]; // Note: returns "private" for non-existent packages
158+
export async function getNpmPackageAccess(package_) {
159+
const arguments_ = ['access', 'get', 'status', package_.name, '--json'];
160+
161+
if (package_.publishConfig?.registry) {
162+
arguments_.push('--registry', package_.publishConfig.registry);
163+
}
164+
165+
const {stdout} = await execa('npm', arguments_);
166+
return JSON.parse(stdout)[package_.name]; // Note: returns "private" for non-existent packages
161167
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import test from 'ava';
2+
import {_createFixture} from '../_helpers/stub-execa.js';
3+
4+
/** @type {ReturnType<typeof _createFixture<import('../../source/util.js')>>} */
5+
const createFixture = _createFixture('../../source/util.js', import.meta.url);
6+
7+
test('main', createFixture, [{
8+
command: 'npm access get status @my/pkg --json',
9+
stdout: '{"@my/pkg": "public"}',
10+
}], async ({t, testedModule: {getNpmPackageAccess}}) => {
11+
t.is(
12+
await getNpmPackageAccess({name: '@my/pkg'}),
13+
'public',
14+
);
15+
});
16+
17+
test('with publishConfig.registry', createFixture, [{
18+
command: 'npm access get status @my/pkg --json --registry https://registry.npmjs.org/',
19+
stdout: '{"@my/pkg": "public"}',
20+
}], async ({t, testedModule: {getNpmPackageAccess}}) => {
21+
t.is(
22+
await getNpmPackageAccess({
23+
name: '@my/pkg',
24+
publishConfig: {
25+
registry: 'https://registry.npmjs.org/',
26+
},
27+
}),
28+
'public',
29+
);
30+
});
31+
32+
test('with external registry', createFixture, [{
33+
command: 'npm access get status @my/pkg --json --registry http://my-internal-registry.local',
34+
stdout: '{"@my/pkg": "private"}',
35+
}], async ({t, testedModule: {getNpmPackageAccess}}) => {
36+
t.is(
37+
await getNpmPackageAccess({
38+
name: '@my/pkg',
39+
publishConfig: {
40+
registry: 'http://my-internal-registry.local',
41+
},
42+
}),
43+
'private',
44+
);
45+
});

0 commit comments

Comments
 (0)