Last active
August 29, 2015 14:08
-
-
Save sudodoki/79116be1465c4e1613f1 to your computer and use it in GitHub Desktop.
Script used for nodeschool.github.io to add data-i18n attributes to index.html node
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
var fs = require('fs'); | |
var cheerio = require('cheerio'); | |
var find = require('cheerio-eq'); | |
var selectors = require('./languages/selectors.json'); | |
fs.readdir(__dirname, function (err, files) { | |
if (err) throw err; | |
files.filter(RegExp.prototype.test.bind(/\.html$/)).forEach(function (filename) { | |
processFile(filename); | |
}); | |
}); | |
function processFile(filename) { | |
fs.readFile(filename, function (err, data) { | |
if (err) return console.log('error is ', err); | |
var $ = cheerio.load(data); | |
// console.log($.root().html()) | |
Object.keys(selectors).forEach(function (selector) { | |
var i18nKey = selectors[selector]; | |
find($, selector).attr('data-i18n', i18nKey) | |
}); | |
fs.writeFile('new-' + filename, $.root().html(), function (err) { | |
if (err) console.log('failed to capture the result for ' + filename) | |
console.log(filename + ' is done, thanks') | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment