-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
67 lines (55 loc) · 2.32 KB
/
index.js
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
/*
* Copyright Broadcom, Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
const fs = require('fs');
const pjson = require('./package.json');
const { createValuesObject, parseMetadataComments } = require('./lib/parser');
const { checkKeys } = require('./lib/checker');
const { combineMetadataAndValues, buildParamsToRenderList } = require('./lib/builder');
const { insertReadmeTable, renderOpenAPISchema } = require('./lib/render');
function getParsedMetadata(valuesFilePath, config) {
const valuesObject = createValuesObject(valuesFilePath);
const valuesMetadata = parseMetadataComments(valuesFilePath, config);
// Check the parsed keys are consistent with the real ones
checkKeys(valuesObject, valuesMetadata.parameters);
// Combine after the check
// valuesMetadata is modified and filled with more info
combineMetadataAndValues(valuesObject, valuesMetadata.parameters);
return valuesMetadata;
}
function runReadmeGenerator(options) {
const valuesFilePath = options.values;
const readmeFilePath = options.readme;
const schemaFilePath = options.schema;
const versionFlag = options.version;
if (versionFlag) {
console.log('Version:', pjson.version); // eslint-disable-line no-console
} else {
if (!readmeFilePath && !schemaFilePath) {
throw new Error('Nothing to do. Please provide the --readme or --schema options.');
}
if (!valuesFilePath) {
throw new Error('Nothing to do. You must provide the --values option');
}
const configPath = options.config ? options.config : `${__dirname}/config.json`;
const config = JSON.parse(fs.readFileSync(configPath));
const parsedMetadata = getParsedMetadata(options.values, config);
if (readmeFilePath) {
/* eslint no-param-reassign: ["error", { "props": false }] */
parsedMetadata.sections.forEach((section) => {
section.parameters = buildParamsToRenderList(section.parameters, config);
});
insertReadmeTable(readmeFilePath, parsedMetadata.sections, config);
}
if (schemaFilePath) {
parsedMetadata.parameters = buildParamsToRenderList(parsedMetadata.parameters, config);
renderOpenAPISchema(schemaFilePath, parsedMetadata.parameters, config);
}
}
}
module.exports = {
runReadmeGenerator,
};