forked from tldr-pages/tldr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-index.js
83 lines (67 loc) · 1.78 KB
/
build-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-License-Identifier: MIT
'use strict';
const glob = require('glob');
function parsePlatform(pagefile) {
return pagefile.split(/\//)[1];
}
function parsePagename(pagefile) {
return pagefile.split(/\//)[2].replace(/\.md$/, '');
}
function parseLanguage(pagefile) {
let pagesFolder = pagefile.split(/\//)[0];
return pagesFolder == 'pages' ? 'en' : pagesFolder.replace(/^pages\./, '');
}
function buildPagesIndex(files) {
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);
}
if (!index[page].language.includes(language)) {
index[page].language.push(language);
}
const targets = index[page].targets;
const exists = targets.some((t) => {return t.platform === os && t.language === language});
if (!exists) {
targets.push({os, language})
}
} else {
index[page] = {
name: page,
platform: [os],
language: [language],
targets: [{os, language}]
};
}
return index;
};
let obj = files.reduce(reducer, {});
return Object.keys(obj)
.sort()
.map(function(page) {
return {
name: page,
platform: obj[page].platform,
language: obj[page].language,
targets: obj[page].targets
};
});
}
function saveIndex(index) {
let indexFile = {
commands: index
};
console.log(JSON.stringify(indexFile));
}
glob('pages*/**/*.md', function (er, files) {
if (er !== null) {
console.error('ERROR finding pages!');
console.error(er);
return;
}
let index = buildPagesIndex(files);
saveIndex(index);
});