build-index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: MIT
  2. 'use strict';
  3. const { glob } = require('glob');
  4. function parsePlatform(pagefile) {
  5. return pagefile.split(/\//)[1];
  6. }
  7. function parsePagename(pagefile) {
  8. return pagefile.split(/\//)[2].replace(/\.md$/, '');
  9. }
  10. function parseLanguage(pagefile) {
  11. let pagesFolder = pagefile.split(/\//)[0];
  12. return pagesFolder == 'pages' ? 'en' : pagesFolder.replace(/^pages\./, '');
  13. }
  14. function buildPagesIndex(files) {
  15. let reducer = function (index, file) {
  16. let os = parsePlatform(file);
  17. let page = parsePagename(file);
  18. let language = parseLanguage(file);
  19. if (index[page]) {
  20. if (!index[page].platform.includes(os)) {
  21. index[page].platform.push(os);
  22. }
  23. if (!index[page].language.includes(language)) {
  24. index[page].language.push(language);
  25. }
  26. const targets = index[page].targets;
  27. const exists = targets.some((t) => {return t.platform === os && t.language === language});
  28. if (!exists) {
  29. targets.push({os, language})
  30. }
  31. } else {
  32. index[page] = {
  33. name: page,
  34. platform: [os],
  35. language: [language],
  36. targets: [{os, language}]
  37. };
  38. }
  39. return index;
  40. };
  41. let obj = files.reduce(reducer, {});
  42. return Object.keys(obj)
  43. .sort()
  44. .map(function(page) {
  45. return {
  46. name: page,
  47. platform: obj[page].platform,
  48. language: obj[page].language,
  49. targets: obj[page].targets
  50. };
  51. });
  52. }
  53. function saveIndex(index) {
  54. let indexFile = {
  55. commands: index
  56. };
  57. console.log(JSON.stringify(indexFile));
  58. }
  59. (async () => {
  60. const files = await glob('pages*/**/*.md');
  61. let index = buildPagesIndex(files);
  62. saveIndex(index);
  63. })().then(() => {
  64. process.exit(0);
  65. }).catch((err) => {
  66. console.error('ERROR building index!');
  67. console.error(er);
  68. process.exit(1);
  69. });