Skip to content

Commit

Permalink
Revamp grunt config
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Jan 21, 2017
1 parent 3138a31 commit 49cc55a
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 785 deletions.
401 changes: 99 additions & 302 deletions Gruntfile.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions build/cleanLn.js
@@ -0,0 +1,3 @@
module.exports = function(src) {
return src.replace(/\r\n/g, '\n');
};
68 changes: 68 additions & 0 deletions build/initConfig.js
@@ -0,0 +1,68 @@
module.exports = function(grunt, config) {
config.all_plugins = {};
config.all_langs = {};
config.loaded_plugins = [];
config.loaded_langs = [];
config.js_files_to_load = config.js_core_files.slice();
config.all_js_files = config.js_core_files.slice();

// list available plugins and languages
grunt.file.expand('src/plugins/**/plugin.js')
.forEach(function(f) {
var n = f.split('/')[2];
config.all_plugins[n] = f;
});

grunt.file.expand('src/i18n/*.json')
.forEach(function(f) {
var n = f.split(/[\/\.]/)[2];
config.all_langs[n] = f;
});

// fill all js files
for (var p in config.all_plugins) {
config.all_js_files.push(config.all_plugins[p]);
}

// parse 'plugins' parameter
var arg_plugins = grunt.option('plugins');
if (typeof arg_plugins === 'string') {
arg_plugins.replace(/ /g, '').split(',').forEach(function(p) {
if (config.all_plugins[p]) {
config.js_files_to_load.push(config.all_plugins[p]);
config.loaded_plugins.push(p);
}
else {
grunt.fail.warn('Plugin ' + p + ' unknown');
}
});
}
else if (arg_plugins === undefined) {
for (var p in config.all_plugins) {
config.js_files_to_load.push(config.all_plugins[p]);
config.loaded_plugins.push(p);
}
}

// default language
config.js_files_to_load.push('.temp/i18n/en.js');
config.loaded_langs.push('en');

// parse 'lang' parameter
var arg_langs = grunt.option('languages');
if (typeof arg_langs === 'string') {
arg_langs.replace(/ /g, '').split(',').forEach(function(l) {
if (config.all_langs[l]) {
if (l !== 'en') {
config.js_files_to_load.push(config.all_langs[l].replace(/^src/, '.temp').replace(/json$/, 'js'));
config.loaded_langs.push(l);
}
}
else {
grunt.fail.warn('Language ' + l + ' unknown');
}
});
}

return config;
};
30 changes: 30 additions & 0 deletions build/processLang.js
@@ -0,0 +1,30 @@
var deepmerge = require('deepmerge');

module.exports = function(grunt, loaded_plugins) {
return function(file, src, wrapper) {
var lang = file.split(/[\/\.]/)[2];
var content = JSON.parse(src);
wrapper = wrapper || ['', ''];

grunt.config.set('lang_locale', content.__locale || lang);
grunt.config.set('lang_author', content.__author);
var header = grunt.template.process('<%= langBanner %>');

loaded_plugins.forEach(function(p) {
var plugin_file = 'src/plugins/' + p + '/i18n/' + lang + '.json';

if (grunt.file.exists(plugin_file)) {
content = deepmerge(content, grunt.file.readJSON(plugin_file));
}
});

return header
+ '\n\n'
+ wrapper[0]
+ 'QueryBuilder.regional[\'' + lang + '\'] = '
+ JSON.stringify(content, null, 2)
+ ';\n\n'
+ 'QueryBuilder.defaults({ lang_code: \'' + lang + '\' });'
+ wrapper[1];
};
};
5 changes: 5 additions & 0 deletions build/removeJshint.js
@@ -0,0 +1,5 @@
module.exports = function(src) {
return src
.replace(/\/\*jshint [a-z:]+ \*\/\r?\n\r?\n?/g, '')
.replace(/\/\*jshint -[EWI]{1}[0-9]{3} \*\/\r?\n\r?\n?/g, '');
};
39 changes: 39 additions & 0 deletions build/tasks/describeErrors.js
@@ -0,0 +1,39 @@
module.exports = function(grunt, config) {
grunt.registerTask('describe_errors', 'List QueryBuilder errors.', function() {
var errors = {};
var total = 0;

for (var f in config.all_js_files) {
grunt.file.read(config.all_js_files[f]).split(/\r?\n/).forEach(function(line, i) {
var matches = /Utils\.error\((?:[^)]+, )?'(\w+)', '([^)]+)'([^)]*)\);/.exec(line);
if (matches !== null) {
(errors[matches[1]] = errors[matches[1]] || []).push({
type: matches[1],
message: matches[2],
file: config.all_js_files[f],
line: i,
args: matches[3].slice(2).split(', ')
});

total++;
}
});
}

grunt.log.write('\n');

for (var e in errors) {
grunt.log.writeln((e + 'Error')['cyan']);
errors[e].forEach(function(error) {
var message = error.message.replace(/{([0-9]+)}/g, function(m, i) {
return error.args[parseInt(i)]['yellow'];
});
grunt.log.writeln(' ' + (error.file + ':' + error.line)['red']);
grunt.log.writeln(' ' + message);
});
grunt.log.write('\n');
}

grunt.log.writeln((total + ' Errors in QueryBuilder.')['cyan']['bold']);
});
};
36 changes: 36 additions & 0 deletions build/tasks/describeTriggers.js
@@ -0,0 +1,36 @@
module.exports = function(grunt, config) {
grunt.registerTask('describe_triggers', 'List QueryBuilder triggers.', function() {
var triggers = {};
var total = 0;

for (var f in config.all_js_files) {
grunt.file.read(config.all_js_files[f]).split(/\r?\n/).forEach(function(line, i) {
var matches = /(e = )?(?:this|that)\.(trigger|change)\('(\w+)'([^)]*)\);/.exec(line);
if (matches !== null) {
triggers[matches[3]] = {
name: matches[3],
type: matches[2],
file: config.all_js_files[f],
line: i,
args: matches[4].slice(2),
prevent: !!matches[1]
};

total++;
}
});
}

grunt.log.write('\n');

for (var t in triggers) {
grunt.log.write(t['cyan'] + ' ' + triggers[t].type['magenta']);
if (triggers[t].prevent) grunt.log.write(' (*)'['yellow']);
grunt.log.write('\n');
grunt.log.writeln(' ' + (triggers[t].file + ':' + triggers[t].line)['red'] + ' ' + triggers[t].args);
grunt.log.write('\n');
}

grunt.log.writeln((total + ' Triggers in QueryBuilder.')['cyan']['bold']);
});
};
23 changes: 23 additions & 0 deletions build/tasks/listModules.js
@@ -0,0 +1,23 @@
module.exports = function(grunt, config) {
grunt.registerTask('list_modules', 'List QueryBuilder plugins and languages.', function() {
grunt.log.writeln('\nAvailable QueryBuilder plugins:\n');

for (var p in config.all_plugins) {
grunt.log.write(p['cyan']);

if (grunt.file.exists(config.all_plugins[p].replace(/js$/, 'scss'))) {
grunt.log.write(' + CSS');
}

grunt.log.write('\n');
}

grunt.log.writeln('\nAvailable QueryBuilder languages:\n');

for (var l in config.all_langs) {
if (l !== 'en') {
grunt.log.writeln(l['cyan']);
}
}
});
};

0 comments on commit 49cc55a

Please sign in to comment.