grunt-wording
Advanced tools
Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "grunt-wording", | ||
"description": "The best Grunt plugin ever.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"homepage": "https://github.com/P1X3L/grunt-wording", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -18,3 +18,5 @@ /* | ||
shared, | ||
fullPrefix; | ||
fullPrefix, | ||
originalPaths = [], | ||
jsonPaths; | ||
@@ -39,10 +41,21 @@ function isEmptyFile(filePath) { | ||
/* | ||
* returns a table from a path. For example : | ||
* "app/template/file.ejs" returns ['app', 'template', 'file'] | ||
*/ | ||
function noExtensionPath(filePath) { | ||
return filePath.replace(path.extname(filePath), '').split(path.sep); | ||
} | ||
function cutPath(filePath) { | ||
return noExtensionPath(filePath).slice(options.rootPapayawhip); | ||
} | ||
function createWording(filePath, fileContent) { | ||
var keys = getFileKeys(fileContent); | ||
var removeExtension = filePath.replace(path.extname(filePath), ''); | ||
var splitPath = removeExtension.split(path.sep).slice(options.rootPapayawhip); | ||
var keys = getFileKeys(fileContent), | ||
usablePath = cutPath(filePath); | ||
// Build nested object using the filepath until filename | ||
var builder = data; | ||
splitPath.forEach(function(dir) { | ||
usablePath.forEach(function(dir) { | ||
if (!builder[dir]) { | ||
@@ -97,5 +110,26 @@ builder[dir] = {}; | ||
var compiled = _.template(fileContent, templateData); | ||
var dest = path.join(this.data.dest, filePath); | ||
var compiled = _.template(fileContent, templateData), | ||
dest = path.join(this.data.dest, filePath), | ||
noKeyFile = !hasWording(fileContent) && filePath; | ||
originalPaths.push(cutPath(filePath).join('/')); | ||
if (noKeyFile) { | ||
var emptyKeyFilePath = cutPath(noKeyFile); | ||
} | ||
if (emptyKeyFilePath) { | ||
var unusedKey = data; | ||
for (var i = 0; i < emptyKeyFilePath.length; i++) { | ||
unusedKey = unusedKey[emptyKeyFilePath[i]]; | ||
if (!unusedKey) { break; } | ||
} | ||
if (unusedKey) { | ||
grunt.log.errorlns( | ||
'Unused key(s) ' + | ||
grunt.log.wordlist(Object.keys(unusedKey), { color: 'red' }) + | ||
' for ' + filePath | ||
); | ||
}; | ||
} | ||
grunt.verbose.write('Creating template ' + dest + '...'); | ||
@@ -112,2 +146,16 @@ grunt.file.write(dest, compiled); | ||
grunt.registerMultiTask('wording', 'Create your wording translations file', function() { | ||
function getPathsFromData(obj, previousPath, accumulator) { | ||
accumulator = accumulator || {}; | ||
var begining = previousPath.length === 0; | ||
for (var key in obj) { | ||
if (typeof obj[key] === 'string') { | ||
accumulator[previousPath] = true; | ||
continue; | ||
} | ||
getPathsFromData(obj[key], previousPath + (begining ? '' : '/') + key, accumulator); | ||
} | ||
return accumulator; | ||
} | ||
options = this.options({ | ||
@@ -126,2 +174,7 @@ delimiters: 'config', | ||
data = JSON.parse(grunt.file.read(options.wording)); | ||
jsonPaths = getPathsFromData(data, ''); | ||
delete jsonPaths[options.sharedPrefix]; | ||
delete jsonPaths.webapp; | ||
jsonPaths = Object.keys(jsonPaths); | ||
} | ||
@@ -140,2 +193,18 @@ | ||
/* | ||
* Warn if an object becomes useless because its | ||
* original path does not exist (deleted or renamed file) | ||
*/ | ||
var inexistantPaths = _.difference(jsonPaths, originalPaths); | ||
for (var i = 0; i < inexistantPaths.length; i++) { | ||
var unusedObject = path.basename(inexistantPaths[i]); | ||
inexistantPaths[i] = path.dirname(inexistantPaths[i]).replace(/\//g, '.'); | ||
grunt.log.errorlns( | ||
'Unused object ' + | ||
grunt.log.wordlist([unusedObject], {color: 'cyan'}) + | ||
' in wording.json at ' + | ||
grunt.log.wordlist([inexistantPaths[i]], {color: 'red'}) | ||
); | ||
} | ||
// Create and fill wording.json | ||
@@ -142,0 +211,0 @@ grunt.log.write('Writing ' + options.wording + '...'); |
88053
556