1

I have a Gruntfile which I intend to use on both OS X and Linux (Ubuntu, to be precise). I have some logic wrapped up inside a grunt-shell task:

shell: {
  somejob_linux: {
    command: [
      'firstlinuxcommand',
      'secondlinuxcommand'
    ]
  },
  somejob_osx: {
    command: [
      'firstosxcommand',
      'secondosxcommand'
    ]
  }
}

I'd like to have the first target (somejob_linux) executed if the Gruntfile is being run on Linux, and somejob_osx for OS X.

Is there an elegant way for me to achieve this? Or is there another alternative for having different commands or Grunt task/targets run for each platform? I'd prefer to keep everything within the Gruntfile, rather than calling out to external scripts simply for this purpose.

Andrew Ferrier
  • 16,664
  • 13
  • 47
  • 76

1 Answers1

1

I don't know of a way to do this in only config, but you could create a custom task to check the environment and then add the appropriate task to the queue:

grunt.registerTask('detectthenrun', 'Detect the environment, then run a task', function() {
    if (/linux/.test(process.platform)) {
        grunt.task.run( 'somejob_linux' );
    } else if (/darwin/.test(process.platform)) {
        grunt.task.run( 'somejob_osx' );
    }
});
Community
  • 1
  • 1
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
  • Thanks. Worth pointing out that it looks like process.platform actually returns `darwin` on OS X. I've corrected your answer. – Andrew Ferrier Dec 31 '14 at 13:37
  • 1
    The edit was rejected as it "looks" like you were editing to muck with the original intent. :) I'll correct this as you suggest... please accept if it solves your problem! – Jordan Kasper Dec 31 '14 at 14:34