Skip to content

Commit

Permalink
implemented modules, inline callback and before/after options
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartvds committed Aug 8, 2013
1 parent d3b09c5 commit d70ac56
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 186 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
node_modules
/node_modules
npm-debug.log
tmp
/tmp
/test/tmp

/*.tgz
/.idea
/.vagrant
32 changes: 19 additions & 13 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
}
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"smarttabs": true,
"node": true,
"browser": true,
"laxcomma": true,
"globals": {

}
}
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.npmignore
.gitignore
.gitmodules

/.vagrant
/cookbooks
/Vagrantfile

/.idea
/node_modules
/*.tgz
88 changes: 81 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

'use strict';

/*jshint -W098 */
module.exports = function (grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
Expand All @@ -25,15 +25,88 @@ module.exports = function (grunt) {

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['test/tmp']
tests: ['tmp', 'test/tmp']
},

// Configuration to be run (and then tested).
execute: {
//error: {src: ['test/fixtures/error*.js']},
sync: {src: ['test/fixtures/**/*.sync.js']},
async: {src: ['test/fixtures/**/*.async.js']},
none: {src: ['test/fixtures/nonexisting.js']}
node_sync: {
src: ['test/fixtures/**/node.sync.*js']
},
node_async: {
src: ['test/fixtures/node.async.js']
},
zero: {
src: ['test/fixtures/nonexisting.js']
},
module_sync: {
options: {
module: true
},
src: ['test/fixtures/module.sync.js']
},
module_async: {
options: {
module: true
},
src: ['test/fixtures/module.async.js']
},
call_sync: {
call: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('call.sync.gruntfile.js');

grunt.file.write(ctx.dest, ctx.data);
}
},
call_async: {
call: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('call.async.gruntfile.js');

// callback get, makes grunt-execute run async
var done = async();

var fs = require('fs');
setTimeout(function () {
fs.writeFile(ctx.dest, ctx.data, function (err) {

// call callback, passing error will fail the task
done(err);
});
}, 500);
}
},
beforeAfter: {
options: {
before: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('before.options.sync.gruntfile.js');

grunt.file.write(ctx.dest, ctx.data);
},
after: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('after.options.sync.gruntfile.js');

grunt.file.write(ctx.dest, ctx.data);
}
},
before: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('before.sync.gruntfile.js');

grunt.file.write(ctx.dest, ctx.data);
},
after: function (grunt, options, async) {
var help = require('./test/helper.js');
var ctx = help.getContext('after.sync.gruntfile.js');

grunt.file.write(ctx.dest, ctx.data);
},
src: ['test/fixtures/node.async.js']
}
},

// Unit tests.
Expand All @@ -49,8 +122,9 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

grunt.registerTask('test', ['clean', 'execute', 'nodeunit']);
grunt.registerTask('test', ['jshint', 'clean', 'execute', 'nodeunit']);

grunt.registerTask('default', ['jshint', 'test']);
grunt.registerTask('default', ['test']);
grunt.registerTask('dev', ['jshint']);

};
110 changes: 86 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# grunt-execute

> Grunt task to execute code in node
> Grunt plugin to execute code in node
## Getting Started
This plugin requires Grunt `~0.4.1`
Expand All @@ -19,46 +19,108 @@ grunt.loadNpmTasks('grunt-execute');

## The "execute" task

The main use-case is easier testing of apps or loose bits javascript, or use code as a poor-mans grunt-task.
Execute javascript files and snippets to test application files, run loose bits development javascript or use basic files as a poor-mans grunt-tasks.

* Run selected files in a node.js child process.
* Run inline functions with grunt/options helpers and async support.
* Run functions before/after looping the selected files.
* Require() selected files as custom callback module.

The callback module file and inline functions all share the same signature with access to grunt, options and optional async callback.

### Overview

In your project's Gruntfile, add a section named `execute` to the data object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
execute: {
target: {
src: ['script.js']
}
}
execute: {
target: {
src: ['script.js']
}
}
})
```

### Options

Also see the Gruntfile.js for real usage examples.

```js
grunt.initConfig({
execute: {
options: {
cwd: '.' //overide code cwd (defaults to '.' for project main, use null for scripts's own directory)
},
many_targets: {
src: ['apps/**/*.js', 'lib/**/index.js'] //supports globs
}
}
})
execute: {
simple_target: {
// execute javascript files in a node child_process
src: ['script.js']
},
cwd_target: {
options: {
// overide code cwd (defaults to '.' for project main)
cwd: '.'
},
src: ['script.js']
},
glob_target: {
// supports grunt glob and filter features
src: ['apps/**/*.js', 'lib/**/index.js']
},
module_target: {
options: {
// use require() instead of a child_process
// the scripts must be a module exporting a function
// use a signature like the inline call-option (see below)

module: true
},
src: ['script.js']
},
callback_sync: {
// simple inline function call
call: function(grunt, options){
grunt.log.writeln('Hello!');
}
},
callback_async: {
// function call also supports async callback
call: function(grunt, options, async){
// get the callback
var done = async();

setTimeout(function(){
grunt.log.writeln('Done!')
done(err);
}, 1000);
}
}
before_after: {
options: {
// like call but executed before/after looping the files
before: function(grunt, options){
console.log('Hello!');
},
after: function(grunt, options){
console.log('Bye!');
}
},
// can also be used outside the options
before: function(grunt, options){
console.log('Hello!');
},
after: function(grunt, options){
console.log('Bye!');
}
src: ['script.js'],
},
}
});
```

## Versions

* 0.1.4 - ditched stdio option, show errors inline (even in webstorm)
* 0.1.3 - basic version, colors disabled

## Todo

* Add options-passing support to fake CLI/env input
## Versions

* Add pre/post require's
* 0.1.5 - Added callback module & inline function support
* 0.1.4 - Ditched stdio option, show errors inline (even in webstorm)
* 0.1.3 - Basic version, colors disabled

## Contributing
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](http://gruntjs.com/).
Expand Down
Loading

0 comments on commit d70ac56

Please sign in to comment.