Executing Shell commands with grunt.
Executing Shell commands synchronously with grunt.
This plugin requires Grunt
If 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:
npm install grunt-wxi-shell --save-dev
*This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade.
In your project's Gruntfile, add a section named
wxi_shell to the data object passed into
# this is coffeescriptwxi_shell:one:options:cwd: 'components/bootstrap'returnOutput: truechained: trueexitSuccess: 02commands:"make""make bootstrap"
wxi_shell:one:options:cwd: 'components/bootstrap'returnOutput: truechained: trueexitSuccess: 0 2commands: "make" "make bootstrap";
cwd, is where a command should be run. Default is the project root (where Gruntfile is present). This works by putting
cd foo/bar && before the command to be run.
You can check out the project at aponxi/npm-execxi.
chained, which is
true by default, is an option that can stop running commands after one of them failed. To see if it failed, it checks the exit code of the command that is run.
returnOutput is also
true by default. This is what returns the output in an array, parsing them line by line. I supply with this option because sometimes you might run a command that only returns server's IP address or sometimes you can run a command that just outputs very long lines and a very long text. I added the ability to opt out so that when unnecessary you might set it to false.
exitSuccess is an array that contains a collection of error codes that you deem to be a successful exit. All programs use different exit codes, sometimes they give a warning and exit with
2. In situations like those, I find myself wanting to allow exiting with
2 for that group of commands to be treated as if command was successfully run. This allows the chained commands to continue.
By default only
0 exit codes are known to be successful runs. If you supply with
exitSuccess option you will be overriding that. So, by your definition; a program that fails to run can still be a success.
true by default. This gives the console output its color. Sometimes I find that I need to redirect output into a log file, and don't want the ANSI escape characters such as
\033[0;32m to appear in the log. This will give you a colorless output in console messages.
true by default. It is used for outputting the command's output as well as giving information on the status (such as "Running Command [1/3]"). There may be times when you just want to quietly utilize the shell command's output. Setting this to
false will save you from headaches. I usually turn it off when I'm conducting tests, because if I don't, it's a real mess.
If you specify cwd then the commands are run as
cd "your/directory" && ls if you dont' then they are run in the project root.
"use strict"=# Project configuration.gruntinitConfig# Configuration to be run (and then tested).wxi_shell:make:options:cwd: 'components/bootstrap'returnOutput: onchained: onexitSuccess: 02commands:"make""make bootstrap"clean:options:cwd: 'components/bootstrap'returnOutput: onchained: onexitSuccess: 02commands:"make clean"# Actually load this plugin's task(s).gruntloadTasks "tasks"# plugin's task(s), then test the result.gruntregisterTask "clean""wxi_shell:clean"gruntregisterTask "test""clean""wxi_shell:make"# By defaultgruntregisterTask "default""test"
"use strict";gruntinitConfigwxi_shell:make:options:cwd: 'components/bootstrap'returnOutput: truechained: trueexitSuccess: 0 2commands: "make" "make bootstrap"clean:options:cwd: 'components/bootstrap'returnOutput: truechained: trueexitSuccess: 0 2commands: "make clean";gruntloadTasks"tasks";gruntregisterTask"clean" "wxi_shell:clean";gruntregisterTask"test" "clean" "wxi_shell:make";return gruntregisterTask"default" "test";;
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].