Managing plugin updates in Cordova
Over the last couple of years, the Cordova team have made it much easier to manage plugin dependencies, with great CLI tooling for adding and removing individual plugins. With Cordova 5 we also saw a move to NPM as the canonical plugin repository. This was great as it also brought about the ability to store specific plugin versions inside your config.xml
file, e.g.
<plugin name="cordova-plugin-device" spec="^1.0.0" />
This is brilliant for teams of multiple developers working on the same project or if you need to come back to a project after a period of time and have to get it re-setup on a new computer.
Updating manually
At the time of writing, the Cordova CLI only provides add
and remove
functionality. For ongoing maintenance of an app, this doesn’t make it terribly easy to keep plugins updated. The only real option to update to a new version of a plugin is to do a remove
followed by an add
, which will install the latest available release. e.g.
cordova plugin remove cordova-plugin-device --save
cordova plugin add cordova-plugin-device --save
This is fine in principle but doesn’t give you much control over which version you want to install.
It is possible using the Cordova CLI to install a specific version, but this requires you to check NPM yourself (either the website or via CLI).
We can do better
Most of this manual effort could be done automatically, so I wrote a small CLI tool which checks NPM for newer versions of all plugins defined in your config.xml
file. The script is available via NPM, installation is as simple as:
npm install -g cordova-plugin-update
You then run cordova-plugin-update
from the project folder that contains your config.xml
file and let the tool do the hard work for you.
Example output:
$ cordova-plugin-update
A newer versions of `cordova-plugin-whitelist` is available (currently 1.0.0)
1) 1.1.0
2) 1.0.1
3) Don't update
Install (1/2/3)?
As you can see from the example above, the tool will attempt to show you all versions that are newer than you currently have installed, giving you the option of which to install.
Rolling back
It might be that after installing an updated version of the plugin an incompatibility is found with you app and you want to roll back to a previous version. In this instance we simple add the --all
option e.g.
cordova-plugin-update --all
This will show all version (other than the one currently installed) and let you pick which one you’d like to install.
Wrapping up
This tool has proved really useful for us at Rareloop, taking some of the headache out of maintaining the apps we produce. Your mileage may vary but my hope is that it will be useful for others too (if it is I’d love to hear from you). The code is on GitHub if you want to see how it works or have a play, please open issues or make pull requests if you want to help improve it.