Skip to content

Commit 35ecdc7

Browse files
committed
tmp
1 parent 71c56a7 commit 35ecdc7

File tree

8 files changed

+116
-32
lines changed

8 files changed

+116
-32
lines changed

src/generators/json-all/example.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/generators/json-all/index.mjs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// @ts-check
2+
'use strict';
3+
4+
import { writeFile, readFile } from 'node:fs/promises';
5+
import { join } from 'node:path';
6+
import { parse as jsoncParse } from 'jsonc-parser';
7+
8+
// TODO add test w/ https://www.npmjs.com/package/jsonschema
9+
10+
/**
11+
* TODO docs
12+
*
13+
* @typedef {Array<ApiDocMetadataEntry>} Input
14+
*
15+
* @type {GeneratorMetadata<Input, Array<import('./generated.d.ts').NodeJsAPIDocumentationSchema>>}
16+
*/
17+
export default {
18+
name: 'json-all',
19+
20+
// This should be kept in sync with the JSON schema version
21+
version: '2.0.0',
22+
23+
description: 'TODO',
24+
25+
dependsOn: 'json',
26+
27+
/**
28+
* Generates a JSON file.
29+
*
30+
* @param {Input} input
31+
* @param {Partial<GeneratorOptions>} param1
32+
* @returns {Promise<any>}
33+
*/
34+
async generate(input, { output }) {
35+
const generatedValue = {
36+
$schema: './node-doc-all-schema.jsonc',
37+
modules: [],
38+
text: []
39+
}
40+
41+
const propertiesToIgnore = [
42+
'$schema',
43+
'source',
44+
]
45+
46+
input.forEach(section => {
47+
const copiedSection = {};
48+
49+
Object.keys(section).forEach(key => {
50+
if (!propertiesToIgnore.includes(key)) {
51+
copiedSection[key] = section[key];
52+
}
53+
})
54+
55+
switch (section.type) {
56+
case 'module':
57+
generatedValue.modules.push(copiedSection);
58+
break;
59+
case 'text':
60+
generatedValue.text.push(copiedSection);
61+
break;
62+
default:
63+
throw new TypeError(`unsupported root section type ${section.type}`);
64+
}
65+
})
66+
67+
if (output) {
68+
// Current directory path relative to the `index.mjs` file
69+
const baseDir = import.meta.dirname;
70+
71+
// Read the contents of the JSON schema
72+
const schemaString = await readFile(
73+
join(baseDir, 'schema.jsonc'),
74+
'utf8'
75+
);
76+
77+
// Parse the JSON schema into an object
78+
const schema = await jsoncParse(schemaString);
79+
80+
// Write the parsed JSON schema to the output directory
81+
// await writeFile(
82+
// join(output, 'node-doc-schema.json'),
83+
// JSON.stringify(schema)
84+
// );
85+
}
86+
87+
return generatedValue;
88+
},
89+
};
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"$id": "nodejs-api-doc-all@v0.0.0",
3+
"$id": "nodejs-api-doc-all@v2.0.0", // This should be kept in sync with the generator version.
44
"title": "Node.js API Documentation Schema (All)",
55
"readOnly": true,
66

77
"properties": {
88
"modules": {
99
"type": "array",
10-
// TODO figure out how to reference the other file here
11-
// "items": { "$ref": "http://json-schema.org/draft-07/schema#" }
12-
"items": { "$ref": "../json/parsed-schema.json" },
10+
"items": { "$ref": "../json/schema.jsonc/#/definitions/Module" },
1311
},
12+
"text": {
13+
"type": "array",
14+
"items": { "$ref": "../json/schema.jsonc/#/definitions/Text" }
15+
}
1416
},
1517
}

src/generators/json/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { writeFile, readFile } from 'node:fs/promises';
55
import { join } from 'node:path';
6-
import { parse } from 'jsonc-parser';
6+
import { parse as jsoncParse } from 'jsonc-parser';
77
import { groupNodesByModule } from '../../utils/generators.mjs';
88
import { createSectionBuilder } from './utils/createSection.mjs';
99

@@ -50,7 +50,7 @@ export default {
5050
const processModuleNodes = head => {
5151
const nodes = groupedModules.get(head.api);
5252
if (!nodes) {
53-
throw new TypeError('TODO');
53+
throw new TypeError(`no grouped nodes found for ${head.api}`);
5454
}
5555

5656
const section = buildSection(head, nodes);
@@ -92,7 +92,7 @@ export default {
9292
);
9393

9494
// Parse the JSON schema into an object
95-
const schema = await parse(schemaString);
95+
const schema = await jsoncParse(schemaString);
9696

9797
// Write the parsed JSON schema to the output directory
9898
// await writeFile(

src/generators/json/utils/createMethodSection.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const createMethodSectionBuilder = () => {
1414
*/
1515
const parseSignatures = (entry, section) => {
1616
section.signatures = [];
17+
18+
1719
};
1820

1921
/**

src/generators/json/utils/createSection.mjs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const createSectionBuilder = () => {
4545
/**
4646
* @type {import('../types.d.ts').Section}
4747
*/
48-
const section = createSectionBase(entry, parent?.type);
48+
const section = (createSectionBase(entry, parent?.type));
4949

5050
section.parent = parent;
5151

@@ -63,23 +63,29 @@ export const createSectionBuilder = () => {
6363
// createPropertySection(entry, parent, section);
6464
break;
6565
case 'text':
66+
if (parent) {
67+
parent.text ??= [];
68+
69+
parent.text.push(section);
70+
}
71+
6672
break;
6773
default:
6874
throw new TypeError(`unhandled section type ${section.type}`);
6975
}
7076

7177
handleChildren(entry, section);
7278

73-
if (parent) {
74-
if (!parent.tmp) {
75-
parent.tmp = [];
76-
}
77-
parent.tmp.push(section);
78-
}
79-
// console.debug(section);
80-
8179
delete section.parent;
8280

81+
// if (parent) {
82+
// if (!parent.tmp) {
83+
// parent.tmp = [];
84+
// }
85+
// parent.tmp.push(section);
86+
// }
87+
// console.debug(section);
88+
8389
return section;
8490
};
8591

src/generators/json/utils/createSectionBase.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const createSectionBaseBuilder = () => {
5858
switch (node.type) {
5959
case 'paragraph': {
6060
addDescriptionAndExamples(section, node.children);
61-
6261
break;
6362
}
6463
case 'emphasis': {

src/generators/json/utils/findParentSection.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
/**
4+
* Finds the closest parent section with the specified type(s).
45
* @param {import('../types.d.ts').Section} section
56
* @param {import('../generated.d.ts').SectionBase['type'] | Array<import('../generated.d.ts').SectionBase['type']>} type
67
* @returns {import('../types.d.ts').Section | undefined}

0 commit comments

Comments
 (0)