-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.mjs
More file actions
78 lines (63 loc) · 2.04 KB
/
index.mjs
File metadata and controls
78 lines (63 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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];
}
});
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}`);
}
});
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;
},
};