Skip to content

Commit

Permalink
publishCli + prefix args -> publishCommand function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Feb 23, 2024
1 parent d9ca565 commit 3163835
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
17 changes: 9 additions & 8 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const exec = (cmd, args, options) => {
*/
const np = async (input = 'patch', options, {pkg, rootDir}) => {
const pkgManager = getPackageManagerConfig(rootDir, pkg);
const [publishCommand, publishArgsPrefix = []] = pkgManager.publishCli || [pkgManager.cli];

// TODO: Remove sometime far in the future
if (options.skipCleanup) {
Expand Down Expand Up @@ -96,9 +95,11 @@ const np = async (input = 'patch', options, {pkg, rootDir}) => {
// To prevent the process from hanging due to watch mode (e.g. when running `vitest`)
const ciEnvOptions = {env: {CI: 'true'}};

function getPackagePublishArguments(options) {
const args = getAdditionalPackagePublishArguments(options);
return [...publishArgsPrefix, ...args];
/** @param {typeof options} _options */
function getPublishCommand(_options) {
const publishCommand = pkgManager.publishCommand || (args => [pkgManager.cli, args]);
const args = getAdditionalPackagePublishArguments(_options);
return publishCommand(args);
}

const tasks = new Listr([
Expand Down Expand Up @@ -166,20 +167,20 @@ const np = async (input = 'patch', options, {pkg, rootDir}) => {
title: 'Publishing package',
skip() {
if (options.preview) {
const args = getPackagePublishArguments(options);
return `[Preview] Command not executed: ${publishCommand} ${args.join(' ')}.`;
const command = getPublishCommand(options);
return `[Preview] Command not executed: ${printCommand(command)}.`;
}
},
/** @type {(context, task) => Listr.ListrTaskResult<any>} */
task(context, task) {
let hasError = false;

return from(execa(publishCommand, getPackagePublishArguments(options)))
return from(execa(...getPublishCommand(options)))
.pipe(
catchError(error => handleNpmError(error, task, otp => {
context.otp = otp;

return execa(publishCommand, getPackagePublishArguments({...options, otp}));
return execa(...getPublishCommand({...options, otp}));
})),
)
.pipe(
Expand Down
3 changes: 1 addition & 2 deletions source/package-manager/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const pnpmConfig = {
installCommandNoLockfile: ['pnpm', ['install']],
versionCommand: version => ['pnpm', ['version', version]],
tagVersionPrefixCommand: ['pnpm', ['config', 'get', 'tag-version-prefix']],
publishCli: ['npm'], // Pnpm does git cleanliness checks, which np already did, and which fail because when publishing package.json has been updated
getRegistryCommand: ['pnpm', ['config', 'get', 'registry']],
lockfiles: ['pnpm-lock.yaml'],
};
Expand All @@ -45,7 +44,7 @@ export const yarnBerryConfig = {
versionCommand: version => ['npm', ['version', version]],
tagVersionPrefixCommand: ['yarn', ['config', 'get', 'version-tag-prefix']],
// Yarn berry offloads publishing to npm, e.g. `yarn npm publish x.y.z`
publishCli: ['yarn', ['npm']],
publishCommand: args => ['yarn', ['npm', ...args]],
getRegistryCommand: ['yarn', ['config', 'get', 'npmRegistryServer']],
throwOnExternalRegistry: true,
lockfiles: ['yarn.lock'],
Expand Down
6 changes: 3 additions & 3 deletions source/package-manager/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export type PackageManagerConfig = {
/**
Given a version string, return a version command e.g. `version => ["npm", ["version", version]]`.
*/
versionCommand: (version: string) => [command: string, args: string[]];
versionCommand: (version: string) => [cli: string, args: string[]];

/**
Use a different CLI (and prefix args) to do the actual publish. Defaults to [`cli`].
Modify the actual publish command. Defaults to `args => [config.cli, args]`.
*/
publishCli?: [command: string, prefixArgs?: string[]];
publishCommand?: (args: string[]) => Command;

/**
CLI command which is expected to output the npm registry to use, e.g. `['npm', ['config', 'get', 'registry']]`.
Expand Down

0 comments on commit 3163835

Please sign in to comment.