Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Init

  • Loading branch information...
commit 60165e8de22c8865bf57af84a94bf29b731fc30a 0 parents
@sindresorhus authored
6 .editorconfig
@@ -0,0 +1,6 @@
+root = true
+
+[*]
+indent_style = tab
+indent_size = 4
+end_of_line = LF
1  .gitignore
@@ -0,0 +1 @@
+node_modules
3  .npmignore
@@ -0,0 +1,3 @@
+test
+grunt.js
+readme.md
2  bin/grunt-shell
@@ -0,0 +1,2 @@
+#!/usr/bin/env node
+require('grunt').npmTasks('grunt-shell').cli();
57 grunt.js
@@ -0,0 +1,57 @@
+module.exports = function( grunt ) {
+ 'use strict';
+
+ function log( stdout ) {
+ console.log( 'Directory listing:\n' + stdout );
+ }
+
+ grunt.initConfig({
+ shell: {
+ subfolder_ls: {
+ command: 'ls',
+ stderr: false,
+ failOnError: false,
+ execOptions: {
+ cwd: './tasks'
+ }
+ },
+ _options: {
+ stdout: log
+ }
+ },
+ lint: {
+ files: [
+ 'grunt.js',
+ 'tasks/**/*.js'
+ ]
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ es5: true,
+ esnext: true,
+ bitwise: true,
+ curly: true,
+ eqeqeq: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ noempty: true,
+ regexp: true,
+ undef: true,
+ strict: true,
+ trailing: true,
+ smarttabs: true,
+ node: true
+ }
+ }
+ });
+
+ grunt.loadTasks('tasks');
+
+ grunt.registerTask('default', 'lint shell');
+
+};
35 package.json
@@ -0,0 +1,35 @@
+{
+ "name": "grunt-shell",
+ "version": "0.1.0",
+ "description": "Grunt task to run shell commands",
+ "keywords": [
+ "gruntplugin",
+ "grunt",
+ "shell",
+ "command",
+ "exec",
+ "cli"
+ ],
+ "homepage": "https://github.com/sindresorhus/grunt-shell",
+ "bugs": "https://github.com/sindresorhus/grunt-shell/issues",
+ "author": {
+ "name": "Sindre Sorhus",
+ "email": "sindresorhus@gmail.com",
+ "url": "http://sindresorhus.com"
+ },
+ "main": "grunt.js",
+ "bin": "bin/grunt-shell",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/sindresorhus/grunt-shell.git"
+ },
+ "dependencies": {
+ "grunt": "~0.3.9"
+ },
+ "engines": {
+ "node": ">=0.6.0"
+ },
+ "licenses": {
+ "type": "MIT"
+ }
+}
225 readme.md
@@ -0,0 +1,225 @@
+# grunt-shell
+
+[Grunt][grunt] task to run shell commands.
+
+E.g. compile Compass (`compass compile`) or get the current git branch (`git branch`).
+
+
+## Getting started
+
+Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-shell`
+
+Then add this line to your project's `grunt.js` gruntfile:
+
+```javascript
+grunt.loadNpmTasks('grunt-shell');
+```
+
+
+## Documentation
+
+
+### Example usage
+
+This grunt task is a [multi task](https://github.com/cowboy/grunt/blob/master/docs/types_of_tasks.md#multi-tasks-%E2%9A%91), which means you can specify multiple subtasks and grunt will iterate over them. The `dist` below is a subtask, you could e.g. create a `dev` subtask to handle stuff while developing. You can also add a special subtask named `_options` that can contain options for all your subtasks.
+
+
+#### Run command
+
+Create a folder named `test`.
+
+```javascript
+shell: {
+ make_directory: {
+ command: 'mkdir test'
+ }
+}
+```
+
+
+#### Run command and display output
+
+Output a directory listing to your Terminal.
+
+```javascript
+shell: {
+ directory_listing: {
+ command: 'ls',
+ stdout: true
+ }
+}
+```
+
+
+#### Run command and handle output
+
+Do whatever you want with the stdout.
+
+```javascript
+function log() {
+ console.log( this );
+}
+
+...
+
+shell: {
+ directory_listing: {
+ command: 'ls',
+ stdout: 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` option.
+
+```javascript
+shell: {
+ subfolder_ls: {
+ command: 'ls',
+ stdout: true,
+ execOptions: {
+ cwd: './tasks'
+ }
+ }
+}
+```
+
+
+#### Custom callback
+
+Define custom callback method to handle everything yourself. Check out [shell.js](https://github.com/sindresorhus/grunt-shell/blob/master/tasks/shell.js) to see how it's handled by default.
+
+```javascript
+function customHandler() {
+ console.log( this, this.data.stdout );
+}
+
+...
+
+shell: {
+ ls: {
+ command: 'ls',
+ callback: customHandler
+ }
+}
+```
+
+
+#### Multiple subtasks
+
+This task is a [multi task](https://github.com/cowboy/grunt/blob/master/docs/types_of_tasks.md#multi-tasks-%E2%9A%91), which means you can specify multiple subtasks and grunt will iterate over them.
+
+```javascript
+shell: {
+ directory_listing: {
+ command: 'ls',
+ stdout: true
+ },
+ compile_coffescript: {
+ command: 'coffee main.coffee',
+ failOnError: true
+ }
+}
+```
+
+#### Global options
+
+You can define global options in a subtask called `_options`. Your subtasks will then inherit those options with the ability to override them.
+
+
+```javascript
+shell: {
+ directory_listing: {
+ command: 'ls',
+ stdout: true
+ },
+ create_folder: {
+ command: 'mkdir test',
+ failOnError: false
+ },
+ _options: {
+ failOnError: true
+ }
+}
+```
+
+
+### Options
+
+
+#### command
+
+**Required**
+Accepts: String
+
+Your command is my wish.
+
+
+#### stdout
+
+Default: `false`
+Accepts: Boolean / Function
+
+Show stdout in the Terminal. You can supply a function to handle the output.
+
+
+#### stderr
+
+Default: `false`
+Accepts: Boolean / Function
+
+Show stderr in the Terminal. You can supply a function to handle the output.
+
+
+#### failOnError
+
+Default: `false`
+Accepts: Boolean
+
+Fail task if it encounters an error.
+
+
+#### execOptions
+
+Default: `undefined`
+Accepts: Object
+
+Specify some options to be passed to the [.exec()](http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) 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: 200\*1024)*
+- `killSignal` String *(Default: 'SIGTERM')*
+
+
+#### callback
+
+Default: `undefined`
+Accepts: Function
+
+Lets you override the default callback with your own. Everything you need is available on `this`.
+
+
+## Tests
+
+Grunt currently doesn't have a way to test tasks directly. You can test this task by running `grunt` and manually verify that it works.
+
+
+## Contribute
+
+In lieu of a formal styleguide, take care to maintain the existing coding style.
+
+
+## License
+
+MIT License
+(c) [Sindre Sorhus](http://sindresorhus.com)
+
+
+[grunt]: https://github.com/cowboy/grunt
+[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
50 tasks/shell.js
@@ -0,0 +1,50 @@
+/*
+ * grunt-shell
+ * 0.1.0 - 2012-06-04
+ * github.com/sindresorhus/grunt-shell
+ *
+ * (c) Sindre Sorhus
+ * sindresorhus.com
+ * MIT License
+ */
+module.exports = function( grunt ) {
+ 'use strict';
+
+ var _ = grunt.utils._;
+ var log = grunt.log;
+
+ grunt.registerMultiTask( 'shell', 'Run shell commands', function() {
+ var exec = require('child_process').exec;
+ var done = this.async();
+ var data = _.extend( grunt.config.get('shell')._options, this.data );
+ var dataOut = data.stdout;
+ var dataErr = data.stderr;
+
+ if ( _.isFunction( data.callback ) ) {
+ data.callback.call( this );
+ return;
+ }
+
+ exec( data.command, data.execOptions, function( err, stdout, stderr ) {
+ if ( stdout ) {
+ if ( _.isFunction( dataOut ) ) {
+ dataOut( stdout );
+ } else if ( dataOut === true ) {
+ log.write( stdout );
+ }
+ }
+
+ if ( stderr ) {
+ if ( _.isFunction( dataErr ) ) {
+ dataErr( stdout );
+ } else if ( dataErr === true ) {
+ log.error( err );
+ } else if ( data.failOnError ) {
+ grunt.fatal( err );
+ }
+ }
+
+ done();
+ });
+ });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.