-
Notifications
You must be signed in to change notification settings - Fork 19
feat: json, json-all generators #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flakey5
wants to merge
5
commits into
main
Choose a base branch
from
flakey5/20240909/new-json-gen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import assert from 'node:assert'; | ||
| import { join } from 'node:path'; | ||
| import { test } from 'node:test'; | ||
|
|
||
| import { Validator } from 'jsonschema'; | ||
| import { SemVer } from 'semver'; | ||
|
|
||
| import { BASE_URL } from '../../../constants.mjs'; | ||
| import createGenerator from '../../../generators.mjs'; | ||
| import createMarkdownLoader from '../../../loaders/markdown.mjs'; | ||
| import createMarkdownParser from '../../../parsers/markdown.mjs'; | ||
| import { SCHEMA_FILENAME } from '../constants.mjs'; | ||
| import jsonAll from '../index.mjs'; | ||
| import { generateJsonSchema } from '../util/generateJsonSchema.mjs'; | ||
|
|
||
| const FIXTURES_DIR = join( | ||
| import.meta.dirname, | ||
| '..', | ||
| 'json', | ||
| '__tests__', | ||
| 'fixtures' | ||
| ); | ||
|
|
||
| const loader = createMarkdownLoader(); | ||
| const parser = createMarkdownParser(); | ||
|
|
||
| test('generator output complies with json schema', async () => { | ||
| const validator = new Validator(); | ||
|
|
||
| const version = 'v1.2.3'; | ||
|
|
||
| /** | ||
| * @type {object} | ||
| */ | ||
| const schema = generateJsonSchema(version); | ||
|
|
||
| const input = [ | ||
| join(FIXTURES_DIR, 'text-doc.md'), | ||
| join(FIXTURES_DIR, 'module.md'), | ||
| ]; | ||
| const files = await loader.loadFiles(input); | ||
| const docs = await parser.parseApiDocs(files); | ||
|
|
||
| const { runGenerators } = createGenerator(docs); | ||
|
|
||
| const result = await runGenerators({ | ||
| generators: ['json-all'], | ||
| input, | ||
| output: undefined, | ||
| version: new SemVer('v1.2.3'), | ||
| releases: [], | ||
| gitRef: 'a'.repeat(40), | ||
| threads: 1, | ||
| typeMap: {}, | ||
| }); | ||
|
|
||
| assert.ok(validator.validate(result, schema).valid); | ||
| }); | ||
|
|
||
| test('combines json correctly', async () => { | ||
| const version = 'v1.2.3'; | ||
|
|
||
| /** | ||
| * @type {Array<import('../../json/generated.d.ts').NodeJsAPIDocumentationSchema>} | ||
| */ | ||
| const jsonOutput = [ | ||
| { | ||
| $schema: `https://nodejs.org/docs/${version}/api/node-doc-schema.json`, | ||
| source: 'doc/api/some-module.md', | ||
| type: 'module', | ||
| '@name': 'Some Module', | ||
| '@module': 'node:module', | ||
| classes: [], | ||
| }, | ||
| { | ||
| $schema: `https://nodejs.org/docs/${version}/api/node-doc-schema.json`, | ||
| source: 'doc/api/some-module.md', | ||
| type: 'text', | ||
| '@name': 'Some Module', | ||
| description: 'asdf', | ||
| text: [ | ||
| { | ||
| type: 'text', | ||
| '@name': 'asdf', | ||
| description: 'bla bla bla', | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const result = await jsonAll.generate(jsonOutput, { | ||
| version: new SemVer(version), | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(result, { | ||
| $schema: `${BASE_URL}docs/${version}/api/${SCHEMA_FILENAME}`, | ||
| modules: [ | ||
| { | ||
| type: 'module', | ||
| '@name': 'Some Module', | ||
| '@module': 'node:module', | ||
| classes: [], | ||
| }, | ||
| ], | ||
| text: [ | ||
| { | ||
| type: 'text', | ||
| '@name': 'Some Module', | ||
| description: 'asdf', | ||
| text: [ | ||
| { | ||
| type: 'text', | ||
| '@name': 'asdf', | ||
| description: 'bla bla bla', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const SCHEMA_FILENAME = 'node-doc-all-schema.json'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import { writeFile } from 'node:fs/promises'; | ||||||||||||||||||||||||
| import { join } from 'node:path'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import { SCHEMA_FILENAME } from './constants.mjs'; | ||||||||||||||||||||||||
| import { BASE_URL } from '../../constants.mjs'; | ||||||||||||||||||||||||
| import { generateJsonSchema } from './util/generateJsonSchema.mjs'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * This generator is responsible for collecting the JSON output generated by | ||||||||||||||||||||||||
| * the `json` generator into a single JSON file. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @typedef {Array<ApiDocMetadataEntry>} Input | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @type {GeneratorMetadata<Input, object>} | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export default { | ||||||||||||||||||||||||
| name: 'json-all', | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // This should be kept in sync with the JSON schema version for this | ||||||||||||||||||||||||
| // generator AND the `json` generator | ||||||||||||||||||||||||
| version: '2.0.0', | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| description: | ||||||||||||||||||||||||
| 'This generator is responsible for collecting the JSON output generated by the `json` generator into a single JSON file.', | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| dependsOn: 'json', | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Generates a JSON file. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @param {Input} input | ||||||||||||||||||||||||
| * @param {Partial<GeneratorOptions>} param1 | ||||||||||||||||||||||||
| * @returns {Promise<object>} | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| async generate(input, { version, output }) { | ||||||||||||||||||||||||
| const versionString = `v${version.toString()}`; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const generatedValue = { | ||||||||||||||||||||||||
| $schema: `${BASE_URL}docs/${versionString}/api/${SCHEMA_FILENAME}`, | ||||||||||||||||||||||||
| modules: [], | ||||||||||||||||||||||||
| text: [], | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const propertiesToIgnore = ['$schema', 'source']; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| input.forEach(section => { | ||||||||||||||||||||||||
| const copiedSection = {}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Object.keys(section).forEach(key => { | ||||||||||||||||||||||||
| if (!propertiesToIgnore.includes(key)) { | ||||||||||||||||||||||||
| copiedSection[key] = section[key]; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
+48
to
+55
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
WDYT? |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| switch (section.type) { | ||||||||||||||||||||||||
| case 'module': | ||||||||||||||||||||||||
| generatedValue.modules.push(copiedSection); | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| case 'text': | ||||||||||||||||||||||||
| generatedValue.text.push(copiedSection); | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||
| throw new TypeError(`unsupported root section type ${section.type}`); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+57
to
+66
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit |
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (output) { | ||||||||||||||||||||||||
| const schema = generateJsonSchema(versionString); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Write the parsed JSON schema to the output directory | ||||||||||||||||||||||||
| await writeFile(join(output, SCHEMA_FILENAME), JSON.stringify(schema)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return generatedValue; | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| import { BASE_URL } from '../../../constants.mjs'; | ||
| import { SCHEMA_FILENAME } from '../constants.mjs'; | ||
| import jsonAll from '../index.mjs'; | ||
|
|
||
| /** | ||
| * @param {string} version | ||
| */ | ||
| export function generateJsonSchema(version) { | ||
| const jsonSchemaUrl = `${BASE_URL}/docs/${version}/api/${SCHEMA_FILENAME}`; | ||
|
|
||
| return { | ||
| $schema: 'http://json-schema.org/draft-07/schema#', | ||
| $id: `nodejs-api-doc-all@v${jsonAll.version}`, | ||
| title: 'Node.js API Documentation Schema (All)', | ||
| readOnly: true, | ||
|
|
||
| properties: { | ||
| modules: { | ||
| type: 'array', | ||
| items: { $ref: `${jsonSchemaUrl}/#/definitions/Module` }, | ||
| }, | ||
| text: { | ||
| type: 'array', | ||
| items: { $ref: `${jsonSchemaUrl}/#/definitions/Text` }, | ||
| }, | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Module | ||
|
|
||
| <!-- introduced_in=v20.0.0 --> | ||
| <!-- source_link=lib/module.js --> | ||
|
|
||
| > Stability: 2 - Stable | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. | ||
|
|
||
| Nulla sagittis pulvinar lacus et venenatis. Morbi eget varius sem, a rutrum velit. Vestibulum placerat vehicula felis. Proin et porttitor augue. Sed feugiat urna sit amet euismod posuere. Phasellus quis pretium velit. Integer porta ut velit ac consectetur. Pellentesque venenatis leo et volutpat dapibus. Nam consectetur quam venenatis feugiat tempus. Curabitur semper fringilla felis. | ||
|
|
||
| Donec sed est at nulla lacinia suscipit id id neque. Cras quis risus a risus mattis consectetur ut ut ex. Cras non metus a orci facilisis laoreet. Sed eu nisi sit amet mauris iaculis placerat. Nullam interdum, lectus sed convallis tristique, nibh nibh ullamcorper leo, non vehicula metus elit a nisl. Donec eros felis, ornare eget tempor nec, egestas mattis elit. Integer semper purus at risus pretium, in tristique ipsum tristique. | ||
|
|
||
| ```mjs | ||
| import { something } from 'node:something'; | ||
| ``` | ||
|
|
||
| ## text section | ||
|
|
||
| <!-- YAML | ||
| changes: | ||
| - version: v3.0.0 | ||
| pr-url: https://github.com/nodejs/node/pull/1234 | ||
| description: asdf | ||
| --> | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. | ||
|
|
||
| Nulla sagittis pulvinar lacus et venenatis. Morbi eget varius sem, a rutrum velit. Vestibulum placerat vehicula felis. Proin et porttitor augue. Sed feugiat urna sit amet euismod posuere. Phasellus quis pretium velit. Integer porta ut velit ac consectetur. Pellentesque venenatis leo et volutpat dapibus. Nam consectetur quam venenatis feugiat tempus. Curabitur semper fringilla felis. | ||
|
|
||
| Donec sed est at nulla lacinia suscipit id id neque. Cras quis risus a risus mattis consectetur ut ut ex. Cras non metus a orci facilisis laoreet. Sed eu nisi sit amet mauris iaculis placerat. Nullam interdum, lectus sed convallis tristique, nibh nibh ullamcorper leo, non vehicula metus elit a nisl. Donec eros felis, ornare eget tempor nec, egestas mattis elit. Integer semper purus at risus pretium, in tristique ipsum tristique. | ||
|
|
||
| ## Class: `Something` | ||
|
|
||
| <!-- YAML | ||
| added: | ||
| - v15.7.0 | ||
| - v14.18.0 | ||
| changes: | ||
| - version: | ||
| - v18.0.0 | ||
| - v16.17.0 | ||
| pr-url: https://github.com/nodejs/node/pull/1234 | ||
| description: No longer experimental. | ||
| --> | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. | ||
|
|
||
| ### `new something.Something([sources[, options]])` | ||
|
|
||
| - `sources` {string\[]|object} something something bla bla bla | ||
| - `options` {object} something something bla bla bla | ||
| - `optionA` {boolean} asdf | ||
| - `bla` qwerty | ||
|
|
||
| ### `something.doThing()` | ||
|
|
||
| - Returns: {Promise} | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. | ||
|
|
||
| ### Static method: `something.doStaticThing()` | ||
|
|
||
| - Returns: {Promise} | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. | ||
|
|
||
| ### Event: `somethingHappened` | ||
|
|
||
| - `thing` {string} thing that happened | ||
| - `how` {string|object} bla | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet est sit amet nisi iaculis ornare. Donec eu orci condimentum, semper quam ut, luctus nisl. Aliquam vel erat efficitur, egestas justo a, accumsan mauris. Quisque scelerisque enim rhoncus ornare iaculis. Sed commodo, sem ac accumsan posuere, sapien magna eleifend massa, sit amet rhoncus sem diam quis nulla. Donec feugiat elit a tellus hendrerit congue quis bibendum urna. Pellentesque mollis tincidunt tortor, in lobortis ex fringilla a. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit