|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync, writeFileSync } from "node:fs" |
| 4 | +import { relative } from "node:path" |
| 5 | +import { fileURLToPath } from "node:url" |
| 6 | +import { runInNewContext } from "node:vm" |
| 7 | + |
| 8 | +const repoRoot = fileURLToPath(new URL("../..", import.meta.url)) |
| 9 | +const errorsPath = fileURLToPath(new URL("../../packages/mobx/src/errors.ts", import.meta.url)) |
| 10 | +const outputPath = fileURLToPath(new URL("../../docs/errors.md", import.meta.url)) |
| 11 | + |
| 12 | +function readNiceErrors() { |
| 13 | + const source = readFileSync(errorsPath, "utf8") |
| 14 | + const match = source.match(/const niceErrors = ([\s\S]*?) as const/) |
| 15 | + |
| 16 | + if (!match) { |
| 17 | + throw new Error(`Could not find niceErrors in ${errorsPath}`) |
| 18 | + } |
| 19 | + |
| 20 | + const objectLiteral = match[1].replace(": PropertyKey", "") |
| 21 | + return runInNewContext(`(${objectLiteral})`, Object.create(null), { timeout: 1000 }) |
| 22 | +} |
| 23 | + |
| 24 | +function placeholder(name) { |
| 25 | + return { |
| 26 | + constructor: { |
| 27 | + name: `{${name}.constructor.name}` |
| 28 | + }, |
| 29 | + toString() { |
| 30 | + return `{${name}}` |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function getParamNames(fn) { |
| 36 | + const match = fn.toString().match(/^[^(]*\(([^)]*)\)/) |
| 37 | + return match ? match[1].split(",").map(name => name.trim()).filter(Boolean) : [] |
| 38 | +} |
| 39 | + |
| 40 | +function renderMessage(message) { |
| 41 | + if (typeof message !== "function") { |
| 42 | + return message |
| 43 | + } |
| 44 | + |
| 45 | + return message(...getParamNames(message).map(placeholder)) |
| 46 | +} |
| 47 | + |
| 48 | +function escapeMarkdownTableCell(value) { |
| 49 | + return String(value).replace(/\r?\n/g, "<br />").replace(/\|/g, "\\|") |
| 50 | +} |
| 51 | + |
| 52 | +function renderDocs(errors) { |
| 53 | + const rows = Object.keys(errors) |
| 54 | + .sort((left, right) => Number(left) - Number(right)) |
| 55 | + .map(code => `| ${code} | ${escapeMarkdownTableCell(renderMessage(errors[code]))} |`) |
| 56 | + .join("\n") |
| 57 | + |
| 58 | + return `--- |
| 59 | +title: MobX error codes |
| 60 | +sidebar_label: Error codes |
| 61 | +hide_title: true |
| 62 | +custom_edit_url: https://github.com/mobxjs/mobx/edit/main/packages/mobx/src/errors.ts |
| 63 | +--- |
| 64 | +
|
| 65 | +<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CEBD4KQ7&placement=mobxjsorg" id="_carbonads_js"></script> |
| 66 | +
|
| 67 | +# MobX error codes |
| 68 | +
|
| 69 | +In development builds, MobX throws full error messages. Production builds replace known messages with short numeric codes to keep the published bundle small. |
| 70 | +
|
| 71 | +| Code | Message | |
| 72 | +| ---- | ------- | |
| 73 | +${rows} |
| 74 | +` |
| 75 | +} |
| 76 | + |
| 77 | +const docs = renderDocs(readNiceErrors()) |
| 78 | +writeFileSync(outputPath, docs) |
| 79 | + |
| 80 | +console.log(`Generated ${relative(repoRoot, outputPath)} from ${relative(repoRoot, errorsPath)}`) |
0 commit comments