|
1
|
import { minimatch } from "minimatch" |
|
2
|
import AbstractGenerator from "./index.js" |
|
3
|
import getBasePath from "../../utils/get-base-path.js" |
|
4
|
import RouteExamplesKindGenerator from "../kinds/route-examples.js" |
|
5
|
import ts from "typescript" |
|
6
|
import type { RouteExamples } from "types" |
|
7
|
import { getRouteExamplesOutputBasePath } from "../../utils/get-output-base-paths.js" |
|
8
|
class RouteExamplesGenerator extends AbstractGenerator { |
|
9
|
protected routeExamplesKindGenerator?: RouteExamplesKindGenerator |
|
10
|
|
|
11
|
async run() { |
|
12
|
this.init() |
|
13
|
|
|
14
|
this.routeExamplesKindGenerator = new RouteExamplesKindGenerator({ |
|
15
|
checker: this.checker!, |
|
16
|
generatorEventManager: this.generatorEventManager, |
|
17
|
}) |
|
18
|
|
|
19
|
let routeExamples: RouteExamples = {} |
|
20
|
|
|
21
|
await Promise.all( |
|
22
|
this.program!.getSourceFiles().map(async (file) => { |
|
23
|
if (file.isDeclarationFile || !this.isFileIncluded(file.fileName)) { |
|
24
|
return |
|
25
|
} |
|
26
|
|
|
27
|
const fileNodes: ts.Node[] = [file] |
|
28
|
|
|
29
|
console.log(`[Route Examples] Generating for ${file.fileName}...`) |
|
30
|
|
|
31
|
// since typescript's compiler API doesn't support |
|
32
|
// async processes, we have to retrieve the nodes first then |
|
33
|
// traverse them separately. |
|
34
|
const pushNodesToArr = (node: ts.Node) => { |
|
35
|
fileNodes.push(node) |
|
36
|
|
|
37
|
ts.forEachChild(node, pushNodesToArr) |
|
38
|
} |
|
39
|
ts.forEachChild(file, pushNodesToArr) |
|
40
|
|
|
41
|
const documentChild = async (node: ts.Node) => { |
|
42
|
if ( |
|
43
|
this.routeExamplesKindGenerator!.isAllowed(node) && |
|
44
|
this.routeExamplesKindGenerator!.canDocumentNode(node) |
|
45
|
) { |
|
46
|
const result = |
|
47
|
await this.routeExamplesKindGenerator!.getDocBlock(node) |
|
48
|
if (!result) { |
|
49
|
return |
|
50
|
} |
|
51
|
|
|
52
|
routeExamples = Object.assign(routeExamples, JSON.parse(result)) |
|
53
|
} |
|
54
|
} |
|
55
|
|
|
56
|
await Promise.all( |
|
57
|
fileNodes.map(async (node) => await documentChild(node)) |
|
58
|
) |
|
59
|
|
|
60
|
if (!this.options.dryRun) { |
|
61
|
this.writeJson(routeExamples) |
|
62
|
} |
|
63
|
}) |
|
64
|
) |
|
65
|
} |
|
66
|
|
|
67
|
/** |
|
68
|
* Checks whether the specified file path is included in the program |
|
69
|
* and is an API file. |
|
70
|
* |
|
71
|
* @param fileName - The file path to check |
|
72
|
* @returns Whether the Route Examples generator can run on this file. |
|
73
|
*/ |
|
74
|
isFileIncluded(fileName: string): boolean { |
|
75
|
console.log(fileName, getBasePath(fileName)) |
|
76
|
return ( |
|
77
|
super.isFileIncluded(fileName) && |
|
78
|
minimatch( |
|
79
|
getBasePath(fileName), |
|
80
|
"packages/core/**/js-sdk/src/@(store|admin|auth)/**", |
|
81
|
{ |
|
82
|
matchBase: true, |
|
83
|
} |
|
84
|
) |
|
85
|
) |
|
86
|
} |
|
87
|
|
|
88
|
/** |
|
89
|
* Writes the route examples to a JSON file. |
|
90
|
* |
|
91
|
* @param routeExamples - The route examples to write. |
|
92
|
*/ |
|
93
|
writeJson(routeExamples: RouteExamples) { |
|
94
|
const filePath = getRouteExamplesOutputBasePath() |
|
95
|
|
|
96
|
const fileContent = JSON.stringify(routeExamples, null, 2) |
|
97
|
|
|
98
|
ts.sys.writeFile(filePath, fileContent) |
|
99
|
} |
|
100
|
} |
|
101
|
|
|
102
|
export default RouteExamplesGenerator |