Skip to content

Execute the final npm publish command from a subdirectory? #423

Closed

Description

@dotansimha

Affected Packages

I guess cli.

Problem

We really want to introduce changesets in one of our largest projects (https://github.com/dotansimha/graphql-code-generator), but we have a limitation: our build process creates dist directory for each package, and inside we have a temporary package.json and all built files.

In tools like lerna, it's possible to set publishConfig.directory and this tell the publish command that the actual directory is different.

Proposed solution

I assume the entire flow should be the same, but these changes are needed:

  • If a different location needs to be published, then changesets version should be executed in order to update the original package.json with the new version number, and commit it.
  • Then, the custom build command should be executed (yarn build for example). This creates ./dist directory in each package of the monorepo, with the compiled code and new package.json (that already contains the updated versions, because of changesets version command)
  • changesets publish should take into account that publishConfig.directory might be set, and do everything the same way, except when it's running npm publish - it should use the subdiretory specified as cwd. This way it will execute it correctly and publish it from the sub-directory.

It seems like changing this line: https://github.com/atlassian/changesets/blob/37567dc495282bd498f8cfecd63d83eed20cbcaf/packages/cli/src/commands/publish/publishPackages.ts#L106 to:

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

Will do the trick?

UPDATE: Tested the change above with patch-package and it seems to work perfectly. You can find the patch file here: https://github.com/dotansimha/graphql-code-generator/blob/master/patches/%40changesets%2Bcli%2B2.9.2.patch

@Andarist

Activity

  1. Andarist commented on Aug 4, 2020

    @Andarist
    Member

    This got implemented in #428 🚀

  2. danawoodman commented on Aug 22, 2022

    @danawoodman

    For those getting here because they're trying to publish a package like a Svelte component library using svelte-kit package (and the associated ./package directory), you just need to add the following to your package.json to get it to work with changesets:

    {
    	"publishConfig": {
    		"directory": "./package"
    	}
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions