Home » Javascript » Why does my Cordova hook have to be run twice before it works?

Why does my Cordova hook have to be run twice before it works?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have a Cordova project I am working on that uses broswerify so I can use require() in the mobile app. Everything works good so I am attempting to move the browserifying of my js files to a Cordova hook so I don’t have to remember to run browserify before installing the app.

Now when I move the command to browserify my js files to a Cordova hook (before_run), it has to be run twice before I see the changes I made.

  1. Make changes to files.
  2. Run cordova run.... Hook runs successfully and app installs.
  3. In my code editor I see the bundle.js file has the updated code so I know the hook is successfully running the browserify command.
  4. Using Chrome debug tools I can see an Android device still has the old bundle.js code.
  5. I run cordova run... again and now I see the new code in the bundle.js in the Android device.

I’m thinking it may be a timing issue, if that is the case how do I make the Cordova hook wait until the bundle.js file is ready before continuing? If its something else can someone point me in the right direction to get this to work properly? I have tried this about 5 times and every time it does not work the first time but does work the 2nd time.

Answers:

Make the hook script run sync for example using an npm script then have a bat or bash script that runs npm

npm run build