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.
Here's a gist with node, bash & shell examples of how to extract the version from the root package.json.
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)\")
For my Angular library, I was forced to bump the version in the project package.json
file, the child one not the parent workspace one. So I needed to update the version property in the parent workspace package.json
file.
├── package.json └── projects └── lib-pwa └── package.json
For this I created in the ~/dev/commands/ng.sh
file the bash ng-version-sync-parent
command:
#!/bin/bash ng-version-sync-parent() { version=`find ./projects -name package.json -exec cat {} + | jq --raw-output '.version'`; echo 'Project package version: '$version; jq --arg version $version ".version = \"$version\"" package.json | sponge package.json version=`cat package.json | jq --raw-output '.version'`; echo 'Workspace package version: '$version; }
I then used it in my workspace directory:
source ~/dev/commands/ng.sh 11:29 $ ng-version-sync-parent Project package version: 0.1.4 Workspace package version: 0.1.4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With