Skip to content

Commit

Permalink
Call pnpm publish directly from the directory of the published pack…
Browse files Browse the repository at this point in the history
…age (#1115)
  • Loading branch information
Andarist authored Mar 22, 2023
1 parent 5c53bff commit feddc88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-fans-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/cli": patch
---

Call `pnpm publish` directly from the directory of the published package. This allows `pnpm` to correctly handle configured `publishConfig.directory`.
34 changes: 24 additions & 10 deletions packages/cli/src/commands/publish/npm-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExitError } from "@changesets/errors";
import { error, info, warn } from "@changesets/logger";
import { PackageJSON } from "@changesets/types";
import { AccessType, PackageJSON } from "@changesets/types";
import pLimit from "p-limit";
import preferredPM from "preferred-pm";
import chalk from "chalk";
Expand All @@ -11,6 +11,13 @@ import isCI from "is-ci";
import { TwoFactorState } from "../../utils/types";
import { getLastJsonObjectFromString } from "../../utils/getLastJsonObjectFromString";

interface PublishOptions {
cwd: string;
publishDir: string;
access: AccessType;
tag: string;
}

const npmRequestLimit = pLimit(40);
const npmPublishLimit = pLimit(10);

Expand Down Expand Up @@ -155,12 +162,13 @@ export let getOtpCode = async (twoFactorState: TwoFactorState) => {
// the call being wrapped in the npm request limit and causing the publishes to potentially never run
async function internalPublish(
pkgName: string,
opts: { cwd: string; access?: string; tag: string },
opts: PublishOptions,
twoFactorState: TwoFactorState
): Promise<{ published: boolean }> {
let publishTool = await getPublishTool(opts.cwd);
let publishFlags = opts.access ? ["--access", opts.access] : [];
publishFlags.push("--tag", opts.tag);

if ((await twoFactorState.isRequired) && !isCI) {
let otpCode = await getOtpCode(twoFactorState);
publishFlags.push("--otp", otpCode);
Expand All @@ -174,13 +182,19 @@ async function internalPublish(
const envOverride = {
npm_config_registry: getCorrectRegistry(),
};
let { code, stdout, stderr } = await spawn(
publishTool.name,
["publish", opts.cwd, "--json", ...publishFlags],
{
env: Object.assign({}, process.env, envOverride),
}
);
let { code, stdout, stderr } =
publishTool.name === "pnpm"
? await spawn("pnpm", ["publish", "--json", ...publishFlags], {
env: Object.assign({}, process.env, envOverride),
cwd: opts.cwd,
})
: await spawn(
publishTool.name,
["publish", opts.publishDir, "--json", ...publishFlags],
{
env: Object.assign({}, process.env, envOverride),
}
);
if (code !== 0) {
// NPM's --json output is included alongside the `prepublish` and `postpublish` output in terminal
// We want to handle this as best we can but it has some struggles:
Expand Down Expand Up @@ -222,7 +236,7 @@ async function internalPublish(

export function publish(
pkgName: string,
opts: { cwd: string; access?: string; tag: string },
opts: PublishOptions,
twoFactorState: TwoFactorState
): Promise<{ published: boolean }> {
// If there are many packages to be published, it's better to limit the
Expand Down
12 changes: 5 additions & 7 deletions packages/cli/src/commands/publish/publishPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,18 @@ async function publishAPackage(
tag: string
): Promise<PublishedResult> {
const { name, version, publishConfig } = pkg.packageJson;
const localAccess = publishConfig?.access;
info(
`Publishing ${chalk.cyan(`"${name}"`)} at ${chalk.green(`"${version}"`)}`
);

const publishDir = publishConfig?.directory
? join(pkg.dir, publishConfig.directory)
: pkg.dir;

const publishConfirmation = await npmUtils.publish(
name,
{
cwd: publishDir,
access: localAccess || access,
cwd: pkg.dir,
publishDir: publishConfig?.directory
? join(pkg.dir, publishConfig.directory)
: pkg.dir,
access: publishConfig?.access || access,
tag,
},
twoFactorState
Expand Down

0 comments on commit feddc88

Please sign in to comment.