|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { writeFileSync } = require("fs") |
| 4 | +const { relative, resolve } = require("path") |
| 5 | + |
| 6 | +const repoRoot = resolve(__dirname, "../..") |
| 7 | +const errorsPath = resolve(repoRoot, "packages/mobx/src/errors.ts") |
| 8 | +const outputPath = resolve(repoRoot, "docs/errors.md") |
| 9 | + |
| 10 | +globalThis.__DEV__ = true |
| 11 | +const { niceErrors } = require(errorsPath) |
| 12 | + |
| 13 | +function formatMarkdownCell(message) { |
| 14 | + let value = message |
| 15 | + |
| 16 | + if (typeof message === "function") { |
| 17 | + // Render dynamic errors with visible placeholders instead of real runtime values |
| 18 | + const match = message.toString().match(/^[^(]*\(([^)]*)\)/) |
| 19 | + const params = match ? match[1] : "" |
| 20 | + const names = params.split(",").map(name => name.trim()).filter(Boolean) |
| 21 | + const args = names.map(name => `{${name}}`) |
| 22 | + value = message(...args).replace("String", args[0]) |
| 23 | + } |
| 24 | + |
| 25 | + // Keep the generated table valid Markdown |
| 26 | + return String(value).replace(/\r?\n/g, "<br />").replace(/\|/g, "\\|") |
| 27 | +} |
| 28 | + |
| 29 | +function renderDocs(errors) { |
| 30 | + const rows = Object.keys(errors) |
| 31 | + .sort((left, right) => Number(left) - Number(right)) |
| 32 | + .map(code => `| ${code} | ${formatMarkdownCell(errors[code])} |`) |
| 33 | + .join("\n") |
| 34 | + |
| 35 | + return `--- |
| 36 | +title: MobX error codes |
| 37 | +sidebar_label: Error codes |
| 38 | +hide_title: true |
| 39 | +custom_edit_url: https://github.com/mobxjs/mobx/edit/main/packages/mobx/src/errors.ts |
| 40 | +--- |
| 41 | +
|
| 42 | +<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CEBD4KQ7&placement=mobxjsorg" id="_carbonads_js"></script> |
| 43 | +
|
| 44 | +# MobX error codes |
| 45 | +
|
| 46 | +In development builds, MobX throws full error messages. Production builds replace known messages with short numeric codes to keep the published bundle smaller. |
| 47 | +
|
| 48 | +| Code | Message | |
| 49 | +| ---- | ------- | |
| 50 | +${rows} |
| 51 | +` |
| 52 | +} |
| 53 | + |
| 54 | +const docs = renderDocs(niceErrors) |
| 55 | +writeFileSync(outputPath, docs) |
| 56 | + |
| 57 | +console.log(`Generated ${relative(repoRoot, outputPath)} from ${relative(repoRoot, errorsPath)}`) |
0 commit comments