Created
October 27, 2016 14:49
-
-
Save brunnels/b3f31ac7155917d508383c77898fac4b to your computer and use it in GitHub Desktop.
Gulp tasks to update il8n json files in jhipster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var gulp = require('gulp'), | |
argv = require('yargs').argv, | |
tap = require('gulp-tap'); | |
var config = require('./config'); | |
module.exports = { | |
setProperty: setProperty, | |
deleteProperty: deleteProperty, | |
addProperty: addProperty | |
} | |
function deleteProperty() { | |
if(argv.property === undefined) { | |
throw new Error("property parameter is required"); | |
} | |
doWork(0); | |
} | |
function setProperty() { | |
if(argv.property === undefined || argv.value === undefined) { | |
throw new Error("property and value parameters are required"); | |
} | |
doWork(1); | |
} | |
function addProperty() { | |
if(argv.property === undefined || argv.value === undefined || argv.filename === undefined) { | |
throw new Error("property, value, and filename parameters are required"); | |
} | |
doWork(2); | |
} | |
function doWork(operation) { | |
// 0 == deleting, 1 == setting, 2 == adding | |
if(operation === undefined) | |
operation = 0; | |
var opCount = 0; | |
var str = gulp.src(config.app + 'i18n/**/*.json') | |
.pipe(tap(function(file) { | |
var json = getObjFromJsonFile(file); | |
var prop = getDescendantProp(json, argv.property); | |
if(prop !== undefined || operation == 2) { | |
//if language is specified use it to filter | |
if(argv.language !== undefined && !checkLanguage(file, argv.language)) { | |
// console.log("skipping file not for specified language: " + file.relative); | |
return; | |
} | |
if(operation == 2 && !checkFilename(file, argv.filename)) { | |
// console.log("skipping unspecified file: " + file.relative); | |
return; | |
} | |
console.log(getOperationString(operation) + ' property value in file: ' + file.relative); | |
operation ? setDescendantProp(json, argv.property, argv.value) : deleteDescendantProp(json, argv.property); | |
opCount++; | |
file.contents = new Buffer(JSON.stringify(json, null, 4)); | |
} | |
})) | |
.pipe(gulp.dest(config.app + 'i18n/')); | |
if(!opCount) { | |
switch (operation) { | |
case 0: | |
case 1: | |
console.log('property ' + getOperationString(operation) + ' operation did not find property "' + argv.property + '" in any file'); | |
break; | |
case 2: | |
console.log('property ' + getOperationString(operation) + ' operation did not find file "' + argv.filename + '"'); | |
break; | |
} | |
} | |
return str; | |
} | |
function getOperationString(operation) { | |
switch (operation) { | |
case 0: | |
return 'deleting'; | |
case 1: | |
return 'setting'; | |
case 2: | |
return 'adding'; | |
} | |
} | |
function setDescendantProp(obj, desc, value) { | |
var arr = desc.split("."); | |
for(var i = 0; i <= arr.length; i++) { | |
var curr = arr.shift(); | |
if(obj[curr] === undefined && i < arr.length) | |
{ | |
obj[curr] = {}; | |
} | |
obj = obj[curr]; | |
} | |
obj[arr[0]] = value; | |
} | |
function getDescendantProp(obj, desc) { | |
var arr = desc.split('.'); | |
while (arr.length && (obj = obj[arr.shift()])); | |
return obj; | |
} | |
function deleteDescendantProp(obj, desc) { | |
var arr = desc.split("."); | |
while(arr.length > 1) { | |
obj = obj[arr.shift()]; | |
} | |
delete obj[arr[0]]; | |
} | |
function getObjFromJsonFile(file) { | |
try { | |
return JSON.parse(file.contents); | |
} | |
catch(e) { | |
return {}; | |
} | |
} | |
function checkLanguage(file, language) { | |
if(language !== undefined) { | |
var relSplit = file.relative.split('\\'); | |
if (relSplit.length) { | |
return relSplit[0] === language; | |
} | |
} | |
return false; | |
} | |
function checkFilename(file, filename) { | |
if(filename !== undefined) { | |
var relSplit = file.relative.split('\\'); | |
if (relSplit.length) { | |
return relSplit[1] === filename; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment