Skip to content

Commit

Permalink
Added grunt-cli to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tinganho committed Feb 5, 2013
1 parent 4be7c45 commit 60e9a4e
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -3,3 +3,5 @@ node_js:
- 0.8
before_install:
- npm install
- npm install grunt-cli -g

63 changes: 63 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,63 @@
module.exports = function(grunt) {

'use strict';

// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*.js']
},
watch: {
files: '<config:lint.files>',
tasks: 'default'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
loopfunc: true,
forin: false,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true,
supernew: true
},
globals: {
gt: true
},
files: ['grunt.js', 'tasks/**/*.js']
},

translate: {
options: {
configDir: './test/translations',
requireJS: true,
defaultLanguage: 'en' // grunt-translate use it to update translation.
},
compile: {
output: './test/translations/output'
},
update: {
src: ['./test/example/**/*.js']
}
}
});
// Load local tasks.
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');


// Default task.
grunt.registerTask('default', 'lint translate:update translate:compile test');



};
147 changes: 147 additions & 0 deletions tasks/helpers/config.js
@@ -0,0 +1,147 @@
var grunt = require('grunt'),
engine = require('./engine'),
path = require('path'),
OPERATORS = require('./operators');

var config = {};

/**
Get all locales
return Array of all locales
*/
config.getAllLocales = function(options){
return Object.keys(grunt.file.readJSON(options.configDir + '/locales.json'));
};

/**
Check for error duplicate
@param Object res result that has already the key
@param String key
@param Array vars Translation vars
@throws Duplicate Translation Keys Error
*/
config.hasErrorDuplicate = function(res, key, vars){

// Check for error duplicate
var errorDuplicate = false;
if(vars.length !== res[key].vars.length) {
errorDuplicate = true;
} else {
for(var i in vars) {
if(res[key].vars[i] !== vars[i]) {
errorDuplicate = true;
break;
}
}
}
if(errorDuplicate) {
throw {
name: 'Duplicate Translation Keys',
message: 'You have used gt(\'' + key + '\') and input two different vars: \n' + 'Variables: ' + res[key].vars.join(',') + '\n' + 'Is not equal: ' + vars.join(',')
};
}
};

/**
Get all locales
@return Object of all locales
*/
config.getAllTranslations = function(options){
var files = grunt.file.expand({filter: 'isFile'}, options.configDir + '/locales/*.json');
var locales = {};
files.forEach(function(locale){
locales[path.basename(locale, '.json')] = grunt.file.readJSON(locale);
});
return locales;
};

/**
Returns translation key from a translation function string
@param String fn string of the function
@return String Translation key
*/
config.getTranslationKey = function(fn){
return (fn.match(/['|"][\w|\-|\s|\&|<|>|\/]+['|"]/))[0].replace(/'/g, '');
};

/**
Returns all the vars in a translation function
@param String fn Function string
@return Array of all vars
*/
config.getVars = function(fn){
var json = fn.match(/\{[\w|\s|:|"|'|\-|\{|\}|\,|\/]*\}/);
if(json === null) {
return [];
}
json = json[0].replace(/\s*\w+\s*\:/g, function(m){
var key = m.match(/\w+/);
return '"' + key + '":';
}).replace(/'/g, function(){
return '"';
});
var vars = JSON.parse(json);
return Object.keys(vars);
};

/**
Checks if an operand has the right syntax
@param String text
@return Boolean
*/
config.isTranslationText = function(text, key) {
if(typeof text === 'undefined') {
throw {
name: 'Undefined translation',
message: 'You have an undefined translation in:\n' + key
};
} else if(text.substr(0,1) !== '"' || text.substr(-1) !== '"') {
throw {
name: 'Translation Text Wrong Syntax',
message: 'You have missed quotation in:\n' + text
};
}
};

/**
Checks if operands and operators has the correct syntax
@param String operand1
@param String operator
@param String operand2
@return Boolean true
@throw Syntax Wrong in Translation JSON
*/
config.isConditions = function(operand1, operator, operand2) {

// Check operands
if(!this.isOperand(operand1) || !this.isOperand(operand2)) {
throw {
name: 'Syntax Wrong in Translation JSON',
message: 'One of the operands have wrong syntax: ' + operand1 + ' or ' + operand2
};
}

// Validate operator
if(OPERATORS.indexOf(operator) === -1) {
throw {
name: 'Syntax Wrong in Translation JSON',
message: '"' + operator + '" should be one of ' + OPERATORS.join(',')
};
}
return true;
};

/**
Checks if an operand has the right syntax
@param String operand
@return Boolean
*/
config.isOperand = function(operand) {
if(/^"?\$?\w+"?$/.test(operand)) {
return true;
}
return false;
};

module.exports = config;

0 comments on commit 60e9a4e

Please sign in to comment.