auto icon indicating copy to clipboard operation
auto copied to clipboard

Support for NPM7 workspaces (monorepo)

Open restfulhead opened this issue 4 years ago • 9 comments

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 avatar Oct 28 '21 22:10 restfulhead

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 :-)

restfulhead avatar Nov 15 '21 22:11 restfulhead

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 avatar Dec 09 '21 09:12 tomdavidson

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 avatar Dec 09 '21 16:12 tomdavidson

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

tomdavidson avatar Dec 10 '21 00:12 tomdavidson

@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.

restfulhead avatar Dec 10 '21 18:12 restfulhead

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

NiccoloOlivieriAchille avatar May 11 '23 15:05 NiccoloOlivieriAchille

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

hipstersmoothie avatar May 11 '23 16:05 hipstersmoothie

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.

restfulhead avatar Feb 23 '24 23:02 restfulhead

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

hipstersmoothie avatar Feb 24 '24 00:02 hipstersmoothie