intuit / auto

Generate releases based on semantic version labels on pull requests.
https://intuit.github.io/auto/
MIT License
2.26k stars 204 forks source link

Support for NPM7 workspaces (monorepo) #2103

Open restfulhead opened 3 years ago

restfulhead commented 3 years ago

NPM 7 has introduced workspaces which allows us to manage multiple packages inside one repository without any external dependencies such as Lerna.

It would be fantastic if the auto npm implementation would support this. Basically it would need to parse the workspaces array in the root package.json and then run the commands for each workspace, e.g. npm publish -w my-first-module.

Could this be added?

restfulhead commented 2 years ago

I'm currently using the following workaround. Might be interesting for others:

  VERSION=`auto version`
  if [ ! -z "$VERSION" ]; then
    auto changelog
    npm version $VERSION -ws
    git add -A
    git commit -m "chore(release): $VERSION [skip ci]"
    npm version $VERSION -m "chore(release): %s [skip ci]"
    git push *** HEAD:main --tags
    VERSION_NO=`node -pe "require('./package.json').version"`
    auto release --use-version $VERSION_NO

    for workspace in $workspaces
    do
      npm publish $workspace/dist
    done

  else 
    echo 'Found skip release label, exiting'
    exit 1
  fi

This performs versioning, changelog, GitHub release at the root, but still publishes each workspace package to NPM individually. It works, but I'd much rather have independent releases :-)

tomdavidson commented 2 years ago

Looks like there is no abstraction around lerna but the software is small enough the change wouldn't be that big.

What about a workaround that copied npm workspaces to lerna packages in a lerna.json so auto could still detect its a monorepo and get each workspace? Then override the lerna execution in a hook?

tomdavidson commented 2 years ago

Actually what/who is using the exported getLernaPackages ?

Rather than the npm equivalent of lerna ls -pla we coud read the package.json and the lerna.json file. Reading the files rather than invoking a cli will give support whatever supports the workspace object in package.json

tomdavidson commented 2 years ago

@restfulhead
what about including a lerna.json file even if you dont use the the cli and configure it with "useWorkspaces": true

restfulhead commented 2 years ago

@tomdavidson I assumed that doing so would run Lerna commands. If it doesn't require Lerna at all, then that would be fine, yeah. I'll give it a try when I find some time.

NiccoloOlivieriAchille commented 1 year ago

Any news on this? More than one year and still no "npm workspaces" integration...

hipstersmoothie commented 1 year ago

The mono repo support depends heavily on lerna's versioning and publishing. You should need a very minimal setup like @tomdavidson said. It should work fine with npm workspaces. I don't think the npm cli has similar features

restfulhead commented 8 months ago

I'm unfamiliar with Lerna and didn't see a straight forward way to replace its commands with NPM. Instead, I wrote a plugin that filters out commits unrelated to the current workspace.

Basically the idea is to run auto version, auto changelog and auto release in each workspace separately, and then only include relevant commits. It works, but with a lot of caveats as mentioned in the readme.

hipstersmoothie commented 8 months ago

yeah the npm plugin is intentionally designed to let lerna do a lot of the heavy lifting. really you only need install it and add the configuration

Your use case is def not the intended flow for the npm plugin. I could certainly see that workflow being useful. I dream of someday making the mono repo functionality native to all different types of auto plugins. But really don't have the time