forked from bitnami/readme-generator-for-helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (58 loc) · 2.44 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
68
69
70
71
/*
* Copyright 2021-2021 VMware, Inc.
* 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(options) {
const valuesFilePath = options.values;
const configPath = options.config ? options.config : `${__dirname}/config.json`;
const config = require(configPath);
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);
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,
};