Home

Awesome

grunt-env Build Status

Specify an ENV configuration as a task, e.g.

grunt.registerTask('dev', ['env:dev', 'lint', 'server', 'watch']);
grunt.registerTask('build', ['env:build', 'lint', 'other:build:tasks']);

Getting Started

Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-env

Then add this line to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-env');

Configuration

  env : {
    options : {
 	//Shared Options Hash
    },
    dev : {
      NODE_ENV : 'development',
      DEST     : 'temp'
    },
    build : {
      NODE_ENV : 'production',
      DEST     : 'dist',
      concat   : {
        PATH     : {
          'value': 'node_modules/.bin',
          'delimiter': ':'
        }
      }
    },
    functions: {
      BY_FUNCTION: function() {
        var value = '123';
        grunt.log.writeln('setting BY_FUNCTION to ' + value);
        return value;
      }
    }
  }

Using external files

You can specify environment values in INI, JSON or YAML style and load them via the src option.

  env : {
    dev : {
      src : "dev.json"
    },
    prod: {
      src: "settings.yaml"
    }
    heroku : {
      src : ".env"
    }
  }

Using envdir

You can specify files to read environment variables from, similar to the daemontools envdir utility.

  env : {
    dev : {
      src : ["envdir/*"],
      options: {
        envdir: true
      }
    }
  }

Dynamic ENV configuration

The following directives can be specified in the options to alter the environment in more specific ways

yourtask : {
  USER : 'you',
  PATH : '/bin:/usr/bin'

  options : {
    add : {
      VERBOSE : '1' // will only be added if VERBOSE isn't already set
    },
    replace : {
      USER : 'me'
    },
    push : {
      PATH : {
        value : '~/bin',
        delimiter : ':'
      }
    },
    unshift : {
      PATH : '/sbin:'
    }
  }
}

Environment-specific configuration

In order to configure your tasks based on the environment, you need to define a task and use templates:


grunt.initConfig({
  env: {
    dev: {
      MY_CONST: 'a'
    },
    prod: {
      MY_CONST: 'b'
    }
  },
  myTask: {
    options: {
      myOpt: <%= MY_CONST %>
    }
  }
});

grunt.registerTask('loadconst', 'Load constants', function() {
    grunt.config('MY_CONST', process.env.MY_CONST);
});

grunt.registerTask('default', [
    'env:dev',
    'loadconst',
    'myTask'
]);

Important note on data types

Environment variables are strings only. If you attempt to assign complex objects, they will be converted to strings.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

License

Licensed under the Apache 2.0 license.

Author

Jarrod Overson