Load process.env from environmental-style shell scripts for subsequent grunt tasks.
npm install grunt-environmentalLoad process.env from environmental-style shell scripts for subsequent grunt tasks.




~0.4.5If you haven't used Grunt before, be sure to check out
the Getting Started guide, as it
explains how to create a Gruntfile as
well as install and use Grunt plugins. Once you're familiar with that process,
you may install this plugin with this command:
``shell`
npm install grunt-environmental --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile
with this line of JavaScript:
`js`
grunt.loadNpmTasks('grunt-environmental');
This task is intended to make environment variables configured using
the conventions of the
environmental package
available to subsequent tasks run within the same Grunt
instance.
The environmental package uses shell scripts to establish the desiredexec
configuration, but invoking them just via has no effect on theprocess.env
environment available in the node interpreter. This task evaluates
the environment, executes environmental's script, re-checks the
shell environment afterward, and then the changes the script made to
the environment it ran within, are made to the environment in node's.
When the environmental Grunt task is invoked with a target,environmental
it uses that target
as the name of the shell script to execute. Forenvironmental:staging
example, running the Grunt task loads./envs/staging.sh
environment variables by executing the script .envs
Specifying a target name that doesn't have a matching shell
script in the directory will produce an error. If thedevelopment
task is invoked without a target name, the default is (and an error will occur if the script./envs/development.sh can't be found).
There are two keys that the environmental task looks for withinoptions
its hash in the overall Grunt configuration,
* envsPath By default, the environmental task looks for./envs
scripts to execute within the directory . If this isn'tenvsPath
the correct directory in your system, use to specify a
string that gives the path of the directory where shell scripts
should be found.
* inject The environmental task can also insert items into node'senvironmental
environment from a hash literal in the Grunt configuration. The hash
of environment variables that should be injected is determined by the
third part of the Grunt task name that is used to invoke it--that is
the options argument name that follows the target name. When a third
task name component is present, the task will lookinject
for an key in its options, and for the task argument as ainject
key within the hash. It will then take the entire content
of the hash under that key and populate it into the current environment.
The keys in each injected hash are used to create the injected environment
variable names. Complete names are created by appending the keys to the
value in the NODE_APP_PREFIX environment variable. This will notNODE_APP_PREFIX
work correctly unless the shell script executed has, in fact, set as is environmental's convention.
For example, this configuration in Gruntfile.js
``
grunt.initConfig({
environmental: {
options: {
envsPath: "deploys"),
inject: {
"greek": {
INJECTED_A: "alpha",
INJECTED_B: "omega"
}
}
}
}
});
Would set deploys as the name of the directory that will be usedNODE_APP_PREFIX
as the location for environmental shell scripts. And, if ALPHABET
is equal to , then invoking the task environmental:test:greekdeploys/test.sh
will load the node environment by executing and thenALPHABET_INJECTED_A
setting the environment variables andALPHABET_INJECTED_B.
Here are two Grunt task definitions using grunt-environmental:
`js`
grunt.registerTask("test", "run automated tests", ["environmental:test", "mochacli:unit"]);
grunt.registerTask("start", "start the server in the development environment",
["environmental", "server-start"]);
With this configuration, the test task will be run with the environment./envs/test.sh
variables set by the script and the start task will./envs/development.sh
be run with the environment established by .
This package also contains a Grunt task named "printenv", equivalent
to the \*nix printenv(1)` command. It's used by the unit tests to sample
Grunt's environment before and after the primary task executes.