|
| 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 | +}; |
0 commit comments