@canalplus/readme.doc
Version:
Readme's an Extremely Accessible Documentation MakEr
136 lines (135 loc) • 4.9 kB
JavaScript
import { realpathSync } from "fs";
import * as process from "process";
import { pathToFileURL } from "url";
import createDocumentation from "./create_documentation.js";
import log from "./log.js";
import currentReadmeVersion from "./version.js";
if (wasCalledAsScript()) {
const args = processArgs();
function main() {
createDocumentation(args.baseInDir, args.baseOutDir, args.options).catch((err) => {
var _a, _b;
const srcMessage = (_b = ((_a = err) !== null && _a !== void 0 ? _a : {}).message) !== null && _b !== void 0 ? _b : "Unknown error";
log("ERROR", "Failed to generated documentation: " + srcMessage);
process.exit(1);
});
}
main();
}
export default createDocumentation;
function wasCalledAsScript() {
const realPath = realpathSync(process.argv[1]);
const realPathAsUrl = pathToFileURL(realPath).href;
return import.meta.url === realPathAsUrl;
}
function processArgs() {
let inDir;
let outDir;
let version;
let shouldClean = false;
let currentFlag;
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
if (currentFlag === undefined && arg[0] === "-") {
if (arg[1] === "-") {
switch (arg.substring(2).trim()) {
case "version":
log("LOG", `v${currentReadmeVersion}`);
process.exit(0);
case "input":
currentFlag = "input";
break;
case "output":
currentFlag = "output";
break;
case "project-version":
currentFlag = "project-version";
break;
case "clean":
shouldClean = true;
break;
case "help":
displayHelp();
process.exit(0);
default:
log("ERROR", `Unrecognized flag: ${arg}`);
process.exit(1);
}
}
else {
switch (arg.substring(1).trim()) {
case "v":
log("LOG", `v${currentReadmeVersion}`);
process.exit(0);
case "i":
currentFlag = "input";
break;
case "o":
currentFlag = "output";
break;
case "p":
currentFlag = "project-version";
break;
case "c":
shouldClean = true;
break;
case "h":
displayHelp();
process.exit(0);
default:
log("ERROR", `Unrecognized flag: ${arg}`);
process.exit(1);
}
}
}
else if (currentFlag !== undefined) {
switch (currentFlag) {
case "input":
inDir = arg;
break;
case "output":
outDir = arg;
break;
case "project-version":
version = arg;
break;
}
currentFlag = undefined;
}
else {
log("ERROR", `Unexpected token in command: ${arg}`);
process.exit(1);
}
}
if (inDir === undefined || outDir === undefined) {
log("ERROR", "The documentation generator needs at least " +
"the input directory (behind an `-i` flag) and the output directory" +
" (behind an `-o` flag) but at least one of them was missing.");
process.exit(1);
}
return {
baseInDir: inDir,
baseOutDir: outDir,
options: {
version,
clean: shouldClean,
},
};
}
/**
* Display through `console.log` an helping message relative to how to run this
* script.
*/
function displayHelp() {
log("LOG",
/* eslint-disable indent */
`Usage: node readme.doc [options]
Options:
-h, --help Display this help
-v, --version Display the current version of README
-i, --input [Mandatory] Root directory where your documentation source files are.
-o, --output [Mandatory] Destination directory where your HTML documentation will be created.
-c, --clean [Optional, Recommended] Remove output directory if it already exists
-p, --project-version [Optional, Recommended] Indicate your current project's version`);
}