From fd5652ed609216250ead8cf0254cfbac1846083b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sun, 19 Feb 2017 01:24:34 -0500 Subject: [PATCH] Add a script to generate documentation for config.js based on comments --- scripts/generate-config-doc.js | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/generate-config-doc.js diff --git a/scripts/generate-config-doc.js b/scripts/generate-config-doc.js new file mode 100644 index 00000000..5a6bd411 --- /dev/null +++ b/scripts/generate-config-doc.js @@ -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 = ``; + +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)}.` +);