2017-02-19 06:24:34 +00:00
|
|
|
"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");
|
2018-11-27 11:15:01 +00:00
|
|
|
const colors = require("chalk");
|
2017-02-19 06:24:34 +00:00
|
|
|
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");
|
|
|
|
|
2018-10-29 07:54:58 +00:00
|
|
|
const infoBlockHeader = `<!--
|
2017-02-19 06:24:34 +00:00
|
|
|
DO NOT EDIT THIS FILE MANUALLY.
|
|
|
|
Content for the following is generated by this script in the main repo:
|
2018-10-29 07:54:58 +00:00
|
|
|
https://github.com/thelounge/thelounge/blob/master/scripts/generate-config-doc.js`;
|
|
|
|
|
|
|
|
const infoBlockTop = `${infoBlockHeader}
|
|
|
|
Last updated at ${getPrettyDate()} (UTC) by ${getGitUsername()}
|
|
|
|
-->`;
|
|
|
|
|
|
|
|
const infoBlockBottom = `${infoBlockHeader}
|
2017-02-19 06:24:34 +00:00
|
|
|
-->`;
|
|
|
|
|
2018-10-29 07:54:58 +00:00
|
|
|
const generatedContent = `${infoBlockTop}\n\n${extractedDoc}\n${infoBlockBottom}\n`;
|
2017-02-19 06:24:34 +00:00
|
|
|
|
|
|
|
writeFileSync(docPath, generatedContent);
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`${colors.bold(generatedContent.split("\n").length)} lines ` +
|
|
|
|
`(${colors.bold(generatedContent.length)} characters) ` +
|
|
|
|
`were written in ${colors.green(docPath)}.`
|
|
|
|
);
|
2018-10-29 07:54:58 +00:00
|
|
|
|
|
|
|
function getPrettyDate() {
|
|
|
|
return (new Date()).toISOString().split(".")[0].replace("T", " ");
|
|
|
|
}
|