-
Notifications
You must be signed in to change notification settings - Fork 67
feat(json): add JSON generators #1079
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
avivkeller
wants to merge
2
commits into
main
Choose a base branch
from
json-generators
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 all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@doc-kit/core': minor | ||
| '@doc-kit/generator-react': patch | ||
| '@node-core/doc-kit-legacy': patch | ||
| --- | ||
|
|
||
| feat: the `json` and `json-all` generators |
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
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
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,31 @@ | ||
| # `json-all` Generator | ||
|
|
||
| The `json-all` generator bundles the documents of the [`json`](./json.md) | ||
| generator into a single `all.json` file. | ||
|
|
||
| ```sh | ||
| npx @doc-kit/cli generate -t json-all -i "doc/api/*.md" -o out --index doc/api/index.md | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "$schema": "https://doc-kit.nodejs.org/schemas/api-doc-all/1.0.0.json", | ||
| "documents": [] | ||
| } | ||
| ``` | ||
|
|
||
| `documents` holds every document in the order of the configured `index`, | ||
| then the rest by `id`. The bundle's schema, shipped as | ||
| `@doc-kit/core/generators/json-all/schema.json`, refers to the `json` | ||
| generator's schema for the documents. | ||
|
|
||
| ## Configuring | ||
|
|
||
| - `output` {string} The directory where `all.json` will be written. | ||
| - `minify` {boolean} Whether to minify the output. Inherited from `global`. | ||
| **Default:** `true`. | ||
| - `index` {Array} The `{ api }` objects defining the document order. Inherited | ||
| from `global`. | ||
| - `schemaURL` {string} Where the bundle's schema is published. | ||
| `{schemaVersion}` is filled in. **Default:** | ||
| `'https://doc-kit.nodejs.org/schemas/api-doc-all/{schemaVersion}.json'`. |
34 changes: 34 additions & 0 deletions
34
packages/core/src/generators/json-all/__tests__/generate.test.mjs
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,34 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { setConfig } from '#utils/configuration/index.mjs'; | ||
|
|
||
| import { SCHEMA_VERSION } from '../../json/constants.mjs'; | ||
| import { generate } from '../generate.mjs'; | ||
|
|
||
| const config = await setConfig({ target: ['json-all'] }); | ||
|
|
||
| const document = id => ({ id, path: `/${id}`, children: [] }); | ||
|
|
||
| describe('json-all', () => { | ||
| it('bundles the documents in index order, then by id', async () => { | ||
| config['json-all'].index = [ | ||
| { section: 'HTTP', api: 'http' }, | ||
| { section: 'File system', api: 'fs' }, | ||
| ]; | ||
| config['json-all'].output = undefined; | ||
|
|
||
| const bundle = await generate( | ||
| ['zlib', 'fs', 'assert', 'http'].map(document) | ||
| ); | ||
|
|
||
| assert.equal( | ||
| bundle.$schema, | ||
| `https://doc-kit.nodejs.org/schemas/api-doc-all/${SCHEMA_VERSION}.json` | ||
| ); | ||
| assert.deepEqual( | ||
| bundle.documents.map(({ id }) => id), | ||
| ['http', 'fs', 'assert', 'zlib'] | ||
| ); | ||
| }); | ||
| }); |
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,5 @@ | ||
| 'use strict'; | ||
|
|
||
| // Where a version of the bundle's schema is published. | ||
| export const SCHEMA_URL = | ||
| 'https://doc-kit.nodejs.org/schemas/api-doc-all/{schemaVersion}.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,35 @@ | ||
| 'use strict'; | ||
|
|
||
| import { join } from 'node:path'; | ||
|
|
||
| import getConfig from '#utils/configuration/index.mjs'; | ||
| import { writeJSON } from '#utils/file.mjs'; | ||
|
|
||
| import { resolveSchemaURL } from '../json/utils/schema.mjs'; | ||
|
|
||
| /** | ||
| * Bundles the `json` generator's documents into one `all.json` file. | ||
| * | ||
| * @type {import('./types').Generator['generate']} | ||
| */ | ||
| export async function generate(input) { | ||
| const config = getConfig('json-all'); | ||
|
|
||
| // Documents follow the configured index; the rest go after it, by id | ||
| const order = new Map(config.index?.map(({ api }, i) => [api, i])); | ||
|
|
||
| const documents = input.toSorted( | ||
| (a, b) => | ||
| (order.get(a.id) ?? Infinity) - (order.get(b.id) ?? Infinity) || | ||
| a.id.localeCompare(b.id) | ||
| ); | ||
|
|
||
| /** @type {import('./types').Bundle} */ | ||
| const bundle = { $schema: resolveSchemaURL(config), documents }; | ||
|
|
||
| if (config.output) { | ||
| await writeJSON(join(config.output, 'all.json'), bundle, config.minify); | ||
| } | ||
|
|
||
| return bundle; | ||
| } |
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,25 @@ | ||
| 'use strict'; | ||
|
|
||
| import { SCHEMA_URL } from './constants.mjs'; | ||
| import { generate } from './generate.mjs'; | ||
|
|
||
| /** | ||
| * This generator bundles the documents of the `json` generator into a single | ||
| * `all.json` file | ||
| * | ||
| * @type {import('./types').Generator} | ||
| */ | ||
| export default { | ||
| name: 'json-all', | ||
|
|
||
| description: | ||
| 'Bundles the documents of the `json` generator into a single `all.json` file', | ||
|
|
||
| dependsOn: '@doc-kit/core/json', | ||
|
|
||
| defaultConfiguration: { | ||
| schemaURL: SCHEMA_URL, | ||
| }, | ||
|
|
||
| generate, | ||
| }; |
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,22 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "$id": "https://doc-kit.nodejs.org/schemas/api-doc-all/1.0.0.json", | ||
| "title": "Bundle", | ||
| "description": "Every document of a documentation set, as emitted by the doc-kit `json-all` generator, in index order.", | ||
| "type": "object", | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string", | ||
| "description": "The URL of the schema this bundle conforms to. Its last path segment is the schema version." | ||
| }, | ||
| "documents": { | ||
| "type": "array", | ||
| "items": { | ||
| "$ref": "https://doc-kit.nodejs.org/schemas/api-doc/1.0.0.json" | ||
| }, | ||
| "description": "The documents, in the order of the configured index, then by id." | ||
| } | ||
| }, | ||
| "required": ["$schema", "documents"], | ||
| "additionalProperties": false | ||
| } |
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,20 @@ | ||
| import type { Document } from '../json/generated/schema'; | ||
|
|
||
| /** | ||
| * Every document of a documentation set, in index order. | ||
| */ | ||
| export interface Bundle { | ||
| /** The URL of the schema the bundle conforms to */ | ||
| $schema: string; | ||
| documents: Array<Document>; | ||
| } | ||
|
|
||
| export interface Configuration { | ||
| /** Where the schema is published; `{schemaVersion}` is filled in */ | ||
| schemaURL: string; | ||
| } | ||
|
|
||
| export type Generator = GeneratorMetadata< | ||
| Configuration, | ||
| Generate<Array<Document>, Promise<Bundle>> | ||
| >; |
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
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.
IMO we should generate that and give it with the generated artifact so a built can be served independently
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.
They are welcome to export the artifact from the source code and host it independently, we also, by default, host our schema