Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created June 13, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukem512/43f8687ac4430343d0b929cc1367fa7d to your computer and use it in GitHub Desktop.
Save lukem512/43f8687ac4430343d0b929cc1367fa7d to your computer and use it in GitHub Desktop.
Mustache-based translations
const fs = require('fs');
var exec = require('child_process').exec;
const DATA = 'data/';
const TEMPLATES = 'templates/';
const OUTPUT = 'build/';
// Retrieve language from args
const args = process.argv.slice(2);
const lang = args[0];
if (!lang || lang == "") {
console.error('No language specified');
process.exit(1);
}
// Check for corresponding data file
const dataFile = DATA + lang + '.json';
if (!fs.existsSync(dataFile)) {
console.error('Data file for "' + lang + '" not found in data directory');
process.exit(1);
}
// Run mustache for perform replacements
// Once per file in the templates directory
fs.readdir(TEMPLATES, (err, files) => {
if (err) {
console.error("Unable to access data directory", err);
process.exit(1);
}
files.forEach((file, index) => {
const templateFile = TEMPLATES + file;
const outputFile = OUTPUT + file;
exec(`mustache ${dataFile} ${templateFile} > ${outputFile}`, (err) => {
if (err) {
console.error('Unable to run mustache on template', err);
process.exit(1);
}
console.log(`Processed ${templateFile} to ${outputFile}`);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment