{"id":1197,"date":"2013-07-20T11:38:33","date_gmt":"2013-07-20T17:38:33","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=1197"},"modified":"2013-07-23T14:53:34","modified_gmt":"2013-07-23T20:53:34","slug":"hooks-and-cordova-cli","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/1197","title":{"rendered":"Hooks and Cordova CLI"},"content":{"rendered":"<p>Hooks are scripts that you can run before and after <a href=\"https:\/\/github.com\/apache\/cordova-cli#project_commands\">each stage of the Cordova CLI lifecycle<\/a>.  They live in a <code>projecthome\/.cordova\/hooks\/before_xxx<\/code> (or after_xxx) where xxx is the project lifecycle stage (prepare, build, etc).<\/p>\n<p>They are <a href=\"https:\/\/github.com\/apache\/cordova-cli#hooks\">somewhat documented here<\/a>, and <a href=\"https:\/\/gist.github.com\/dpogue\/4100866\">here&#8217;s an example<\/a> you can &#8220;borrow&#8221; from.  As we&#8217;ll see in the next few posts, they are a lifesaver at dealing with some of the current deficiencies of Cordova CLI.<\/p>\n<p>But to start with, all you need to know is that hooks are chunks of code that are passed the project base directory as the first argument (and therefore available via <code>var rootdir = process.argv[2];<\/code> for node, or <code>$1<\/code> for shell scripts).  These chunks can be any kind of executable code (shell\/node\/perl\/python\/compiled c, etc).  All I focus on here are project specific hooks&#8211;I don&#8217;t know how module hooks would work.<\/p>\n<p>I have typically used hooks to move and manipulate files and used node (though you can use any scripting language) and am a node newbie&#8211;therefore stackoverflow and the <a href=\"http:\/\/nodejs.org\/api\/fs.html\">node filesystem API docs<\/a> were my friends.<\/p>\n<p>Each hook is executed in alphabetical order within the before_ or after_ directory.  A good idea is to name each hook to make the order explict:<\/p>\n<ul>\n<li>001_script<\/li>\n<li>010_script<\/li>\n<li>020_script<\/li>\n<li>120_script<\/li>\n<\/ul>\n<p>executes scripts in the order you would expect, where:<\/p>\n<ul>\n<li>1_script<\/li>\n<li>10_script<\/li>\n<li>20_script<\/li>\n<li>120_script<\/li>\n<\/ul>\n<p>does not.<\/p>\n<p>In addition, if you write node scripts, consider using the synchronous versions of the file manipulation commands.  While the default node commands (to move a file, say) are asynchronous, and therefore fast, for a build environment I have seen confusing results if I didn&#8217;t make everything synchronous.  If you need the build process to be faster, then consider going async, but know what you are doing.<\/p>\n<p>In the next post, I will discuss configuration for different environments.<\/p>\n<p><a href=\"http:\/\/eepurl.com\/BHYJ9\">Subscribe to my infrequent Cordova newsletter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hooks are scripts that you can run before and after each stage of the Cordova CLI lifecycle. They live in a projecthome\/.cordova\/hooks\/before_xxx (or after_xxx) where xxx is the project lifecycle [&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,7,60],"tags":[],"class_list":["post-1197","post","type-post","status-publish","format-standard","hentry","category-cordova-cli","category-mobile-technology","category-phonegap"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1197","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=1197"}],"version-history":[{"count":12,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1197\/revisions"}],"predecessor-version":[{"id":1285,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1197\/revisions\/1285"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}