Automating the process of adding a Cordova platform and plugins for both cordova and ionic based projects.
Standard process of creating a cordova project and adding plugins:
- cordova create SampleApp com.example.sampleapp SampleApp
- cd SampleApp
- cordova platfrom add android
- cordova platfrom add ios
- cordova plugin add cordova-plugin-camera
- cordova plugin add cordova-plugin-device
- cordova run android
- cordova run ios
The Problem
- Mainatining and managaing all the plugins is a cumberson process.
- When the plugins are added a specific version of the plugin is added.
- If the plugins have to be modified or the project has to be updated then it would be easier to have all the plugins with their respective version.
The Solution
- Add all platfrom (with specific version) using a single command.
- Add all plugins (with specific version) using a single command.
- All the plugins and platforms are mentioned in a file - package.json
The key "platforms" has an array of platforms as value
"platforms": [
"android",
"ios"
]
The above snippet will add the latest cordova android platform version.
The version of platform can also be specificed as below:
"platforms": [
"[email protected]",
"ios"
]
The above snippet will add cordova android platform version 4.1.1
The key "plugins" has an array of plugins as value
"plugins": [
"http://github.com/don/cordova-filechooser.git",
"cordova-plugin-camera",
"cordova-plugin-device",
"cordova-plugin-dialogs",
"cordova-plugin-fcm",
"cordova-plugin-file",
"cordova-plugin-file-transfer",
"cordova-plugin-image-picker",
"cordova-plugin-inappbrowser",
"cordova-plugin-network-information",
"cordova-plugin-spinner-dialog",
"cordova-plugin-splashscreen"
]
The above snippet adds the latest version of each respective plugin.
A specific version of a plugin can be added as specified below:
"plugins": [
"[email protected]",
]
Automating the process
- Platform automation
The platform installation script platforms.js under tasks directory adds each platform
For running the platform script, type:
node tasks/platfroms.js (This command will add all the platforms mentioned)
node tasks/platfroms.js remove (This command will remove all the platforms mentioned)
- Plugin automation
The plugin installation script plugins.js under tasks directory adds each plugin
For running the plugin script, type:
node tasks/plugins.js (This command will add all the plugins mentioned)
node tasks/plugins.js remove (This command will remove all the plugins mentioned)
Note: Place the package.json and tasks folder under the root directory
This will be same for both ionic and cordova based hybrid projects.