{"id":1425,"date":"2014-02-17T16:06:59","date_gmt":"2014-02-17T22:06:59","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=1425"},"modified":"2014-02-17T16:07:37","modified_gmt":"2014-02-17T22:07:37","slug":"1425","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/1425","title":{"rendered":"Accessing more build information from your Cordova CLI hooks"},"content":{"rendered":"<p>Hooks now give you, as of cordova CLI 3.3.1-0.4.2, much more information about the environment you were executing in.<\/p>\n<p>Before, if I had platform specific code, I needed to check at runtime:<\/p>\n<pre>App.config.flurryid = \"\";\r\n    if (window.device && window.device.platform) {\r\n        if (window.device.platform == \"Android\") {\r\n            App.config.flurryid = \/*REP*\/ 'notreallyanandroidflurryid' \/*REP*\/ ;\r\n        }\r\n        if (window.device.platform == \"iOS\") {\r\n            App.config.flurryid = \/*REP*\/ 'notreallyaniosflurryidxxx' \/*REP*\/ ;\r\n        }\r\n    }<\/pre>\n<p>Now, you can inject platform specific code at build time, via hooks.  The runtime code is simplified to <\/p>\n<pre>App.config.flurryid = \/*REP*\/ 'myflurryid' \/*REP*\/;<\/pre>\n<p>and you just replace <code>'myflurryid'<\/code> with the appropriate value from your configuration file (<a href=\"http:\/\/devgirl.org\/2013\/11\/12\/three-hooks-your-cordovaphonegap-project-needs\/\">more on platform specific configuration files<\/a>).<\/p>\n<p>From the readme in the hooks directory: <\/p>\n<blockquote><p>All scripts are run from the project&#8217;s root directory and have the root directory passes as the first argument. All other options are passed to the script using environment variables:<\/p>\n<p>* CORDOVA_VERSION &#8211; The version of the Cordova-CLI.<br \/>\n* CORDOVA_PLATFORMS &#8211; Comma separated list of platforms that the command applies to (e.g.: android, ios).<br \/>\n* CORDOVA_PLUGINS &#8211; Comma separated list of plugin IDs that the command applies to (e.g.: org.apache.cordova.file, org.apache.cordova.file-transfer)<br \/>\n* CORDOVA_HOOK &#8211; Path to the hook that is being executed.<br \/>\n* CORDOVA_CMDLINE &#8211; The exact command-line arguments passed to cordova (e.g.: cordova run ios &#8211;emulate)<\/p><\/blockquote>\n<p>Here&#8217;s a sample plugin which you can put in your <code>hooks\/before_prepare<\/code> directory.  If you do this and then run <code>cordova -d prepare<\/code> you can see the values for all the variables.  <\/p>\n<pre>#!\/usr\/bin\/env node\r\n\r\nconsole.log(\"process.env.CORDOVA_VERSION: \"+process.env.CORDOVA_VERSION);\r\nconsole.log(\"process.env.CORDOVA_PLATFORMS: \"+process.env.CORDOVA_PLATFORMS);\r\nconsole.log(\"process.env.CORDOVA_PLUGINS: \"+process.env.CORDOVA_PLUGINS);\r\nconsole.log(\"process.env.CORDOVA_HOOK: \"+process.env.CORDOVA_HOOK);\r\nconsole.log(\"process.env.CORDOVA_CMDLINE: \"+process.env.CORDOVA_CMDLINE);<\/pre>\n<p>Here&#8217;s the output for a test project.<\/p>\n<pre>Executing hook \r\n\r\n\"\"\/home\/mooreds\/git\/testproject3\/hooks\/before_prepare\/test.js\" \r\n\r\n\"\/home\/mooreds\/git\/testproject3\"\"\r\nprocess.env.CORDOVA_VERSION: 3.3.1-0.4.2\r\nprocess.env.CORDOVA_PLATFORMS: android\r\nprocess.env.CORDOVA_PLUGINS: \r\nprocess.env.CORDOVA_HOOK: \/home\/mooreds\/git\/testproject3\/hooks\/before_prepare\/test.js\r\nprocess.env.CORDOVA_CMDLINE: node \/usr\/local\/bin\/cordova -d prepare<\/pre>\n<p>Note that if you are using this for platform specific code, you will want to run the commands separately: <code>cordova build android<\/code> and <code>cordova build ios<\/code>.  If you just run <code>cordova build<\/code> and you have both ios and android platforms installed, the <code>process.env.CORDOVA_PLATFORMS<\/code> variable will be <code>android,ios<\/code>, and your code won&#8217;t be able to differentiate between them.<\/p>\n<p>If you want to know about more Cordova CLI changes, check out <a href=\"http:\/\/devgirl.org\/2014\/02\/10\/3-cordova-cli-changes\/\">&#8220;3 Cordova CLI Changes You Should Know About&#8221;<\/a> which covers version 3.3.1-0.3.1.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hooks now give you, as of cordova CLI 3.3.1-0.4.2, much more information about the environment you were executing in. Before, if I had platform specific code, I needed to check [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,60],"tags":[],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-cordova-cli","category-phonegap"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1425"}],"version-history":[{"count":9,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1434,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1425\/revisions\/1434"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}