Using Grunt to Automate MobileFirst/Hybrid Builds

When working with MobileFirst and hybrid mobile applications, any changes to your web assets requires running both a build and deploy to see your changes. The command line makes this easy by letting you combine the calls into one (mfp bd), but while developing, it may be a pain to constantly run this. So for fun, I wrote the following Grunt script.

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch:{
            scripts:{
                files:['common/**'],
                tasks:['exec:build','exec:announce'],
                options:{
                    interrupt:true
                }
            }
        },
        exec:{
            build:{
                cmd:'mfp bd'
            },
            announce: {
                cmd:'say "All tasks complete" --voice=Vicki'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-exec');
    
    grunt.registerTask('default', ['watch']);

};

Basically it just watches for changes to the common folder and then executes the mfp command to run bd. When done, it uses the Mac "say" program to speak which is totally necessary for the proper execution of this program! (Ok, it is totally not necessary and those of yo u on PC should remove that part.)

Since build/deploy can take about 20 seconds, I use the interrupt option to kill an existing bd process. Let me know if this is helpful in the comments below!

Like This?

If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can also subscribe to the email feed to get notified of new posts.

See Also