Skip to content

kaizenjegan/grunt-shell

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grunt-shell Build Status

Run shell commands

A good way to interact with other CLI tools. E.g. compiling Compass compass compile or get the current git branch git branch.

Use Stack Overflow for support questions.


🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos.


Install

$ npm install --save-dev grunt-shell

Usage

require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks

grunt.initConfig({
	shell: {
		options: {
			stderr: false
		},
		target: {
			command: 'ls'
		},
		another: 'ls ./src' // shorthand
	}
});

grunt.registerTask('default', ['shell']);

Examples

Run command

Create a folder named test.

grunt.initConfig({
	shell: {
		makeDir: {
			command: 'mkdir test'
		}
	}
});

The command property supports templates:

grunt.initConfig({
	testDir: 'test',
	shell: {
		makeDir: {
			command: 'mkdir <%= testDir %>'
		}
	}
});

You can also supply a function that returns the command:

grunt.initConfig({
	shell: {
		hello: {
			command: () => 'echo hello'
		}
	}
});

Which can also take arguments:

module.exports = grunt => {
	grunt.loadNpmTasks('grunt-shell');
	grunt.initConfig({
		shell: {
			greet: {
				command: greeting => 'echo ' + greeting
			}
		}
	});
	grunt.registerTask('default', ['shell:greet:hello']);
}

Run command and display the output

Output a directory listing in your Terminal.

grunt.initConfig({
	shell: {
		dirListing: {
			command: 'ls'
		}
	}
});

Custom callback

Do whatever you want with the output.

function log(err, stdout, stderr, cb) {
	if (err) {
		cb(err);
		return;
	}

	console.log(stdout);
	cb();
}

grunt.initConfig({
	shell: {
		dirListing: {
			command: 'ls',
			options: {
				callback: log
			}
		}
	}
});

Option passed to the .exec() method

Run a command in another directory. In this example we run it in a subfolder using the cwd (current working directory) option.

grunt.initConfig({
	shell: {
		subfolderLs: {
			command: 'ls',
			options: {
				stderr: false,
				execOptions: {
					cwd: 'tasks'
				}
			}
		}
	}
});

Multiple commands

Run multiple commands by placing them in an array which is joined using && or ;. && means run this only if the previous command succeeded. You can also use & to have the commands run concurrently (by executing all commands except the last one in a subshell).

grunt.initConfig({
	shell: {
		multiple: {
			command: [
				'mkdir test',
				'cd test',
				'ls'
			].join('&&')
		}
	}
});

Config

command

Required
Type: string Function

Command to run or a function which returns the command. Supports underscore templates.

Command can be omitted by directly setting the target with the command.

cwd

Type: string

Shortcut. Same as options.execOptions.cwd (see below).

Options

stdout

Type: boolean
Default: true

Show stdout in the terminal.

stderr

Type: boolean
Default: true

Show stderr in the terminal.

stdin

Type: boolean
Default: true

Forward the terminal's stdin to the command.

failOnError

Type: boolean
Default: true

Fail task if it encounters an error. Doesn't apply if you specify a callback.

stdinRawMode

Type: boolean
Default: false

Set stdin to act as a raw device.

callback(err, stdout, stderr, cb)

Type: Function

Lets you override the default callback with your own.

Make sure to call the cb method when you're done. Supply an error as the first argument to cb to print a warning and cause the task to fail.

preferLocal

Type: boolean
Default: true

Execute local binaries by name like $ npm run-script.

execOptions

Type: Object

Specify some options to be passed to the .exec() method:

  • cwd string Current working directory of the child process
  • env Object Environment key-value pairs
  • setsid boolean
  • encoding string (Default: 'utf8')
  • timeout number (Default: 0)
  • maxBuffer number (Default: 1000 * 1000 * 10 → 10 MB)
  • killSignal string (Default: 'SIGTERM')

License

MIT © Sindre Sorhus

About

Run shell commands

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%