UNPKG

tsplot

Version:

A CLI and tooling library to plot Typescript project information to different template targets.

82 lines 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.render = exports.setupRenderCommand = void 0; const tslib_1 = require("tslib"); const commander_1 = require("commander"); const consola_1 = require("consola"); const lib_1 = require("../../lib"); const utils_1 = require("../../lib/utils"); const utils_2 = require("../utils"); const GROUP_BY_TS_PATHS = 'tsPaths'; /** @internal */ function setupRenderCommand(program) { return (0, utils_2.setupSharedOptions)(program .command('render') .description('renders typescript AST and type checker information to a desired target ' + 'format using built-in and custom templates (e.g. plant-uml, mermaid)') .argument('<template>', 'the template name that shall be rendered as output (e.g. class-diagram)') .option('--baseDir <path>', 'custom base directory to use for template file resolution') .option('--groupBy <namespace:glob...>', 'grouping specification to use for namespacing members. Can be a list of ' + 'custom specifiers in the format `namespace:glob` or `namespace:glob,glob` or. ' + "the keyword `tsPaths` to use the typescript paths definitions. Won't group by default") .addOption(new commander_1.Option('-t, --target <name>', 'the rendering output target. Can be a custom one or one of the built-in ' + `targets (${Object.values(lib_1.KnownTarget).join(', ')})`).default(lib_1.KnownTarget.PlantUML))).action(render); } exports.setupRenderCommand = setupRenderCommand; function getPaths(options) { let { groupBy } = options; const useTsPaths = groupBy === null || groupBy === void 0 ? void 0 : groupBy.includes(GROUP_BY_TS_PATHS); if (useTsPaths) { groupBy = groupBy === null || groupBy === void 0 ? void 0 : groupBy.filter((str) => str !== GROUP_BY_TS_PATHS); } let paths = (groupBy === null || groupBy === void 0 ? void 0 : groupBy.length) ? (0, utils_2.getPathsFromKeyValueListPairs)(groupBy.map(utils_2.parseKeyValueListPair)) : {}; if (useTsPaths) { paths = Object.assign(Object.assign({}, (0, utils_1.getParsedCommandLine)(options.project).options.paths), paths); } return paths; } function render(template, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { (0, utils_2.setupConsola)(options); (0, utils_2.logSharedOptions)(options); const view = (0, utils_2.getProjectView)(Object.assign(Object.assign({}, options), { paths: getPaths(options) })); const renderView = (projectView) => { const output = (0, lib_1.render)(template, { baseDir: options.baseDir, context: { projectView }, target: options.target, }); if (options.target === lib_1.KnownTarget.Mermaid) { (0, utils_2.logSizeWarningIfExceeding)({ output, edges: lib_1.ProjectGraph.fromView(projectView, { keepFilter: true }).edges .length, maxOutputSize: 50000, maxEdges: 500, description: 'These are defaults set by Mermaid. To allow rendering of larger diagrams ' + 'you can adjust the configuration (see https://mermaid.js.org/config/schema-docs/config.html)', }); } return output; }; if (options.debug) { const s = yield (0, utils_2.collectStats)(Object.assign(Object.assign({}, options), { silent: true })); consola_1.consola.debug('stats:', JSON.stringify(s, null, 2)); } if (!options.split || !(options === null || options === void 0 ? void 0 : options.from)) { const confinedView = yield (0, utils_2.getConfinedProjectViewFromMemberOrDefault)(view, options); yield (0, utils_2.output)(renderView(confinedView), options); } else { const viewBatch = yield Promise.all(options.from.map((m) => (0, utils_2.getConfinedProjectViewFromMemberOrDefault)(view, Object.assign(Object.assign({}, options), { from: [m] })))); yield Promise.all(viewBatch.map((v, index) => (0, utils_2.output)(renderView(v), Object.assign(Object.assign({}, options), { output: (0, utils_2.interpolateOutputPath)(options.output, { memberName: options.from[index], index, }) })))); } }); } exports.render = render; //# sourceMappingURL=render.js.map