|
| 1 | +const _fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const TypeDoc = require('typedoc') |
| 4 | +const { PageEvent } = TypeDoc |
| 5 | +const { |
| 6 | + prependYAML, |
| 7 | +} = require('typedoc-plugin-markdown/dist/utils/front-matter') |
| 8 | + |
| 9 | +const fs = _fs.promises |
| 10 | + |
| 11 | +const DEFAULT_OPTIONS = { |
| 12 | + // disableOutputCheck: true, |
| 13 | + cleanOutputDir: true, |
| 14 | + excludeInternal: true, |
| 15 | + readme: 'none', |
| 16 | + out: path.resolve(__dirname, '../docs/api'), |
| 17 | + entryDocument: 'index.md', |
| 18 | + hideBreadcrumbs: false, |
| 19 | + hideInPageTOC: true, |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * |
| 24 | + * @param {Partial<import('typedoc').TypeDocOptions>} config |
| 25 | + */ |
| 26 | +exports.createTypeDocApp = function createTypeDocApp(config = {}) { |
| 27 | + const options = { |
| 28 | + ...DEFAULT_OPTIONS, |
| 29 | + ...config, |
| 30 | + } |
| 31 | + |
| 32 | + const app = new TypeDoc.Application() |
| 33 | + |
| 34 | + // If you want TypeDoc to load tsconfig.json / typedoc.json files |
| 35 | + app.options.addReader(new TypeDoc.TSConfigReader()) |
| 36 | + // app.options.addReader(new TypeDoc.TypeDocReader()) |
| 37 | + |
| 38 | + /** @type {'build' | 'serve'} */ |
| 39 | + let targetMode = 'build' |
| 40 | + |
| 41 | + app.renderer.on( |
| 42 | + PageEvent.END, |
| 43 | + /** |
| 44 | + * |
| 45 | + * @param {import('typedoc/dist/lib/output/events').PageEvent} page |
| 46 | + */ |
| 47 | + (page) => { |
| 48 | + if (page.url !== 'index.md' && page.contents) { |
| 49 | + page.contents = prependYAML(page.contents, { |
| 50 | + sidebar: 'auto', |
| 51 | + // TODO: figure out a way to point to the source files? |
| 52 | + editLinks: false, |
| 53 | + sidebarDepth: 3, |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + async function serve() { |
| 60 | + app.bootstrap(options) |
| 61 | + app.convertAndWatch(handleProject) |
| 62 | + } |
| 63 | + |
| 64 | + async function build() { |
| 65 | + if ( |
| 66 | + (await exists(options.out)) && |
| 67 | + (await fs.stat(options.out)).isDirectory() |
| 68 | + ) { |
| 69 | + await fs.rm(options.out, { recursive: true }) |
| 70 | + } |
| 71 | + app.bootstrap(options) |
| 72 | + const project = app.convert() |
| 73 | + return handleProject(project) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * |
| 78 | + * @param {import('typedoc').ProjectReflection} project |
| 79 | + */ |
| 80 | + async function handleProject(project) { |
| 81 | + if (project) { |
| 82 | + // Rendered docs |
| 83 | + try { |
| 84 | + await app.generateDocs(project, options.out) |
| 85 | + app.logger.info(`generated at ${options.out}.`) |
| 86 | + } catch (error) { |
| 87 | + app.logger.error(error) |
| 88 | + } |
| 89 | + } else { |
| 90 | + app.logger.error('No project') |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + build, |
| 96 | + serve, |
| 97 | + /** |
| 98 | + * |
| 99 | + * @param {'build' | 'serve'} command |
| 100 | + */ |
| 101 | + setTargetMode(command) { |
| 102 | + targetMode = command |
| 103 | + }, |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +async function exists(path) { |
| 108 | + try { |
| 109 | + await fs.access(path) |
| 110 | + return true |
| 111 | + } catch { |
| 112 | + return false |
| 113 | + } |
| 114 | +} |
0 commit comments