3

So, I have the grunt file below. I'm wanting to add a task that will start my node app and watch for changes in a directory and restart. I have been using supervisor, node-dev (which are great) but I want to run one command and start my whole app. There has got to be a simple way to do this, but I'm just missing it. It is written in coffeescript as well (not sure if that changes things)...

module.exports = function(grunt) {
  grunt.initConfig({
    /*exec: {
        startApi: {
            command: "npm run-script start-api"
        }
    },*/
    //static server
    server: {
        port: 3333,
        base: './public',
        keepalive: true
    },

    // Coffee to JS compilation
    coffee: {
        compile: {
            files: {
                './public/js/*.js': './src/client/app/**/*.coffee'
            },
            options: {
                //basePath: 'app/scripts'
            }
        }
    },


    mochaTest: {
        all: ['test/**/*.*']
    },


    watch: {
        coffee: {
            files: './src/client/app/**/*.coffee',
            tasks: 'coffee'
        },
        mochaTest: {
            files: 'test/**/*.*',
            tasks: 'mochaTest'
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-mocha-test');
//grunt.loadNpmTasks('grunt-exec');

grunt.registerTask( 'default', 'server coffee mochaTest watch' );
};

As you can see in the comments, I tries grunt-exec, but the node command stops the execution of the other tasks.

aheuermann
  • 2,599
  • 2
  • 20
  • 17

1 Answers1

8

You can set grunt to run default task and the watch task when you start your node app:

in app.js

var cp = require('child_process');
var grunt = cp.spawn('grunt', ['--force', 'default', 'watch'])

grunt.stdout.on('data', function(data) {
    // relay output to console
    console.log("%s", data)
});

Then just run node app as normal!

Credit

flynfish
  • 5,857
  • 3
  • 24
  • 33
  • hey do you have an updated link for that reference? I tried to find it but had no luck. also, instead of creating a child process, could we not just use grunt-exec to start the app via supervisor? – Leonidas Mar 05 '13 at 04:23
  • Looks like that blog is down right now :/ I don't have any other reference to it. As for using supervisory, that might be possible, I'm not familiar with it though. – flynfish Mar 05 '13 at 18:35
  • Thanks. With regards to your example, why not run your app from Grunt instead of the other way around? Although I guess this way you can use something like nodemon which will restart your server whenever there are changes and then run grunt again. – Leonidas Mar 06 '13 at 01:38
  • 6
    It might be possible to now run the app from Grunt based on this [answer](http://stackoverflow.com/questions/15044026/running-node-app-through-grunt). Also, I found an [archive of the reference](http://web-archive-me.com/page/1254392/2013-01-29/http://cobbweb.me/blog/2012/06/07/using-grunt-dot-js-to-build-your-frontend-in-a-node-dot-js-slash-express-dot-js-app/) – flynfish Mar 06 '13 at 02:24