-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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'`. |
| 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'] | ||
| ); | ||
| }); | ||
| }); |
| 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'; | ||
|
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. IMO we should generate that and give it with the generated artifact so a built can be served independently
Member
Author
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. They are welcome to export the artifact from the source code and host it independently, we also, by default, host our schema |
||
| 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; | ||
| } |
| 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, | ||
| }; |
| 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 | ||
| } |
| 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>> | ||
| >; |
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.
yet another package... At least it is a dev one, so Node.js consuming doc-kit won't use it right?