forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
200 lines (183 loc) · 4.6 KB
/
util.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
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
195
196
197
198
199
200
import glob from "fast-glob";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const dir = dirname(fileURLToPath(import.meta.url));
export type Project = {
/**
* The title of the example, from README.md
*/
title: string;
/**
* e.g.: examples/01-basic/01-minimal
*/
pathFromRoot: string;
/**
* e.g.: minimal
*/
projectSlug: string;
/**
* e.g.: basic/minimal
*/
fullSlug: string;
group: {
/**
* e.g.: examples/01-basic
*/
pathFromRoot: string;
/**
* e.g.: basic
*/
slug: string;
};
/**
* contents from .bnexample.json
*/
config: {
playground: boolean;
docs: boolean;
dependencies?: any;
devDependencies?: any;
shortTitle?: string;
author: string;
pro?: boolean;
};
};
export function groupBy<T>(arr: T[], key: (el: T) => string) {
const groups: Record<string, T[]> = {};
arr.forEach((val) => {
const k = key(val);
if (!groups[k]) {
groups[k] = [];
}
groups[k].push(val);
});
return groups;
}
export function groupProjects(projects: Project[]) {
const grouped = groupBy(projects, (p) => p.group.slug);
return Object.fromEntries(
Object.entries(grouped).map(([key, projects]) => {
const group = projects[0].group;
return [
key,
{
...group,
projects,
},
];
})
);
}
export function addTitleToGroups(grouped: ReturnType<typeof groupProjects>) {
// read group titles from /pages/examples/_meta.json
const meta = JSON.parse(
fs.readFileSync(
path.resolve(dir, "../../../docs/pages/examples/_meta.json"),
"utf-8"
)
);
const groupsWithTitles = Object.fromEntries(
Object.entries(grouped).map(([key, group]) => {
const title = meta[key];
if (!title) {
throw new Error(
`Missing group title for ${key}, add to examples/_meta.json?`
);
}
return [
key,
{
...group,
title,
},
];
})
);
return groupsWithTitles;
}
export type Files = Record<
string,
{
filename: string;
code: string;
hidden: boolean;
}
>;
export function getProjectFiles(project: Project): Files {
const dir = path.resolve("../../", project.pathFromRoot);
const files = glob.globSync((dir + "/**/*").replace(/\\/g, '/'), {
ignore: ["**/node_modules/**/*", "**/dist/**/*"],
});
const passedFiles = Object.fromEntries(
files.map((fullPath) => {
const filename = fullPath.substring(dir.length);
return [
filename,
{
filename,
code: fs.readFileSync(fullPath, "utf-8"),
hidden:
!(filename.endsWith(".tsx") || filename.endsWith(".css")) ||
filename.endsWith("main.tsx"),
},
];
})
);
return passedFiles;
}
/**
* Get the list of example Projects based on the /examples folder
*/
export function getExampleProjects(): Project[] {
const examples: Project[] = glob
.globSync(path.join(dir, "../../../examples/**/*/.bnexample.json").replace(/\\/g, '/'))
.map((configPath) => {
const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
const directory = path.dirname(configPath);
const readmePath = path.join(directory, "README.md");
if (!fs.existsSync(readmePath)) {
throw new Error(`Missing README.md for ${directory}`);
}
const md = fs.readFileSync(readmePath, "utf-8");
const title = md.match(/# (.*)/)?.[1];
if (!title?.length) {
throw new Error(`Missing title in README.md for ${directory}`);
}
const [groupDir, exampleDir] = path
.relative(path.resolve("../../examples"), directory)
.split(path.sep);
const group = {
pathFromRoot: path.relative(
path.resolve("../../"),
path.join(directory, "..")
),
// remove optional 01- prefix
slug: groupDir.replace(/^\d{2}-/, ""),
};
const projectSlug = exampleDir.replace(/^\d{2}-/, "");
const project = {
projectSlug,
fullSlug: `${group.slug}/${projectSlug}`,
pathFromRoot: path.relative(path.resolve("../../"), directory),
config,
title,
group,
};
return project;
});
// examples.sort((a, b) => {
// if (a.config?.order && b.config?.order) {
// return a.config.order - b.config.order;
// }
// if (a.config?.order) {
// return -1;
// }
// if (b.config?.order) {
// return 1;
// }
// return 0;
// });
return examples;
}