API to modify the content of a Gruntfile.js file

Gruntfile Editor

An API to modify a Gruntfile.js content

npm install --save gruntfile-editor
var GruntfileEditor = require('gruntfile-editor');
var editor = new GruntfileEditor();
editor.insertConfig('compass', '{ foo: "bar" }');
fs.writeFileSync('Gruntfile.js', editor.toString());

Create a new editor instance. You can pass the content of the Gruntfile to edit to the constructor. If no content is provided, a default file structure is used.

Insert a configuration block inside the grunt.initConfig() call.

Register a task inside a named task group

editor.registerTask('build', 'compass');
// output: grunt.registerTask('build', ['compass']); 
editor.registerTask('build', ['compass', 'uglify']);
// output: grunt.registerTask('build', ['compass', 'uglify']); 

You can specify an optional description.

editor.registerTask('build', 'A task description', ['compass', 'uglify']);
// output: grunt.registerTask('build', 'A task description', ['compass', 'uglify']); 

Insert a variable to the top of the Gruntfile.

editor.insertVariable('root', '"project/foo"');
// output: var root = 'project/foo'; 

Insert a piece of arbritary JavaScript code to the top of the Gruntfile.

// output: require('load-grunt-tasks')(grunt); 

Load a Grunt plugin.

// output: grunt.loadNpmTasks("grunt-contrib-uglify"); 
editor.loadNpmTasks(['grunt-contrib-uglify', 'grunt-contrib-concat']);
// output: 
// grunt.loadNpmTasks("grunt-contrib-concat"); 
// grunt.loadNpmTasks("grunt-contrib-uglify"); 

Returns the modified Gruntfile as a string.

Copyright (c) 2012 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.