Add a script to generate documentation for config.js based on comments

This commit is contained in:
Jérémie Astori 2017-02-19 01:24:34 -05:00
parent 056a38caeb
commit fd5652ed60
No known key found for this signature in database
GPG key ID: B9A4F245CD67BDE8

View file

@ -0,0 +1,61 @@
"use strict";
// Usage: `node generate-config-doc.js DOC_REPO_PATH`
//
// Example:
//
// ```sh
// node scripts/generate-config-doc.js ../thelounge.github.io/
// ```
const {readFileSync, writeFileSync} = require("fs");
const colors = require("colors/safe");
const log = require("../src/log");
const {join} = require("path");
const {spawnSync} = require("child_process");
function getGitUsername() {
return spawnSync("git", ["config", "user.name"], {encoding: "utf8"})
.stdout
.trim();
}
const configContent = readFileSync(
join(__dirname, "..", "defaults", "config.js"),
"utf8"
);
const docPath = join(process.argv[2], "_includes", "config.js.md");
const extractedDoc = configContent
.split("\n")
.reduce((acc, line) => {
line = line.trim();
if (line.startsWith("// ")) {
acc.push(line.substr(3));
} else if (acc.length > 0 && acc[acc.length - 1] !== "") {
// Treat whitespaces between comment blocks as separators in the generated
// Markdown. Multiple blank lines are treated as one.
acc.push("");
}
return acc;
}, []).join("\n");
const infoBlock = `<!--
DO NOT EDIT THIS FILE MANUALLY.
Content for the following is generated by this script in the main repo:
https://github.com/thelounge/lounge/blob/master/scripts/generate-config-doc.js
Last updated at ${Date()} by ${getGitUsername()}
-->`;
const generatedContent = `${infoBlock}\n\n${extractedDoc}\n${infoBlock}\n`;
writeFileSync(docPath, generatedContent);
log.info(
`${colors.bold(generatedContent.split("\n").length)} lines ` +
`(${colors.bold(generatedContent.length)} characters) ` +
`were written in ${colors.green(docPath)}.`
);