Skip to content

Commit

Permalink
Make boolean logic easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi authored and sindresorhus committed Dec 9, 2023
1 parent 819ed29 commit 777148d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions source/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,27 @@ const ui = async (options, {pkg, rootDir, isYarnBerry}) => {
}
}

const needsPrereleaseTag = answers => options.runPublish && (answers.version?.isPrerelease() || answers.customVersion?.isPrerelease()) && !options.tag;
const canBePublishedPublicly = options.availability.isAvailable && !options.availability.isUnknown && options.runPublish && (pkg.publishConfig && pkg.publishConfig.access !== 'restricted') && !npm.isExternalRegistry(pkg);
const needsPrereleaseTag = answers => (
options.runPublish
&& (answers.version?.isPrerelease() || answers.customVersion?.isPrerelease())
&& !options.tag
);

// Note that inquirer question.when is a bit confusing. Only `false` will cause the question to be skipped.
// Any other value like `true` and `undefined` means ask the question.
// so we make sure to always return an explicit boolean here to make it less confusing
// see https://github.com/SBoudrias/Inquirer.js/pull/1340
const needToAskForPublish = (() => {
if (!isScoped(pkg.name) || !options.availability.isAvailable || options.availability.isUnknown || !options.runPublish) {
return false;
}

if (!pkg.publishConfig) {
return true;
}

return pkg.publishConfig.access !== 'restricted' && !npm.isExternalRegistry(pkg);
})();

const answers = await inquirer.prompt({
version: {
Expand Down Expand Up @@ -318,7 +337,7 @@ const ui = async (options, {pkg, rootDir, isYarnBerry}) => {
},
publishScoped: {
type: 'confirm',
when: isScoped(pkg.name) && canBePublishedPublicly,
when: needToAskForPublish,
message: `This scoped repo ${chalk.bold.magenta(pkg.name)} hasn't been published. Do you want to publish it publicly?`,
default: false,
},
Expand Down

0 comments on commit 777148d

Please sign in to comment.