-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit.ts
219 lines (201 loc) · 6.57 KB
/
pre-commit.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { resolve } from "node:path";
import { createWriteStream, existsSync, rmSync } from "node:fs";
import { rm, readFile, writeFile } from "node:fs/promises";
import { Readable } from "node:stream";
import { finished } from "node:stream/promises";
import { ReadableStream } from "node:stream/web";
import { exec } from "node:child_process";
import { promisify } from "node:util";
type MintlifyNav = { group: string; pages: Array<string | MintlifyNav> };
const tempFile = `${crypto.randomUUID()}.yml`; // This is 'global' so cleanup can non-awkwardly access it
const run = async (cmd: string) => {
const result = await promisify(exec)(cmd);
if (result.stderr) console.error(result.stderr);
result.stdout = result.stdout.trim();
return result;
};
const checkExecutingLocation = () => {
if (!resolve(process.argv[1]).includes(".git/hooks")) {
console.error(
"Consider making this a proper pre-commit hook by 'cp'-ing it into .git/hooks/!",
);
}
};
const determineGitRepoPath = async () => run("git rev-parse --show-toplevel");
const isString = (obj: unknown): obj is string =>
typeof obj === "string" || obj instanceof String;
const loadMintConfig = async (): Promise<{
navigation: MintlifyNav[];
openapi: string;
}> => {
const gitRootLocation = await determineGitRepoPath();
const currentMintConfig = await readFile(
resolve(gitRootLocation.stdout, "mint.json"),
{ encoding: "utf8" },
);
const mintJson = JSON.parse(currentMintConfig);
if (!isMintlifyNav(mintJson?.navigation)) {
throw new Error(
"The 'navigation' config in the mint.json appears to be malformed",
);
}
if (!isString(mintJson?.openapi)) {
throw new Error(
"The 'openapi' config in the mint.json appears to be malformed",
);
}
return mintJson;
};
const downloadOpenAPISchema = async () => {
const mintConfig = await loadMintConfig();
const schema = await fetch(mintConfig.openapi);
if (!schema.body) {
throw Error("Failed to fetch schema (received empty response body)");
}
const fileStream = createWriteStream(tempFile, { flags: "wx+" });
await finished(
Readable.fromWeb(schema.body as ReadableStream<any>).pipe(fileStream),
);
return tempFile;
};
const generateMintlifySchema = async () => {
const gitRootLocation = await determineGitRepoPath();
const schemaLocation = await downloadOpenAPISchema();
const autogenFolder = "partner-solutions/api-reference/endpoints";
await rm(resolve(gitRootLocation.stdout, autogenFolder), {
recursive: true,
force: true,
});
// This does a `cd` because Mintlify seems to take the full path when generating the suggested navigation, which is non-portable as it'll be machine-specific
const schemaGeneration = await run(
`cd ${gitRootLocation.stdout} && npx @mintlify/scraping openapi-file ${schemaLocation} -o ${autogenFolder}`,
);
return schemaGeneration;
};
const mintlifyNavigationToMap = (navigation: MintlifyNav[]) => {
return new Map(navigation.map(({ group, pages }) => [group, pages]));
};
const isMintlifyNav = (obj: unknown): obj is MintlifyNav[] => {
return (
Array.isArray(obj) &&
obj.every(
(element) =>
isString(element) ||
(isString(element?.group) &&
Array.isArray(element?.pages) &&
element.pages.every(
(page: any) => isString(page) || isMintlifyNav([page]),
) &&
element.pages.length > 0),
)
);
};
const processNewNavigation = async () => {
const output = await generateMintlifySchema();
let newNavigation = "";
let inJson = false;
for (const line of output.stdout.split("\n")) {
if (inJson) {
newNavigation += line;
}
inJson = line == "navigation object suggestion:" || inJson;
}
const nav = JSON.parse(newNavigation);
if (!isMintlifyNav(nav)) {
throw new Error(
`${JSON.stringify(
nav,
)} is invalid; check the Mintlify schema generation output: ${
output.stdout
}`,
);
}
return mintlifyNavigationToMap(nav);
};
const extractRoutes = (nav: MintlifyNav[]): string[] =>
nav
.flatMap(({ pages }) =>
pages.map((page) => (isString(page) ? page : extractRoutes([page]))),
)
.flat();
const isNavigationConfigValid = async (nav: MintlifyNav[]) => {
const gitRootLocation = await determineGitRepoPath();
const invalid = extractRoutes(nav).filter(
(route) => !existsSync(resolve(gitRootLocation.stdout, `${route}.mdx`)),
);
if (invalid.length != 0) {
throw new Error(
`Invalid route configurations exist in the navigation: ${JSON.stringify(
invalid,
)}`,
);
}
};
const filterIgnoredRoutes = ({
nav,
routes,
}: {
nav: MintlifyNav[];
routes: string[];
}): MintlifyNav[] =>
nav.map(({ group, pages }) => ({
group,
pages: pages
.filter(
(page) =>
!isString(page) || !routes.some((route) => page.endsWith(route)),
)
.map((page) =>
isString(page) ? page : filterIgnoredRoutes({ nav: [page], routes })[0],
),
}));
const updateMintlifyConfig = async ({ ignore }: { ignore: string[] }) => {
const gitRootLocation = await determineGitRepoPath();
const mintConfig = await loadMintConfig();
const generatedNav = await processNewNavigation();
const newNav = new Map();
for (const [group, pages] of mintlifyNavigationToMap(mintConfig.navigation)) {
const routes = generatedNav.get(group);
if (routes != null) {
newNav.set(group, Array.from(new Set(pages.concat(routes))));
} else {
newNav.set(group, pages);
}
}
const nav = [];
for (const [group, pages] of newNav) {
nav.push({ group, pages });
}
mintConfig.navigation = filterIgnoredRoutes({ nav, routes: ignore });
await writeFile(
resolve(gitRootLocation.stdout, "mint.json"),
JSON.stringify(mintConfig, null, 4),
);
await isNavigationConfigValid(mintConfig.navigation);
};
const checkForBrokenLinks = async () => run("npx mintlify broken-links");
(async () => {
checkExecutingLocation();
const gitRootLocation = await determineGitRepoPath();
const ignoredEndpoints = await readFile(
resolve(gitRootLocation.stdout, "ignore-endpoints"),
{ encoding: "utf8" },
);
await updateMintlifyConfig({
ignore: ignoredEndpoints.split("\n").filter(Boolean),
});
const brokenLinks = await checkForBrokenLinks();
console.log(brokenLinks.stdout.trim());
await rm(tempFile);
})().catch((e) => {
if ("stderr" in e) {
console.error(String(e.stderr).trim());
}
if ("stdout" in e) {
console.error(String(e.stdout).trim());
} else {
console.error(e);
}
if (existsSync(tempFile)) rmSync(tempFile);
process.exit(1);
});