4.0.0 • Published 2 years ago

grunt-shell v4.0.0

Weekly downloads
92,838
License
MIT
Repository
github
Last release
2 years ago

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')
js-workflow-jsongeneratornarrowspark-rising@infinitebrahmanuniverse/nolb-grunt-s@everything-registry/sub-chunk-1798grasshopper-admingrunt-auto-versiongrunt-auto-versioningkraatusljve.iokhoomlymmagix-app-deploymalorygrunt-phpmd-runnergrunt-angular-toolboxgrunt-bundle-wrappergrunt-compass-wrappergrunt-jquery-toolboxgrunt-gh-deploygrunt-git-flowgruntfile-convention-webappgrunt-ncftp-pushgrunt-topologygrunt-release-hoodiegrunt-set-angulargrunt-set-angular-libgrunt-set-ionicgrunt-set-jslibgrunt-set-rapptorgrunt-set-staticgrunt-web-rentenna3grunt-build-gh-pagesgrunt-behat-wrappergrunt-drupal-buildgrunt-emaillocalizationgrunt-drupal-tasksgrunt-friendlyweb-drupalgrunt-friendlyweb-drupal-projectgrunt-localgov-drupalgrunt-localgov-drupal-projectgrunt-fcoo-grunt-plugingrunt-fennecgrunt-fh-buildgrunt-phpunit-runnergrunt-qunit-puppeteerhelperpresshoodie-integration-testjhoguet-sidekickjs-project-commonsmixpanel_export_peoplemonacoremusichub-commonmusichub-connectionmusichub-connectormusichub-playlistmusichub-viewsnode-oyster@raja_rakoto/minificatorpixbi-buildpeons-angulartestfauxtontest-angualrjs-componentsunrisesusi-forgesvg-assetsthehelp-projectnucleo-clintafonixcorereact-baseplaterecroomrms-meteor-buildtowelieudosmileycoresindresorhus.jssgk-jarvissails-rx-react-jsrutha-grunt-mongo-migrationsspark-to-jsupload-foldertwogridtourismunicornsecommerce1000-packages@fishawack/config-grunt@masterofbob777/grunt-config@mitchallen/microservice-core@sparkpost/gruntfilebreathe-easybumble-buildcharlie-workcharettejscaipicaipi-cliastrodatechronicles_of_gruntcpan-distributions-listbedrock-testbbdfw_cucumber_new
4.0.0

2 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.2

9 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.7.0

10 years ago

0.6.4

10 years ago

0.6.3

10 years ago

0.6.2

10 years ago

0.6.1

11 years ago

0.6.0

11 years ago

0.5.0

11 years ago

0.4.0

11 years ago

0.3.1

11 years ago

0.3.0

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago