|
| 1 | +import { Application } from 'typedoc'; |
| 2 | +import { JsonObject, PackageJson } from 'type-fest'; |
| 3 | +import { PageEvent, RendererEvent } from 'typedoc/dist/lib/output/events'; |
| 4 | +import { resolve } from 'path'; |
| 5 | +import { writeJson, readJsonSync } from 'fs-extra'; |
| 6 | +import { Reflection, ReflectionGroup } from 'typedoc/dist/lib/models'; |
| 7 | + |
| 8 | +const request = require('sync-request'); |
| 9 | + |
| 10 | + |
| 11 | +const pkg: PackageJson = readJsonSync(resolve(process.cwd(), './package.json')); |
| 12 | +const fileName = 'version.map.json'; |
| 13 | +const versionMapUrl = `https://unity-template.github.io/onex-utils/${fileName}`; |
| 14 | + |
| 15 | +let versionMap: JsonObject = {}; |
| 16 | + |
| 17 | +export const load = (that: Application) => { |
| 18 | + that.listenTo(that.application.renderer, { |
| 19 | + [RendererEvent.BEGIN]: getVersionMap, |
| 20 | + [RendererEvent.END]: saveVersionMap, |
| 21 | + [PageEvent.BEGIN]: updateVersionMap, |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * 获取官网中对应的json |
| 27 | + */ |
| 28 | +const getVersionMap = () => { |
| 29 | + try { |
| 30 | + const result = request('GET', versionMapUrl); |
| 31 | + versionMap = JSON.parse(result.body.toString()); |
| 32 | + } catch (err) { |
| 33 | + console.log('获取远程versionMap报错: ', err); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * 将生成的json进行保存 |
| 39 | + */ |
| 40 | +const saveVersionMap = (event: RendererEvent) => { |
| 41 | + writeJson(resolve(event.outputDirectory, fileName), versionMap); |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * 依据 versionMap 更新 version map 相关字段 |
| 46 | + */ |
| 47 | +const updateVersionMap = (page: PageEvent) => { |
| 48 | + page?.model?.groups.forEach((group: ReflectionGroup) => { |
| 49 | + if (!group.categories) { |
| 50 | + group.children.forEach((reflect: Reflection) => { |
| 51 | + if ((reflect.parent as any)?.version) { |
| 52 | + const parentAlias = reflect.parent?.getAlias() ?? ''; |
| 53 | + const currentAlias = reflect.getAlias(); |
| 54 | + const key = `${parentAlias}_${currentAlias}`; |
| 55 | + versionMap[key] ??= pkg.version; |
| 56 | + (reflect as any).version = versionMap[key]; |
| 57 | + return; |
| 58 | + } |
| 59 | + versionMap[reflect.name] ??= pkg.version; |
| 60 | + (reflect as any).version = versionMap[reflect.name]; |
| 61 | + }); |
| 62 | + } |
| 63 | + }); |
| 64 | +}; |
0 commit comments