UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

130 lines (126 loc) 5.32 kB
import { __name } from './chunk-IRPJW6HH.js'; import { findFileName } from '@stryke/path/file-path-fns'; import { joinPaths } from '@stryke/path/join-paths'; import { writeFile } from 'node:fs/promises'; import { sep } from 'node:path'; import { Application, TypeDocReader, PackageJsonReader, TSConfigReader, Converter, PageEvent, ReflectionKind } from 'typedoc'; var objectToFrontmatter = /* @__PURE__ */ __name((object = {}) => Object.entries(object).filter(([, value]) => { return value !== void 0 && value !== null && value !== "" && typeof value === "string" || Array.isArray(value) && value.length > 0; }).map(([key, value]) => `${key}: ${value}`).join("\n"), "objectToFrontmatter"); var onRendererPageEnd = /* @__PURE__ */ __name((frontmatterObject) => (event) => { if (!event.contents) { return; } else if (/README\.md$/.test(event.url)) { event.preventDefault(); return; } const frontmatter = `--- title: '${event.model.name}' ${objectToFrontmatter(frontmatterObject)} --- `; event.contents = frontmatter + event.contents; }, "onRendererPageEnd"); var buildNavigationFromProjectReflection = /* @__PURE__ */ __name((baseUrl = "", project) => { const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/gm, ""); const result = { type: "flat" }; const isGroupOfModules = /* @__PURE__ */ __name((group) => group.title === "Modules", "isGroupOfModules"); const reflectionToNavItem = /* @__PURE__ */ __name((reflection) => { return { title: reflection.name, url: `${baseUrlWithoutTrailingSlash}/${reflection.url}`.replace(/\.md$/, "") }; }, "reflectionToNavItem"); const modulesGroupToNavigationGroup = /* @__PURE__ */ __name((module) => ({ items: (module.groups ?? []).flatMap((group) => group.children.map(reflectionToNavItem)), name: module.name }), "modulesGroupToNavigationGroup"); const navFromReflectionGroups = /* @__PURE__ */ __name((groups, nav = { type: "flat" }) => { groups.forEach((group) => { if (isGroupOfModules(group)) { nav.type = "modular"; nav.modules = group.children.map(modulesGroupToNavigationGroup); } else { nav.items = nav?.items?.length ? nav.items : []; nav.items = nav.items.concat(group.children.flatMap(reflectionToNavItem)); } }); return nav; }, "navFromReflectionGroups"); return navFromReflectionGroups(project.groups, result); }, "buildNavigationFromProjectReflection"); var onDeclaration = /* @__PURE__ */ __name((entryPoints = []) => (context, reflection) => { if (reflection.kind === ReflectionKind.Module) { const matchingEntryPoint = entryPoints.find((entryPoint) => entryPoint.path === reflection.sources[0].fullFileName); reflection.name = matchingEntryPoint?.name ?? reflection.name; } }, "onDeclaration"); var typedocConfig = { excludeExternals: true, excludeInternal: true, excludePrivate: true, excludeProtected: true, githubPages: false }; var markdownPluginConfig = { hideBreadcrumbs: true, hideInPageTOC: true, hidePageHeader: true, hidePageTitle: true }; var removeTrailingSlash = /* @__PURE__ */ __name((pathString = "") => pathString.endsWith(sep) ? pathString.slice(0, pathString.length - 1) : pathString, "removeTrailingSlash"); var initTypedoc = /* @__PURE__ */ __name(async (context, options) => { const { baseUrl = "/docs/", outputPath } = options; const entryPoints = options.entryPoints ?? context.entry.map((entry) => ({ name: entry.name || entry.output || findFileName(entry.file, { withExtension: false }), path: joinPaths(context.options.projectRoot, entry.file) })); const app = await Application.bootstrapWithPlugins({ ...typedocConfig, ...markdownPluginConfig, gitRevision: context.options.workspaceConfig.branch || "main", tsconfig: context.tsconfig.tsconfigFilePath, exclude: context.tsconfig.tsconfigJson.exclude?.filter(Boolean), out: outputPath, basePath: baseUrl, entryPoints: entryPoints?.map((e) => e.path), plugin: [ "typedoc-plugin-markdown", "@storm-stack/core/lib/typedoc/plugin" ], theme: "storm-stack", readme: "none", excludePrivate: true, hideGenerator: true }, [ new TypeDocReader(), new PackageJsonReader(), new TSConfigReader() ]); app.options.addReader(new TSConfigReader()); app.converter.on(Converter.EVENT_CREATE_DECLARATION, onDeclaration(entryPoints)); const getReflections = /* @__PURE__ */ __name(async () => app.convert(), "getReflections"); const generateDocs = /* @__PURE__ */ __name(async (opts) => { const { outputPath: outputFolder, project, frontmatter } = opts; app.renderer.on(PageEvent.END, onRendererPageEnd(frontmatter)); await app.generateDocs(project, outputFolder || outputPath); }, "generateDocs"); const generateNavigationJSON = /* @__PURE__ */ __name(async (project, outputFolder = outputPath) => { const navigation = buildNavigationFromProjectReflection(baseUrl, project); await writeFile(`${removeTrailingSlash(outputFolder)}/nav.json`, JSON.stringify(navigation)); }, "generateNavigationJSON"); return { app, generateDocs, generateNavigationJSON, getReflections }; }, "initTypedoc"); var init_default = initTypedoc; export { initTypedoc, init_default };