Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created November 14, 2014 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebuildy/7920cb35e246ab80f39e to your computer and use it in GitHub Desktop.
Save ebuildy/7920cb35e246ab80f39e to your computer and use it in GitHub Desktop.
This small piece of NodeJS code allows you to split in files the Grunt task file. I wrote this because mine was too big, much better to have small files.
---------------------------------------------------------------
Splitr.js
---------------------------------------------------------------
var json = {
watch: {
js: {
files: ['<%= yeoman.app %>/scripts/{,*/}*.js', '<%= yeoman.common %>/js/{,*/}*.js'],
tasks: ['concat']
},
compass: {
....
},
.....
}, .......
};
var fs = require('fs');
for (var i in json)
{
var buffer = JSON.stringify(json[i], null, 4);
buffer = buffer.replace(/\"([^(\")"]+)\":/g,"$1:");
buffer = 'module.exports = ' + buffer + ';';
fs.writeFileSync('./grunt/' + i + '.js', buffer);
}
---------------------------------------------------------------
Gruntfile.js (need to import blod in your package.json file)
---------------------------------------------------------------
'use strict';
module.exports = function (grunt)
{
require('load-grunt-tasks')(grunt);
var gruntConfig = {
pkg: grunt.file.readJSON('package.json')
};
grunt.util._.extend(gruntConfig, loadConfig('./grunt/'));
grunt.initConfig(gruntConfig);
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
...
});
};
function loadConfig(path) {
var glob = require('glob');
var object = {};
var key;
glob.sync('*', {cwd: path}).forEach(function(option) {
key = option.replace(/\.js$/,'');
object[key] = require(path + option);
});
return object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment