tldr/scripts/build-index.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2019-04-02 19:11:21 +01:00
const glob = require('glob');
function parsePlatform(pagefile) {
return pagefile.split(/\//)[1];
}
function parsePagename(pagefile) {
return pagefile.split(/\//)[2].replace(/\.md$/, '');
}
function parseLanguage(pagefile) {
2019-04-02 19:11:21 +01:00
let pagesFolder = pagefile.split(/\//)[0];
return pagesFolder == 'pages' ? 'en' : pagesFolder.replace(/^pages\./, '');
}
function buildPagesIndex(files) {
2019-04-02 19:11:21 +01:00
let reducer = function (index, file) {
let os = parsePlatform(file);
let page = parsePagename(file);
let language = parseLanguage(file);
if (index[page]) {
if (!index[page].platform.includes(os)) {
index[page].platform.push(os);
}
2019-04-02 19:11:21 +01:00
if (!index[page].language.includes(language)) {
index[page].language.push(language);
}
} else {
index[page] = {name: page, platform: [os], language: [language]};
}
2019-04-02 19:11:21 +01:00
return index;
};
2019-04-02 19:11:21 +01:00
let obj = files.reduce(reducer, {});
return Object.keys(obj)
.sort()
.map(function(page) {
return {
name: page,
platform: obj[page]["platform"],
language: obj[page]["language"]
};
});
}
function saveIndex(index) {
2019-04-02 19:11:21 +01:00
let indexFile = {
2016-01-24 08:54:27 +00:00
commands: index
};
2019-04-02 19:11:21 +01:00
2016-01-24 08:54:27 +00:00
console.log(JSON.stringify(indexFile));
}
glob('pages*/**/*.md', function (er, files) {
if (er !== null) {
console.error('ERROR finding pages!');
console.error(er);
return;
}
2019-04-02 19:11:21 +01:00
let index = buildPagesIndex(files);
saveIndex(index);
});