Skip to content
7 changes: 3 additions & 4 deletions src/generators/legacy-json-all/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/generators/legacy-json/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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));
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/utils/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why this different order with api === report?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why this different order with api === report?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why this different order with api === report?

? {
stability,
stabilityText,
meta,
}
: {
meta,
stability,
stabilityText,
}),
classes,
methods,
properties,
miscs,
...(api === 'index' ? undefined : { modules }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why for api === index we gotta set nothing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why for api === index we gotta set nothing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 can you add inline comment explaining why for api === index we gotta set nothing?

globals,
},
null,
2
);
Loading