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
49 changes: 49 additions & 0 deletions src/utils/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,52 @@ 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
* @returns {string} - The JSON output
*/
export const legacyToJSON = section =>
JSON.stringify(
section,
[
// TODO: remove this array once all the additional keys have been introduced downstream
'added',
'changes',
'classes',
'classMethods',
'commit',
'ctors',
'default',
'deprecated',
'desc',
'description',
'displayName',
'events',
'examples',
'globals',
'introduced_in',
'meta',
'methods',
'miscs',
...(section.api === 'index' ? [] : ['modules']),
'name',
'napiVersion',
'options',
'params',
'pr-url',
'properties',
'removed',
'return',
'shortDesc',
'signatures',
'source',
'stability',
'stabilityText',
'textRaw',
'type',
'version',
],
2
);
Loading