factbook.json/buildIndex.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-11-07 12:04:29 +01:00
const fs = require('fs');
2016-11-07 14:28:47 +01:00
const exclude = ['almanac.md', 'readme.md', 'summary.md', 'buildindex.js',
'package.json', 'meta', '.git', 'countryindex.json', 'alternatecountryindex.json'];
2016-11-07 12:04:29 +01:00
const countryIndex = [];
2016-11-07 14:28:47 +01:00
const alternateIndex = {};
2016-11-07 12:04:29 +01:00
2016-11-07 14:28:47 +01:00
// Read files in sub directories and extract country information
2016-11-07 12:04:29 +01:00
const readFile = (dir, file) => {
const country = JSON.parse(fs.readFileSync(`${dir}/${file}`).toString());
const currentCountry = {
directory: dir,
filename: file
};
2016-11-07 12:53:08 +01:00
currentCountry.country = (country.Government['Country name'] &&
country.Government['Country name']['conventional short form']) ?
country.Government['Country name']['conventional short form'].text : '';
2016-11-07 12:04:29 +01:00
countryIndex.push(currentCountry);
2016-11-07 14:28:47 +01:00
if (!alternateIndex[dir]) {
alternateIndex[dir] = [];
}
alternateIndex[dir].push({
filename: file,
country: currentCountry.country
});
2016-11-07 12:04:29 +01:00
};
2016-11-07 14:28:47 +01:00
// Find files in sub directories
2016-11-07 12:04:29 +01:00
const readDir = (dir) => {
if (!exclude.includes(dir.toLowerCase())) {
const continent = fs.readdirSync(dir);
continent.forEach((country) => {
readFile(dir, country);
});
}
};
2016-11-07 14:28:47 +01:00
// Read the directories in the root folder
2016-11-07 12:04:29 +01:00
const continents = fs.readdirSync('./');
continents.forEach((continent) => {
readDir(continent);
});
2016-11-07 14:28:47 +01:00
// Create output files
fs.writeFileSync('countryIndex.json', JSON.stringify(countryIndex, null, 2));
fs.writeFileSync('alternateCountryIndex.json', JSON.stringify([alternateIndex], null, 2));