Skip to content

Commit 4748a0e

Browse files
authored
Merge pull request #25 from unity-template/Genluo/issue15
feat: 函数自动判断版本支持(fix: #15)
2 parents a3cca5d + 2664281 commit 4748a0e

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"name": "lg208143"
4646
},
4747
"dependencies": {
48-
"axios": "^0.21.1",
4948
"dayjs": "^1.9.6",
5049
"lodash.chunk": "^4.2.0",
5150
"lodash.debounce": "^4.0.8",
@@ -108,6 +107,7 @@
108107
"sass-loader": "^10.1.0",
109108
"sinon": "^9.2.1",
110109
"stats-filelist": "^1.0.1",
110+
"sync-request": "^6.1.0",
111111
"ts-jest": "^26.4.4",
112112
"ts-loader": "^8.0.11",
113113
"ts-node": "^9.1.1",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'promise-synchronizer';

script/theme/src/assets/css/elements/_member.sass

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@
3434

3535
a[data-tsd-kind="Private"]
3636
color: var(--color-ts-private)
37+
.tsd-version
38+
margin-bottom: 10px
39+
span
40+
padding: 5px 10px
41+
border-radius: 4px
42+
background: rgb(19, 82, 0)
43+
color: rgb(228, 247, 210)
44+
opacity: 1
45+
font-size: 14px

script/theme/src/partials/member.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<h3>{{#each flags}}<span class="tsd-flag ts-flag{{this}}">{{this}}</span> {{/each}}{{{wbr name}}}</h3>
55
{{/if}}
66

7+
{{#if version}}
8+
<div class="tsd-version"><span>{{version}}</span> provide</div>
9+
{{/if}}
10+
711
{{#if signatures}}
812
{{> member.signatures}}
913
{{else}}{{#if hasGetterOrSetter}}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"plugin": [
3232
"typedoc-plugin-nojekyll",
3333
"./script/plugin/build/packages/alias/index.js",
34-
"./script/plugin/build/packages/categories/index.js"
34+
"./script/plugin/build/packages/categories/index.js",
35+
"./script/plugin/build/packages/version/index.js"
3536
],
3637
"out": "docs",
3738
"entryPoints": ["./src"],

0 commit comments

Comments
 (0)