Skip to content

Commit 8d3a984

Browse files
mifisindresorhus
authored andcommittedDec 9, 2023
Fix yarn npm publish for scoped packages
1 parent 777148d commit 8d3a984

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed
 

‎source/ui.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const checkNewFilesAndDependencies = async (pkg, rootDir) => {
120120
};
121121

122122
// eslint-disable-next-line complexity
123-
const ui = async (options, {pkg, rootDir, isYarnBerry}) => {
123+
const ui = async (options, {pkg, rootDir, isYarnBerry = false}) => {
124124
const oldVersion = pkg.version;
125125
const extraBaseUrls = ['gitlab.com'];
126126
const repoUrl = pkg.repository && githubUrlFromGit(pkg.repository.url, {extraBaseUrls});
@@ -241,12 +241,14 @@ const ui = async (options, {pkg, rootDir, isYarnBerry}) => {
241241
&& !options.tag
242242
);
243243

244+
const alreadyPublicScoped = isYarnBerry && options.runPublish && await util.getNpmPackageAccess(pkg.name) === 'public';
245+
244246
// Note that inquirer question.when is a bit confusing. Only `false` will cause the question to be skipped.
245247
// Any other value like `true` and `undefined` means ask the question.
246248
// so we make sure to always return an explicit boolean here to make it less confusing
247249
// see https://github.com/SBoudrias/Inquirer.js/pull/1340
248250
const needToAskForPublish = (() => {
249-
if (!isScoped(pkg.name) || !options.availability.isAvailable || options.availability.isUnknown || !options.runPublish) {
251+
if (alreadyPublicScoped || !isScoped(pkg.name) || !options.availability.isAvailable || options.availability.isUnknown || !options.runPublish) {
250252
return false;
251253
}
252254

@@ -347,7 +349,7 @@ const ui = async (options, {pkg, rootDir, isYarnBerry}) => {
347349
...options,
348350
version: answers.version || answers.customVersion || options.version,
349351
tag: answers.tag || answers.customTag || options.tag,
350-
publishScoped: answers.publishScoped,
352+
publishScoped: alreadyPublicScoped || answers.publishScoped,
351353
confirm: true,
352354
repoUrl,
353355
releaseNotes,

‎source/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,8 @@ export const validateEngineVersionSatisfies = (engine, version) => {
150150
throw new Error(`\`np\` requires ${engine} ${engineRange}`);
151151
}
152152
};
153+
154+
export async function getNpmPackageAccess(name) {
155+
const {stdout} = await execa('npm', ['access', 'get', 'status', name, '--json']);
156+
return JSON.parse(stdout)[name]; // Note: returns "private" for non-existent packages
157+
}

0 commit comments

Comments
 (0)
Please sign in to comment.