Cordova CLI, using Git, and saving plugins/platforms
Asked Answered
G

2

10

I'm trying to figure out how to reconcile some Cordova + git "best practices" with what I think is reality, and I'm hoping someone can shed some light on this for me.

If I understand correctly, the current "best practice" is to add these directories to my .gitignore (from the book "Developing with Cordova CLI", the current version):

platforms/
plugins/
node_modules/

This removes the easily downloadable plugins and mostly boilerplate platform code from version control because it can be easily generated with a simple Cordova CLI command.

But, this seems counter-intuitive because - and I'm thinking like NPM or Bower - with the Cordova CLI I can't save which platforms and plugins I'm using in a config file. With NPM, I can add a --save switch to save the package in the package.json file. This allows me to not version control my node_modules folder and instead use 'npm install'. With the Cordova CLI I can't seem to use the --save switch (is there an equivalent) to 'remember' the plugins or platforms I intend to use.

It seems that the config.xml file in the www/ directory doesn't save which platforms or plugins have been added.

Is there some other file in the project that keeps a memory of which platforms and plugins I want to use? How does it work?

Graven answered 14/2, 2014 at 23:29 Comment(0)
G
3

Cordova 4.3.0 + allows you to save and restore platforms and plugins. Saved information is stored in config.xml file. See v5.0.0 release notes and the official Cordova docs.

You can save platforms and plugins using the --save option when you add them:

cordova platforms add PLATFORM --save
cordova plugins add PLUGIN --save

Or you can save platforms and plugins that are currently added:

cordova platforms save
cordova plugins save

By doing this there is no need to check in platforms or plugins into your code repository. They will be automatically restored based on your config.xml file when cordova prepare command is run.

Gies answered 5/8, 2015 at 19:15 Comment(0)
F
2

I typically write a hook to capture the plugins I want to use in my project. You can see this in an article I wrote here: http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/

With the new modular architecture of Cordova 3.x, every app needs plugins, even to use basic functionality such as logging or geolocation. Rather than document which plugins/features your project needs and ask each new developer to install them, download and install them automatically with a hook in the after_platform_add step. Using this plugin, every time a developer checks out the project and adds a platform, they automatically have the required plugins.

You also may be interested in following along with this bug, which suggests npm style --save functionality: https://issues.apache.org/jira/browse/CB-5775

Platforms are a little more difficult because they don't fit into the hook architecture, but you could write a shell script which you could execute to add your platforms.

#!/bin/sh
for plat in ios android; do
   cordova platform add $plat
done

You could do something similar with the version of cordova you have installed in node_modules (at least that is what I think you are installing in node_modules)--have shell script to get the correct version of cordova:

#!/bin/sh
VERSION=3.3.1-0.4.2
npm install cordova@$VERSION

PS Glad you liked the book!

Flugelhorn answered 16/2, 2014 at 3:19 Comment(2)
Thanks! The hook sort of works. I get an error when running the command cordova platform add android on Windows 7. Running command: cmd args=["/c","C:\\Program Files\\nodejs\\node.exe","C:\\path\\Dev\\GitHub\\app\\.cordova\\hook s\\before_platform_ls\\010_install_plugins.js","C:\\path\\Dev\\GitHub\\app"] 'C:\Program' is not recognized as an internal or external command, operable program or batch file. Hook failed with error code 1: node However, I can run the .js file directly with just node \path\to\hook.js and there is no error. I'm not sure what's going on...Graven
It looks, at first blush, like the command line interpreter doesn't like that space in 'Program Files' when it is trying to execute node. I would try to install node.js in c:\node or something like that and see if you still get that error.Flugelhorn

© 2022 - 2024 — McMap. All rights reserved.