Created
August 25, 2020 11:39
-
-
Save predorock/a8d37d4d75223476a18c4e756b21524e to your computer and use it in GitHub Desktop.
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
const gulp = require('gulp'); | |
const csv = require('csv-parser'); | |
const fs = require('fs'); | |
const _ = require('lodash'); | |
const paths = { | |
translations: { | |
input: [ | |
'./translations/medics_translations.csv', | |
'./translations/intro_text_translations.csv', | |
'./translations/invoice_validation.csv', | |
'./translations/messages_templates.csv' | |
], | |
output: './src/assets/i18n' | |
}, | |
permissions: { | |
input: [ | |
'./permissions/permissions.csv', | |
'./permissions/general_info_permissions.csv', | |
'./permissions/news.permissions.csv', | |
'./permissions/medical-case.permissions.csv' | |
], | |
output: './src/assets/permissions', | |
outputFileName: 'permissions.json' | |
} | |
}; | |
/* | |
* Converts a string to a bool. | |
* | |
* This conversion will: | |
* | |
* - match 'true', 'on', or '1' as true. | |
* - ignore all white-space padding | |
* - ignore capitalization (case). | |
* | |
* ' tRue ','ON', and '1 ' will all evaluate as true. | |
* | |
*/ | |
function strToBool(s) | |
{ | |
// will match one and only one of the string 'true','1', or 'on' rerardless | |
// of capitalization and regardless off surrounding white-space. | |
// | |
regex=/^\s*(true|1|on)\s*$/i | |
return regex.test(s); | |
} | |
gulp.task('translations', () => { | |
const _translations = {}; | |
const inputs = [].concat(paths.translations.input); | |
inputs.forEach(file => { | |
fs.createReadStream(file) | |
.pipe(csv()) | |
.on('data', (entry) => { | |
const {key} = entry; | |
delete entry.key; | |
_.forEach(entry, (value, lang) => { | |
if (_.isNil(_translations[lang])) { | |
_translations[lang] = {}; | |
} | |
_translations[lang][key] = value; | |
}); | |
}) | |
.on('end', (ev) => { | |
_.forEach(_translations, function (localeDict, locale) { | |
const fileName = `${locale}.json`; | |
fs.writeFileSync(`${paths.translations.output}/${fileName}`, JSON.stringify(localeDict)); | |
console.log(`Translating ${file} ${locale} done!`); | |
}); | |
}); | |
}) | |
}); | |
gulp.task('permissions', () => { | |
const permissions = {}; | |
const inputs = [].concat(paths.permissions.input); | |
inputs.forEach(file => { | |
fs.createReadStream(file) | |
.pipe(csv()) | |
.on('data', (entry) => { | |
const role = entry.role.trim(); | |
delete entry.role; | |
if (!permissions[role]) { | |
permissions[role] = []; | |
} | |
_.forEach(entry, (value, permission) => { | |
const isActive = strToBool(value); | |
if (isActive) { | |
const perm = permission.trim(); | |
const hasAlreadyPermission = permissions[role].indexOf(perm); | |
if (hasAlreadyPermission >= 0) { | |
console.warn(`Warning: Permission '${perm}' for role '${role}' has already been inserted, the value will be overwrite`); | |
permissions[role][hasAlreadyPermission] = perm; | |
} else { | |
permissions[role].push(perm); | |
} | |
} | |
}); | |
}) | |
.on('end', (ev) => { | |
const outPath = `${paths.permissions.output}/${paths.permissions.outputFileName}`; | |
fs.writeFileSync(outPath, JSON.stringify(permissions)); | |
console.log(`Permission loaded from ${file} to ${outPath}`); | |
}); | |
}) | |
}); | |
gulp.task('default', ['translations', 'permissions']); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment