-
Notifications
You must be signed in to change notification settings - Fork 271
/
model.tsx
194 lines (179 loc) · 4.93 KB
/
model.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type { ReferenceType, ReflectionType, Type } from 'typedoc';
import { getModule, ReflectionKind } from '../../typedoc-model';
const BlueprintsApi = getModule('@wp-playground/blueprints');
export const BlueprintSteps = BlueprintsApi.children
.filter((entry) => entry.name.match(/Step$/))
.filter(
(entry) => !['CompiledStep', 'GenericStep', 'Step'].includes(entry.name)
)
.filter((entry) => !entry?.flags?.isPrivate)
.map((entry) => entry.name)
.sort();
const parsedSteps = {};
export function getStepAPI(name) {
if (!(name in parsedSteps)) {
const stepDetails = getBlueprintStepDetails(name);
const fnDetails = getBlueprintFunctionDetails(stepDetails);
const stepExample =
stepDetails?.examples?.[0]?.inBlueprint ||
stepDetails.examples?.[0]?.raw;
const fnExample = fnDetails?.examples?.[0];
const stepId = stepDetails.props?.find((prop) => prop.name === 'step')
?.type?.value;
parsedSteps[name] = {
stepId,
stepDetails,
fnDetails,
stepExample,
fnExample,
};
}
return JSON.parse(JSON.stringify(parsedSteps[name]));
}
function getBlueprintFunctionDetails(StepDetails: BlueprintStepDetails) {
// Parse function
const FunctionDefinition = BlueprintsApi.children?.find(
(entry) =>
entry.kind === ReflectionKind.Function &&
(entry.signatures?.[0]?.parameters?.[0]?.type as ReferenceType)
?.name === 'UniversalPHP' &&
(
(entry.signatures?.[0]?.parameters?.[1]?.type as ReferenceType)
?.typeArguments?.[0] as ReferenceType
)?.name === StepDetails.name
);
if (!FunctionDefinition) {
throw new Error(`Blueprint function ${StepDetails.name} not found`);
return {};
}
const Signature = FunctionDefinition?.signatures?.[0];
const examples = StepDetails?.examples
?.filter((entry) => entry.json)
?.map((entry) => entry.json)
?.map(({ ...exampleJson }) => {
const { clone, placeholder } =
replaceResourcesWithPlaceholders(exampleJson);
const formatted = JSON.stringify(clone, null, 4)
.split('\n')
.map((line) => ` ${line}`)
.join('\n')
.replace(`${placeholder}`, 'await fetchMyFile()');
return `import { ${FunctionDefinition.name} } from '@wp-playground/blueprints';
${FunctionDefinition.name}(
playground,
${formatted},
progress
)
`;
});
return {
name: FunctionDefinition.name,
description: Signature.comment?.summary?.[0]?.text || '',
parameters: Signature.parameters.map((entry) => ({
name: entry.name,
type: entry.type,
description: entry.comment?.summary?.[0]?.text || '',
})),
examples,
};
}
function replaceResourcesWithPlaceholders(json) {
const placeholder = 987123874;
const clone = JSON.parse(JSON.stringify(json));
for (const key in clone) {
if (typeof clone[key] === 'object' && 'resource' in clone[key]) {
clone[key] = placeholder;
}
}
return { clone, placeholder };
}
interface BlueprintStepDetails {
name: string;
summary: Array<{ kind: string; text: string }>;
examples: Array<{
raw: string;
json?: Record<string, any> & { step: string };
inBlueprint?: string;
}>;
props: Array<{
name: string;
type: any;
description: string;
}>;
needsLogin?: boolean;
hasRunnableExample: boolean;
}
function getBlueprintStepDetails(name: string): BlueprintStepDetails {
// Parse step
const StepDefinitions = BlueprintsApi.children.filter((entry) =>
entry.name.match(/Step$/)
);
const Step = StepDefinitions.find((entry) => entry.name === name);
const summary = Step?.comment?.summary?.filter((part) => part);
const children =
Step?.children ||
((Step as any as Type)?.type as any as ReflectionType)?.declaration
?.children;
const props = children?.map((entry) => ({
name: entry.name,
type: entry.type,
description: entry.comment?.summary?.[0]?.text || '',
}));
const hasRunnableExample = !!Step?.comment?.blockTags?.find(
(tag) => tag.tag === '@hasRunnableExample'
);
return {
name,
summary,
examples: parseExamples(Step),
hasRunnableExample,
props,
};
}
function parseExamples(Step) {
const needsLogin = !!Step?.comment?.blockTags?.find(
(tag) => tag.tag === '@needsLogin'
);
const landingPage = Step?.comment?.blockTags?.find(
(tag) => tag.tag === '@landingPage'
)?.content?.[0]?.text;
const examples = Step?.comment?.blockTags
?.filter((tag) => tag.tag === '@example')
.map((tag) => tag.content?.[0]?.text)
.filter(Boolean)
.map((example) =>
example
.trim()
.replace(/^```ts/g, '')
.replace(/```$/g, '')
.trim()
.replace(/^<code>/g, '')
.replace(/<\/code>$/g, '')
.trim()
)
.map((example) => {
try {
const parsed = JSON.parse(example);
return {
raw: example,
json: parsed,
inBlueprint: JSON.stringify(
{
landingPage: landingPage || undefined,
steps: [
needsLogin ? { step: 'login' } : null,
parsed,
].filter(Boolean),
},
null,
4
),
};
} catch (e) {
console.log(example);
console.error(e);
return { raw: example };
}
});
return examples;
}