In a previous post, I blogged about how to manage plugins with variables. I wanted to expand on that some more, and this time, talk about how to use your package.json to manage your plugins with versions as well as a way to reset your cordova set up.
The problem
Whenever I start a new Cordova project, I start by adding in all my plugins. Then once they are added, I’ll then commit them all and push the repository with all the plugins.
My workflow is usually like this:
- cordova create new ProjectApp
- cd ProjectApp
- cordova platform add ios
- cordova plugin add org.apache.cordova.camera
- cordova plugin add org.apache.cordova.contacts
- insert more plugin statements for every plugin we want
- cordova run ios
- cordova run android
Occassionaly, I run into this issue when I’m using plugins that require native variable hooks when installing. The prime example is the facebook plugin, it requires the APP_ID
to be passed in with the cordova plugin add
command with the options of --variable APP_ID="some_id"
.
What I’d rather do
It’d be nice to have these plugins being saved with their version, so when the next user needs to pull the plugins, or modify the installation, they can just modify the package.json and run a command to install them all. That way, we can get some kind of versioning on our plugins.
Ideally, I want to just type cordova setup
– have it look at my package.json file, and just begin installing what’s listed there.
Making the dream come true
First, lets start by putting our platforms and plugins in our package.json like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Automating Platforms
Now, we’ll need a script that will look at our package.json
and begin installing our platforms and plugins.
My platform installation script is located in the tasks
directory named platforms.js
, and looks like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Automating Plugins
My plugin installation script is also in my tasks
directory, and named plugins.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
Great. Now I don’t really need to add all the plugins, remove them, or worry about platforms. I can just run my scripts by doing node tasks/platforms.js
or node tasks/plugins.js
to have it set up my project as stated in my package.json
file.
Easier management for teams, I’d like to think.
Hope this helps others.