npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-shell

v4.0.0

Published

Run shell commands

Downloads

324,102

Readme

grunt-shell

Run shell commands

A good way to interact with other CLI tools. For example, get the current Git branch with git branch.

Install

npm install --save-dev grunt-shell

Usage

require('load-grunt-tasks')(grunt);

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(error, stdout, stderr, callback) {
	if (error) {
		callback(error);
		return;
	}

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

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. Does not apply if you specify a callback.

stdinRawMode

Type: boolean
Default: false

Set stdin to act as a raw device.

callback(error, stdout, stderr, callback)

Type: Function

Lets you override the default callback with your own.

Make sure to call the callback method when you're done. Supply an error as the first argument to callback 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')