When building an Angular Library application, is there an automated way to keep the root level package.json and the library application's package.json (e.g. under projects/my-library
) file versions in sync when using npm version
commands?
When I use the command, it's only incrementing the root level package.json, is there a special command, or any other way to propagate the version number down to the library package.json file?
I have seen some solutions such as running a script after ng build
to read the root level package.json version number and write that into the library one but I'm not sure this is the best approach.
Has anyone else faced this when building libraries in Angular and if so what was your way to deal with it?
I've seen people ask the same question to vsavkin (Nrwl/Nx dude) and his response was a fairly involved git sub-module approach.
While this workflow is a little complex, it does allow shared libraries to be individually versioned whilst keeping them in the same parent git-repo.
A simpler alternative is, as you have already mentioned, to keep all libraries in a monorepo the same version as the root package.json - similar to how the Angular monorepo bumps its versions of all the Angular packages at the same time.
This can be done easily in node, or in bash/shell with a little bit more scripting.
To actually bump the versions of the packages here's a simple approach: install this npm package (a nice CLI for editing json files) and read the In-Place editing section. You should now have all the tools you need!
Here's an example bash script using node:
cd libs/my-library && json -I -f package.json this.version=$(node -pe \"JSON.stringify(require('../../package.json').version)\")