diff --git a/src/generators/legacy-json-all/index.mjs b/src/generators/legacy-json-all/index.mjs index a98eb980..3667b23e 100644 --- a/src/generators/legacy-json-all/index.mjs +++ b/src/generators/legacy-json-all/index.mjs @@ -3,6 +3,8 @@ import { writeFile } from 'node:fs/promises'; import { join } from 'node:path'; +import { legacyToJSON } from '../../utils/generators.mjs'; + /** * This generator consolidates data from the `legacy-json` generator into a single * JSON file (`all.json`). @@ -83,10 +85,7 @@ export default { } if (output) { - await writeFile( - join(output, 'all.json'), - JSON.stringify(generatedValue, null, 2) - ); + await writeFile(join(output, 'all.json'), legacyToJSON(generatedValue)); } return generatedValue; diff --git a/src/generators/legacy-json/index.mjs b/src/generators/legacy-json/index.mjs index 7b6448d7..a897863d 100644 --- a/src/generators/legacy-json/index.mjs +++ b/src/generators/legacy-json/index.mjs @@ -4,7 +4,7 @@ import { writeFile } from 'node:fs/promises'; import { join } from 'node:path'; import { createSectionBuilder } from './utils/buildSection.mjs'; -import { groupNodesByModule } from '../../utils/generators.mjs'; +import { groupNodesByModule, legacyToJSON } from '../../utils/generators.mjs'; const buildSection = createSectionBuilder(); @@ -78,7 +78,7 @@ export default { for (const section of chunkResult) { const out = join(output, `${section.api}.json`); - await writeFile(out, JSON.stringify(section, null, 2)); + await writeFile(out, legacyToJSON(section)); } } diff --git a/src/utils/generators.mjs b/src/utils/generators.mjs index ed0c89de..87eaff95 100644 --- a/src/utils/generators.mjs +++ b/src/utils/generators.mjs @@ -109,3 +109,63 @@ export const sortChanges = (changes, key = 'version') => { */ export const leftHandAssign = (target, source) => Object.keys(source).forEach(k => k in target || (target[k] = source[k])); + +/** + * Transforms an object to JSON output consistent with the JSON version. + * @param {Object} section - The source object + * @param section.api + * @param section.type + * @param section.source + * @param section.introduced_in + * @param section.meta + * @param section.stability + * @param section.stabilityText + * @param section.classes + * @param section.methods + * @param section.properties + * @param section.miscs + * @param section.modules + * @param section.globals + * @returns {string} - The JSON output + */ +export const legacyToJSON = ({ + api, + type, + source, + introduced_in, + meta, + stability, + stabilityText, + classes, + methods, + properties, + miscs, + modules, + globals, +}) => + JSON.stringify( + { + type, + source, + introduced_in, + ...(api === 'report' + ? { + stability, + stabilityText, + meta, + } + : { + meta, + stability, + stabilityText, + }), + classes, + methods, + properties, + miscs, + ...(api === 'index' ? undefined : { modules }), + globals, + }, + null, + 2 + );