UNPKG

autouml

Version:

Autogenerate UML diagrams using d2

165 lines (164 loc) 5.7 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_UML_OPTIONS = exports.createOptionsFromCLI = exports.generateUsageMessage = exports.PARSE_ARGS_CONFIG = void 0; /** * Main entry into the program */ var util = __importStar(require("node:util")); var buildUML_1 = require("./buildUML"); /** * Valid command line arguments. This object details all flags and their uses, and the program overall usage statement */ var PARSE_ARGS_CONFIG = { usage: "usage: autouml [OPTION]...", description: "Generate a UML diagram for a Typescript project.", options: { help: { type: "boolean", short: "h", description: "display this help and exit", }, verbose: { type: "boolean", short: "v", description: "print a message for each major step", }, baseDir: { type: "string", short: "d", description: "set the base directory for all operations", }, tsconfigFileName: { type: "string", short: "c", description: "set the name of the tsconfig file to search for", }, outPath: { type: "string", short: "o", description: "set the output file", }, debugASTPath: { type: "string", short: "a", description: "set the output file for the produced AST", }, includeNodeModules: { type: "boolean", short: "n", description: "include references to types in node modules", }, }, }; exports.PARSE_ARGS_CONFIG = PARSE_ARGS_CONFIG; /** * Generate the usage statement from a given config object. * @param config A valid IParseArgsConfig object * @returns A well formatted usage statement to print to console */ function generateUsageMessage(config) { var usage = [config.usage, config.description]; var maxLength = 0; var descs = []; if (config.options) { for (var _i = 0, _a = Object.entries(config.options); _i < _a.length; _i++) { var _b = _a[_i], long = _b[0], op = _b[1]; var optionText = " --".concat(long, ", -").concat(op.short); var typeText = op.type === "boolean" ? "" : "<".concat(op.type, ">"); var t = "".concat(optionText, " ").concat(typeText); maxLength = Math.max(maxLength, t.length); usage.push(t); descs.push(op.description); } for (var i = 2; i < usage.length; i++) { usage[i] += new Array(maxLength - usage[i].length) .fill(" ") .join(""); usage[i] += " ".concat(descs[i - 2]); } } return usage.join("\n"); } exports.generateUsageMessage = generateUsageMessage; var DEFAULT_UML_OPTIONS = { baseDir: "./", tsconfigFileName: "tsconfig.json", outPath: "./uml.d2", target: 0 /* autouml.codegen.Target.d2 */, verbose: false, debugASTPath: "", includeNodeModules: false, }; exports.DEFAULT_UML_OPTIONS = DEFAULT_UML_OPTIONS; /** * Create the options object based on command line arguments. This is expected to only be called from direct invocation of this script. * @returns an options object, or null if parsing failed or help was invoked */ function createOptionsFromCLI() { // parse the command line arguments var commandLineArgs; try { commandLineArgs = util.parseArgs(PARSE_ARGS_CONFIG); } catch (e) { throw new Error("".concat(e, "\n").concat(generateUsageMessage(PARSE_ARGS_CONFIG))); } var flags = commandLineArgs.values; if (flags.help) { console.log(generateUsageMessage(PARSE_ARGS_CONFIG)); return null; } var options = structuredClone(DEFAULT_UML_OPTIONS); // TODO: change options object based on command line inputs if (flags.verbose) { options.verbose = true; } if (flags.includeNodeModules) { options.includeNodeModules = true; } if (flags.baseDir) { options.baseDir = flags.baseDir; } if (flags.tsconfigFileName) { options.tsconfigFileName = flags.tsconfigFileName; } if (flags.outDir) { options.outPath = flags.outPath; } if (flags.debugASTPath) { options.debugASTPath = flags.debugASTPath; } return options; } exports.createOptionsFromCLI = createOptionsFromCLI; if (require.main === module) { var options = createOptionsFromCLI(); if (options) { (0, buildUML_1.buildUML)(options); } }