factbook.json/buildIndex.js
Martin Trobäck 2d44771eb7 Code cleanup
2016-11-07 12:53:08 +01:00

35 lines
No EOL
999 B
JavaScript

const fs = require('fs');
const exclude = ['almanac.md', 'readme.md', 'summary.md', 'buildindex.js', 'package.json', 'meta', '.git', 'countryindex.json'];
const countryIndex = [];
const readFile = (dir, file) => {
const country = JSON.parse(fs.readFileSync(`${dir}/${file}`).toString());
const currentCountry = {
directory: dir,
filename: file
};
currentCountry.country = (country.Government['Country name'] &&
country.Government['Country name']['conventional short form']) ?
country.Government['Country name']['conventional short form'].text : '';
countryIndex.push(currentCountry);
};
const readDir = (dir) => {
if (!exclude.includes(dir.toLowerCase())) {
const continent = fs.readdirSync(dir);
continent.forEach((country) => {
readFile(dir, country);
});
}
};
const continents = fs.readdirSync('./');
continents.forEach((continent) => {
readDir(continent);
});
fs.writeFileSync('countryIndex.json', JSON.stringify(countryIndex, null, 2));