-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
sourceJoyTemplates.ts
34 lines (32 loc) · 1005 Bytes
/
sourceJoyTemplates.ts
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
const templateMap = new Map<string, { files: Record<string, string>; codeVariant: 'TS' | 'JS' }>();
// @ts-ignore
const req = require.context(
'../../../data/joy/getting-started/templates/?raw',
true,
/^\.\/[^/]+\/.*\.(js|tsx|ts)$/,
);
req.keys().forEach((key: string) => {
const match = /\/(?<name>[^/]+)\/(?<filePath>.*)/.exec(key);
if (match) {
const name = match.groups?.name;
const filePath = match.groups?.filePath;
if (name && filePath) {
const { files, codeVariant } = templateMap.get(name) || {};
templateMap.set(name, {
files: { ...files, [filePath]: req(key) },
codeVariant: filePath.match(/\.(ts|tsx)$/) ? 'TS' : codeVariant || 'JS',
});
}
}
});
export interface TemplateData {
files: Record<string, string>;
codeVariant: 'TS' | 'JS';
}
export default function sourceJoyTemplates() {
return {
names: Array.from(templateMap.keys()),
templates: Array.from(templateMap.values()),
map: new Map(templateMap),
};
}