forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenDocs.ts
More file actions
215 lines (188 loc) · 6.56 KB
/
genDocs.ts
File metadata and controls
215 lines (188 loc) · 6.56 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import {
addTitleToGroups,
getExampleProjects,
getProjectFiles,
groupProjects,
Project,
} from "./util.js";
/*
`genDocs` generates the nextjs example blocks for the website docs.
Note that these files are not checked in to the repo, so this command should always be run before running / building the site
*/
const dir = path.parse(fileURLToPath(import.meta.url)).dir;
const DOCS_DIR = path.resolve(dir, "../../../docs/");
const COMPONENT_DIR = path.resolve(DOCS_DIR, "./components/example/generated/");
const EXAMPLES_PAGES_DIR = path.resolve(DOCS_DIR, "./content/examples/");
/**
* Generates the <ExampleBlock> component that has all the source code of the example
* This block can be used both in the /docs and in the /example page
*/
async function generateCodeForExample(project: Project) {
const projectFiles = getProjectFiles(project);
const componentTarget = path.join(
COMPONENT_DIR,
"components",
project.fullSlug,
);
const indexFile = path.join(componentTarget, "index.tsx");
fs.rmSync(componentTarget, { recursive: true, force: true });
fs.mkdirSync(componentTarget, { recursive: true });
fs.writeFileSync(
indexFile,
`"use client";
import Component from "./App";
export default Component;`,
);
projectFiles.forEach(({ filename, code }) => {
const target = path.join(componentTarget, filename);
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(target, code);
});
}
const templatePageForExample = (project: Project) => `---
title: ${project.title}
author: ${project.config.author}
---
${project.readme}
`;
/**
* Generate the page for the example in /examples overview
*
* Consists of the contents of the readme
*/
async function generatePageForExample(project: Project) {
const code = templatePageForExample(project);
const target = path.join(EXAMPLES_PAGES_DIR, project.fullSlug + ".mdx");
fs.writeFileSync(target, code);
}
/**
* generates meta.json file for each example group, so that order is preserved
*/
async function generateMetaForExampleGroup(group: {
title: string;
slug: string;
projects: Project[];
}) {
if (!fs.existsSync(path.join(EXAMPLES_PAGES_DIR, group.slug))) {
fs.mkdirSync(path.join(EXAMPLES_PAGES_DIR, group.slug));
}
const target = path.join(EXAMPLES_PAGES_DIR, group.slug, "meta.json");
const meta = {
title: group.title,
pages: group.projects.map((project) => project.projectSlug),
};
const code = JSON.stringify(meta, undefined, 2);
fs.writeFileSync(target, code);
}
/**
* Generates the exampleGroups.gen.ts file, which contains all the necessary
* data about the examples & their groups for the components we use in the
* docs. E.g. the interactive demos and example cards.
*/
async function generateExampleGroupsData(projects: Project[]) {
const target = path.join(COMPONENT_DIR, "exampleGroupsData.gen.ts");
const groups = addTitleToGroups(groupProjects(projects));
const exampleGroupsData = Object.values(groups).map((group) => ({
exampleGroupName: group.slug,
title: group.title,
pathFromRoot: group.pathFromRoot,
examplesData: group.projects.map((project) => ({
exampleName: project.projectSlug,
exampleGroupName: group.slug,
title: project.title,
author: project.config.author,
isPro: project.config.pro || false,
inTailwindApp: project.config.tailwind || false,
showStackBlitzLink: project.config.stackBlitz ?? true,
pathFromRoot: project.pathFromRoot,
files: Object.fromEntries(
getProjectFiles(project).map((file) => [
file.filename.substring(1), // remove leading slash
file.code,
]),
),
})),
}));
const code = `// generated by dev-scripts/examples/genDocs.ts
export type ExampleGroupsData = {
exampleGroupName: string;
title: string;
pathFromRoot: string;
examplesData: {
exampleName: string;
exampleGroupName: string;
title: string;
author: string;
isPro: boolean;
inTailwindApp: boolean;
showStackBlitzLink: boolean;
pathFromRoot: string;
files: Record<string, string>;
}[];
}[];
export type ExampleData = ExampleGroupsData[number]["examplesData"][number];
export const exampleGroupsData: ExampleGroupsData = ${JSON.stringify(exampleGroupsData, undefined, 2)};
`;
fs.writeFileSync(target, code);
}
async function addDependenciesToExample(project: Project) {
const dependencies = project.config.dependencies || {};
const devDependencies = project.config.devDependencies || {};
if (
Object.keys(dependencies).length > 0 ||
Object.keys(devDependencies).length > 0
) {
const packageJson = path.join(DOCS_DIR, "package.json");
const packageJsonContent = fs.readFileSync(packageJson, "utf-8");
const packageJsonObject = JSON.parse(packageJsonContent);
packageJsonObject.dependencies = {
...packageJsonObject.dependencies,
...dependencies,
};
packageJsonObject.devDependencies = {
...packageJsonObject.devDependencies,
...devDependencies,
};
// Change all instances of `@blocknote/*` to `workspace:*`, since we want packages to be installed from the workspace
Object.entries(packageJsonObject.dependencies).forEach(([key, value]) => {
if (key.startsWith("@blocknote/")) {
packageJsonObject.dependencies[key] = "workspace:*";
}
});
Object.entries(packageJsonObject.devDependencies).forEach(
([key, value]) => {
if (key.startsWith("@blocknote/")) {
packageJsonObject.devDependencies[key] = "workspace:*";
}
},
);
fs.writeFileSync(packageJson, JSON.stringify(packageJsonObject, null, 2));
}
}
// clean old files / dirs
fs.rmSync(COMPONENT_DIR, { recursive: true, force: true });
fs.readdirSync(EXAMPLES_PAGES_DIR, { withFileTypes: true }).forEach((file) => {
if (file.isDirectory()) {
fs.rmSync(path.join(EXAMPLES_PAGES_DIR, file.name), {
recursive: true,
force: true,
});
}
});
// generate new files
const projects = getExampleProjects().filter((p) => p.config?.docs === true);
const groups = addTitleToGroups(groupProjects(projects));
for (const group of Object.values(groups)) {
await generateMetaForExampleGroup(group);
for (const project of group.projects) {
// eslint-disable-next-line no-console
console.log("generating docs for", project.fullSlug);
await generateCodeForExample(project);
await generatePageForExample(project);
await addDependenciesToExample(project);
}
}
await generateExampleGroupsData(projects);