Sato
Sato

Reputation: 8622

How to run grunt task based on environment variable?

For example, I have something like this:

  if(ENV === 'production') {
    grunt.registerTask('default', ['mkdir', 'copy', 'min']); // production
  } else {
    grunt.registerTask('default', ['mkdir', 'copy']); // dev
  }

if I do ENV=development grunt or grunt, I want dev task to be executed, and if I do ENV=production grunt, i want production task to be executed.

I cannot register two tasks: default-dev, default-prod and run grunt default-dev or grunt default-prod.

I have to use ENV variable to specify which task to run.

Upvotes: 0

Views: 851

Answers (1)

Iceman
Iceman

Reputation: 6145

Replace you ENV with process.env.NODE_ENV.

Refer Node.js docs

if (process.env.NODE_ENV === 'production') {
    grunt.registerTask('default', ['mkdir', 'copy', 'min']); // production
} else {
    grunt.registerTask('default', ['mkdir', 'copy']); // dev
}

Upvotes: 2

Related Questions