Integrating IBM MobileFirst Command Line Tools with a Grunt Workflow

The IBM MobileFirst Platform Command Line Interface allows you to integrate MobileFirst technologies with your existing workflows and tools. In this video tutorial, I show you step-by-step how to integrate the MobileFirst CLI into a Grunt workflow that verifies, builds, and publishes MobileFirst artifacts automatically as you make code changes.


Below you will find the completed Gruntfile that was used in this tutorial. Feel free to copy and paste it as a starting point for your own Grunt build.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module.exports = function (grunt) {
    // Project configuration.
    grunt.initConfig({
        jshint: {
            js: {
                // define the files to lint
                src: ['apps/**/*.js', 'adapters/**/*.js'],
                // configure JSHint (documented at http://www.jshint.com/docs/)
                options: {
                    curly: true,
                    eqeqeq: false,
                    '-W041': true,
                    browser: true,
                },
            },
            html: {
                options: {
                    extract: 'always',
                    browser: true,
                    undef: false,
                },
                src: ['apps/**/*.html'],
            },
            options: {
                // comment out to prevent further tasks from running after linting fails
                force: true,
            },
        },
        watch: {
            apps: {
                files: ['apps/*', 'apps/**'],
                tasks: ['jshint', 'mobilefirst:build:apps', 'mobilefirst:deploy:apps'],
            },
            adapters: {
                files: ['adapters/*', 'adapters/**'],
                tasks: ['jshint', 'mobilefirst:build:adapters', 'mobilefirst:deploy:adapters'],
            },
        },
        mobilefirst: {
            build: {},
            deploy: {},
        },
    });
    var execCommand = function (name, cmd, opts) {
        var exec = require('child_process').exec;
        var done = this.async();
        exec(cmd, opts || {}, function (err, stdout, stderr) {
            if (err) {
                grunt.log.error(stderr);
                done();
                grunt.fail.fatal(name + ' failed.');
            }
            console.log(stdout);
            grunt.log.ok(name + ' succeeded.');
            done();
        });
    };
    grunt.registerMultiTask('mobilefirst', function(artifactFolder) {
        execCommand.apply(this, ['MobileFirst Task', 'mfp ' + this.target, {
            cwd: artifactFolder
        }]);
    });
    // Load additional grunt tasks
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    // Default task(s).
    grunt.registerTask('default', ['jshint', 'mobilefirst:build:apps', 'mobilefirst:build:adapters']);
};
Last modified on May 01, 2016
Share this post: