forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-docgen.mjs
More file actions
284 lines (239 loc) · 8.55 KB
/
Copy patherror-docgen.mjs
File metadata and controls
284 lines (239 loc) · 8.55 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import jsdoc from 'jsdoc-api';
import fs from 'node:fs';
import ts from 'typescript';
const sourceBranch = process.env.SOURCE_BRANCH || 'main';
const sourceRepo = process.env.SOURCE_REPO || 'withastro/astro';
const errorURL = `https://raw.githubusercontent.com/${sourceRepo}/${sourceBranch}/packages/astro/src/core/errors/errors-data.ts`;
// Fill this in to test a response locally, without fetching.
const STUB = undefined; // fs.readFileSync('../astro/packages/astro/src/core/errors/errors-data.ts', {encoding: 'utf-8',});
const HEADER = `---
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
# Do not make edits to it directly, they will be overwritten.
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
# Translators, please remove this note and the <DontEditWarning/> component.
title: Error reference
i18nReady: true
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
---
import DontEditWarning from '~/components/DontEditWarning.astro'
<DontEditWarning />
The following reference is a complete list of the errors you may encounter while using Astro. For additional assistance, including common pitfalls, please also see our [Troubleshooting Guide](/en/guides/troubleshooting/).
`;
const FOOTER = '';
export async function run() {
const astroErrorData = await getAstroErrorsData();
// TODO: Implement compiler errors
const alreadyDocumentedErrors = fs.readdirSync('src/content/docs/en/reference/errors');
const documentedErrors = [];
let astroResult = '';
for (const comment of astroErrorData.jsdoc) {
// Header
if (comment.kind === 'heading') {
astroResult += `\n## ${comment.name}\n\n`;
if (comment.description) {
astroResult += `${comment.description.trim()}\n\n`;
}
continue;
}
// The `@see` comments are often formatted in a way where despite containing multiple lines
// they count as one big string. We'll manually create an array when this happens so we can map through it later
if (comment.see && comment.see.length === 1) {
comment.see = comment.see[0].split('\n');
}
// The error's title. Fallback to the error's name if we don't have one
const errorTitle = sanitizeString(
// the name has string like "export.UnknownError", and we remove the "export." bit to get the name of the error
astroErrorData.errors[comment.meta.code.name.slice('exports.'.length)].title ?? comment.name
);
const completeReferenceEntry = [
// Errors can be deprecated, as such we add a little "deprecated" caution to errors that needs it
getDeprecatedText(comment.deprecated),
'',
// Get the error message and print it in a blockquote
getMessage(
comment.name,
astroErrorData.errors[comment.name].message,
comment.tags.find((tag) => tag.title === 'message')?.value
),
// Show the error's description under a header
comment.description && `## What went wrong?\n${comment.description.trim()}`,
'',
// List @see entries
comment.see
? `**See Also:**\n${comment.see.map((s) => `- ${s.replace('-', '')}`.trim()).join('\n')}`
: undefined,
'\n\n',
]
.filter((l) => l !== undefined)
.join('\n')
// Replace absolute links with relative ones
.replace(/https\\?:\/\/docs\.astro\.build\//g, '/');
const fileName = getKebabFilename(comment.name);
const filePath = `src/content/docs/en/reference/errors/${fileName}.mdx`;
fs.writeFileSync(filePath, getErrorReferenceEntryHeader(errorTitle) + completeReferenceEntry);
documentedErrors.push(`${fileName}.mdx`);
// Build string for error reference list
astroResult += [
`- [**${comment.name}**](/en/reference/errors/${fileName}/)<br/>${errorTitle}\n`,
]
.filter((l) => l !== undefined)
.join('\n');
}
// Add deprecation notice for errors that are no longer emitted
const deprecatedErrors = alreadyDocumentedErrors.filter(
(error) => !documentedErrors.includes(error)
);
for (const error of deprecatedErrors) {
const filePath = `src/content/docs/en/reference/errors/${error}`;
const currentContent = fs.readFileSync(filePath, 'utf8');
// If the error got removed without a deprecation, add a deprecation notice
if (!currentContent.includes(':::caution[Deprecated]')) {
fs.writeFileSync(
filePath,
currentContent.replace(
'<DontEditWarning />',
[
'<DontEditWarning />',
'',
getDeprecatedText(
'This error is from an older version of Astro and is no longer in use. If you are unable to upgrade your project to a more recent version, then you can consult [unmaintained snapshots of older documentation](/en/upgrade-astro/#older-docs-unmaintained) for assistance.'
),
].join('\n')
),
'utf8'
);
}
}
fs.writeFileSync(
'src/content/docs/en/reference/error-reference.mdx',
HEADER + astroResult + FOOTER,
'utf8'
);
/**
* @param {string} errorName
* @param {string} message
* @param {string} cleanMessage
* @returns {(string | undefined)} Formatted message for the error or `undefined`
*/
function getMessage(errorName, message, cleanMessage) {
let resultMessage = undefined;
if (cleanMessage) {
resultMessage = `> ${cleanMessage}`;
} else if (message) {
if (typeof message === 'string') {
resultMessage = `> **${errorName}**: ${sanitizeString(message)}`;
} else {
resultMessage = `> **${errorName}**: ${String.raw({
raw: extractStringFromFunction(message.toString()),
})}`;
}
}
if (resultMessage) {
return `${resultMessage}\n`;
}
return undefined;
}
/**
* @param {string | boolean} deprecateMention
*/
function getDeprecatedText(deprecateMention) {
if (!deprecateMention) {
return undefined;
}
return [
'',
':::caution[Deprecated]',
typeof deprecateMention === 'string'
? deprecateMention
: 'This error cannot be emitted by Astro anymore',
':::',
].join('\n');
}
/**
* @param {string} filename
*/
function getKebabFilename(filename) {
return filename.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
}
async function getAstroErrorsData() {
const inputBuffer = STUB || (await fetch(errorURL).then((r) => r.text()));
const compiledResult = ts.transpileModule(inputBuffer, {
compilerOptions: {
module: 'esnext',
target: 'esnext',
removeComments: false,
},
}).outputText;
const encodedJs = encodeURIComponent(compiledResult);
const dataUri = `data:text/javascript;charset=utf-8,${encodedJs}`;
/**
* @type {{AstroErrorData: Object.<string, {code: number, message: string, hint: string, name: string}>}
*/
const data = await import(dataUri);
/**
* Get all the JSDoc comments in the file marked with the tag `@docs`
*/
const jsDocComments = (await jsdoc.explain({ source: compiledResult })).filter((data) =>
data.tags?.some((tag) => tag.title === 'docs')
);
return {
errors: data,
jsdoc: jsDocComments,
};
}
/**
*
* @param {string} errorTitle
* @returns {string}
*/
function getErrorReferenceEntryHeader(errorTitle) {
const sanitizedTitle = errorTitle.replaceAll('`', '');
return `
---
# NOTE: This file is auto-generated from 'scripts/error-docgen.mjs'
# Do not make edits to it directly, they will be overwritten.
# Instead, change this file: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
# Translators, please remove this note and the <DontEditWarning/> component.
title: ${sanitizedTitle}
i18nReady: true
githubURL: https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/errors-data.ts
---
import DontEditWarning from '~/components/DontEditWarning.astro'
<DontEditWarning />
`.trimStart();
}
/**
* @param {string} func
*/
function extractStringFromFunction(func) {
const arrowIndex = func.indexOf('=>') + '=>'.length;
return expressionToText(func.slice(arrowIndex).trim().slice(1, -1));
// Turn the following syntax: `{componentName}` into this result: `COMPONENT_NAME`
function expressionToText(text) {
return sanitizeString(
text.replaceAll(
/\${([^}]+)}/gm,
(_, match1) =>
`${match1
.split(/\.?(?=[A-Z])/)
.join('_')
.toUpperCase()}`
)
);
}
}
/**
* MDX doesn't like some characters, so we need to sanitize the messages regarding the usage of client directives and HTML tags.
* Also remove unnecessary backslashes for inline code and replace newlines with `<br/>`
* @param {string} message
*/
function sanitizeString(message) {
return message
.replaceAll(/\\`/gm, '`')
.replaceAll(/`?(client:[\w]+(="\(.+\)")?)`?/g, '`$1`')
.replaceAll(/([^`\\])</gm, '$1\\<')
.replaceAll(/>([^`\\])/gm, '\\$1>')
.replaceAll('\\n', '<br/>');
}
run();