11cb0ef41Sopenharmony_ci// Build all.json by combining the miscs, modules, classes, globals, and methods
21cb0ef41Sopenharmony_ci// from the generated json files.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciimport fs from 'fs';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst source = new URL('../../out/doc/api/', import.meta.url);
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Get a list of generated API documents.
91cb0ef41Sopenharmony_ciconst jsonFiles = fs.readdirSync(source, 'utf8')
101cb0ef41Sopenharmony_ci  .filter((name) => name.includes('.json') && name !== 'all.json');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Read the table of contents.
131cb0ef41Sopenharmony_ciconst toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Initialize results. Only these four data values will be collected.
161cb0ef41Sopenharmony_ciconst results = {
171cb0ef41Sopenharmony_ci  miscs: [],
181cb0ef41Sopenharmony_ci  modules: [],
191cb0ef41Sopenharmony_ci  classes: [],
201cb0ef41Sopenharmony_ci  globals: [],
211cb0ef41Sopenharmony_ci  methods: [],
221cb0ef41Sopenharmony_ci};
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// Identify files that should be skipped. As files are processed, they
251cb0ef41Sopenharmony_ci// are added to this list to prevent dupes.
261cb0ef41Sopenharmony_ciconst seen = new Set(['all.json', 'index.json']);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// Extract (and concatenate) the selected data from each document.
291cb0ef41Sopenharmony_ci// Expand hrefs found in json to include source HTML file.
301cb0ef41Sopenharmony_cifor (const link of toc.match(/<a.*?>/g)) {
311cb0ef41Sopenharmony_ci  const href = /href="(.*?)"/.exec(link)[1];
321cb0ef41Sopenharmony_ci  const json = href.replace('.html', '.json');
331cb0ef41Sopenharmony_ci  if (!jsonFiles.includes(json) || seen.has(json)) continue;
341cb0ef41Sopenharmony_ci  const data = JSON.parse(
351cb0ef41Sopenharmony_ci    fs.readFileSync(new URL(`./${json}`, source), 'utf8')
361cb0ef41Sopenharmony_ci      .replace(/<a href=\\"#/g, `<a href=\\"${href}#`),
371cb0ef41Sopenharmony_ci  );
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  for (const property in data) {
401cb0ef41Sopenharmony_ci    if (Object.hasOwn(results, property)) {
411cb0ef41Sopenharmony_ci      data[property].forEach((mod) => {
421cb0ef41Sopenharmony_ci        mod.source = data.source;
431cb0ef41Sopenharmony_ci      });
441cb0ef41Sopenharmony_ci      results[property].push(...data[property]);
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // Mark source as seen.
491cb0ef41Sopenharmony_ci  seen.add(json);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// Write results.
531cb0ef41Sopenharmony_cifs.writeFileSync(new URL('./all.json', source),
541cb0ef41Sopenharmony_ci                 `${JSON.stringify(results, null, 2)}\n`, 'utf8');
55