public
Last active

A simple sample gruntfile. If you are trying this, make sure to run `npm install`.

  • Download Gist
Gruntfile.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
function commentCommand(cmd) {
cmd = cmd || 'cmd';
var result = require('execSync')
.exec('grep -nr "// '+cmd+':" ./')
.stdout
.split('\n')[0];
if (result !== '') {
var reg = new RegExp('// '+cmd+':(.+)', 'i');
result = result.match(reg)[1];
}
return result;
}
 
module.exports = function (grunt) {
 
// config
grunt.initConfig({
 
exec: {
run: {
cmd: commentCommand
}
},
 
watch: {
scripts: {
files: 'app.js',
tasks: ['jshint', 'exec']
}
},
 
jshint: {
files: ['**/*.js', '!**/node_modules/**']
}
});
 
// load
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-exec');
 
// register
grunt.registerTask('default', ['watch']);
};
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var args = process.argv.slice(2);
 
switch (args[0]) {
// cmd: node app a
case 'a':
console.log('Hello!');
break;
 
// -cmd: node app b
case 'b':
console.log('World!');
break;
 
// -cmd: node app
default:
console.log('No parames matched :(');
}
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11
{
"name": "grunt-demo",
"version": "0.1.0",
"devDependencies": {
"execSync": "~0.0.4",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.4.3",
"grunt-contrib-watch": "~0.4.0",
"grunt-exec": "~0.4.0"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.