Skip to content

Commit

Permalink
Deduplicate packageManager creation (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Mar 1, 2024
1 parent aa03498 commit 5aa226f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
16 changes: 14 additions & 2 deletions source/cli-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import logSymbols from 'log-symbols';
import meow from 'meow';
import updateNotifier from 'update-notifier';
import {gracefulExit} from 'exit-hook';
import {getPackageManagerConfig} from './package-manager/index.js';
import config from './config.js';
import * as util from './util.js';
import * as git from './git-util.js';
Expand Down Expand Up @@ -106,7 +107,7 @@ updateNotifier({pkg: cli.pkg}).notify();

/** @typedef {Awaited<ReturnType<typeof getOptions>>['options']} Options */

export async function getOptions() {
async function getOptions() {
const {package_, rootDirectory} = await util.readPackage(cli.flags.contents);

const localConfig = await config(rootDirectory);
Expand All @@ -124,6 +125,12 @@ export async function getOptions() {
package_.packageManager = flags.packageManager;
}

const packageManager = getPackageManagerConfig(rootDirectory, package_);

if (packageManager.throwOnExternalRegistry && npm.isExternalRegistry(package_)) {
throw new Error(`External registry is not yet supported with ${packageManager.id}.`);
}

const runPublish = !flags.releaseDraftOnly && flags.publish && !package_.private;

const availability = runPublish ? await npm.isPackageNameAvailable(package_) : {
Expand All @@ -138,13 +145,18 @@ export async function getOptions() {

const options = await ui({
...flags,
packageManager,
runPublish,
availability,
version,
branch,
}, {package_, rootDirectory});

return {options, rootDirectory, package_};
return {
options: {...options, packageManager},
rootDirectory,
package_,
};
}

try {
Expand Down
6 changes: 2 additions & 4 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {getPackagePublishArguments} from './npm/publish.js';
import enable2fa, {getEnable2faArguments} from './npm/enable-2fa.js';
import handleNpmError from './npm/handle-npm-error.js';
import releaseTaskHelper from './release-task-helper.js';
import {findLockfile, getPackageManagerConfig, printCommand} from './package-manager/index.js';
import {findLockfile, printCommand} from './package-manager/index.js';
import * as util from './util.js';
import * as git from './git-util.js';
import * as npm from './npm/util.js';
Expand All @@ -36,9 +36,7 @@ const exec = (command, arguments_, options) => {
@param {import('./cli-implementation.js').Options} options
@param {{package_: import('read-pkg').NormalizedPackageJson; rootDirectory: string}} context
*/
const np = async (input = 'patch', options, {package_, rootDirectory}) => {
const packageManager = getPackageManagerConfig(rootDirectory, package_);

const np = async (input = 'patch', {packageManager, ...options}, {package_, rootDirectory}) => {
// TODO: Remove sometime far in the future
if (options.skipCleanup) {
options.cleanup = false;
Expand Down
9 changes: 1 addition & 8 deletions source/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {htmlEscape} from 'escape-goat';
import isScoped from 'is-scoped';
import isInteractive from 'is-interactive';
import {execa} from 'execa';
import {getPackageManagerConfig} from './package-manager/index.js';
import Version, {SEMVER_INCREMENTS} from './version.js';
import * as util from './util.js';
import * as git from './git-util.js';
Expand Down Expand Up @@ -125,17 +124,11 @@ const checkNewFilesAndDependencies = async (package_, rootDirectory) => {
@param {import('./cli-implementation.js').CLI['flags']} options
@param {{package_: import('read-pkg').NormalizedPackageJson; rootDirectory: string}} context
*/
const ui = async (options, {package_, rootDirectory}) => {
const ui = async ({packageManager, ...options}, {package_, rootDirectory}) => { // eslint-disable-line complexity
const oldVersion = package_.version;
const extraBaseUrls = ['gitlab.com'];
const repoUrl = package_.repository && githubUrlFromGit(package_.repository.url, {extraBaseUrls});

const packageManager = getPackageManagerConfig(rootDirectory, package_);

if (packageManager.throwOnExternalRegistry && npm.isExternalRegistry(package_)) {
throw new Error(`External registry is not yet supported with ${packageManager.id}.`);
}

const {stdout: registryUrl} = await execa(...packageManager.getRegistryCommand);
const releaseBranch = options.branch;

Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import test from 'ava';
import sinon from 'sinon';
import esmock from 'esmock';
import {npmConfig as packageManager} from '../source/package-manager/configs.js';
import * as util from '../source/util.js';
import np from '../source/index.js';

const defaultOptions = {
cleanup: true,
tests: true,
publish: true,
packageManager,
runPublish: true,
availability: {
isAvailable: false,
Expand Down
3 changes: 2 additions & 1 deletion test/ui/new-files-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {execa} from 'execa';
import {removePackageDependencies, updatePackage} from 'write-package';
import stripAnsi from 'strip-ansi';
import {readPackage} from 'read-pkg';
import {npmConfig as packageManager} from '../../source/package-manager/configs.js';
import {createIntegrationTest} from '../_helpers/integration-test.js';
import {mockInquirer} from '../_helpers/mock-inquirer.js';

Expand Down Expand Up @@ -52,7 +53,7 @@ const createFixture = test.macro(async (t, package_, commands, expected) => {
},
});

await ui({runPublish: true, version: 'major', yarn: false}, {package_, rootDirectory: temporaryDirectory});
await ui({runPublish: true, version: 'major', packageManager}, {package_, rootDirectory: temporaryDirectory});
const logs = logsArray.join('').split('\n').map(log => stripAnsi(log));

const {unpublished, firstTime, dependencies} = expected;
Expand Down
2 changes: 2 additions & 0 deletions test/ui/prompts/tags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import sinon from 'sinon';
import {npmConfig as packageManager} from '../../../source/package-manager/configs.js';
import {npPackage} from '../../../source/util.js';
import {mockInquirer} from '../../_helpers/mock-inquirer.js';

Expand All @@ -25,6 +26,7 @@ const testUi = test.macro(async (t, {version, tags, answers}, assertions) => {
});

const results = await ui({
packageManager,
runPublish: true,
availability: {},
}, {
Expand Down
2 changes: 2 additions & 0 deletions test/ui/prompts/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import sinon from 'sinon';
import {npmConfig as packageManager} from '../../../source/package-manager/configs.js';
import {mockInquirer} from '../../_helpers/mock-inquirer.js';

const testUi = test.macro(async (t, {version, answers}, assertions) => {
Expand All @@ -20,6 +21,7 @@ const testUi = test.macro(async (t, {version, answers}, assertions) => {
});

const results = await ui({
packageManager,
runPublish: false,
availability: {},
}, {
Expand Down

0 comments on commit 5aa226f

Please sign in to comment.