简体   繁体   中英

How can I write a Gruntfile with different shell command behaviour for OS X vs. Linux?

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.

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' );
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM