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