Skip to content

Commit fb13bd1

Browse files
committed
chore(scripts): move format-component-api to docs
1 parent f692c3b commit fb13bd1

5 files changed

Lines changed: 44 additions & 15 deletions

File tree

bun.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"private": true,
33
"scripts": {
4-
"dev": "bun run --parallel index-docs dev:vite",
4+
"dev": "bun run format-component-api && bun run --parallel index-docs dev:vite",
55
"dev:vite": "vite dev",
6-
"build": "bun run --parallel build:index-docs build:sitemap && bun run build:md-docs && bun run build:vite",
6+
"build": "bun run format-component-api && bun run --parallel build:index-docs build:sitemap && bun run build:md-docs && bun run build:vite",
77
"index-docs": "bun scripts/index-docs.ts",
88
"build:index-docs": "bun scripts/index-docs.ts",
99
"build:md-docs": "bun scripts/generate-md-docs.ts",
1010
"build:sitemap": "bun scripts/generate-sitemap.ts",
11+
"format-component-api": "bun scripts/format-component-api.ts",
1112
"build:vite": "vite build",
1213
"preview": "vite preview"
1314
},
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
14
import { format } from "prettier";
25
import plugin from "prettier/plugins/typescript";
3-
import componentApi from "../docs/src/COMPONENT_API.json" with { type: "json" };
6+
import componentApi from "../src/COMPONENT_API.json" with { type: "json" };
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
const outFile = path.join(__dirname, "../src/COMPONENT_API.json");
410

511
const WHITESPACE_REGEX = /\s{2,}/;
612
const TRAILING_SEMICOLON_REGEX = /;\s*$/;
713
const EVENT_DETAIL_PREFIX_REGEX = /type EventDetail = /;
814
const SLOT_PROPS_PREFIX_REGEX = /type SlotProps = /;
915

10-
const formatTypeScript = async (value) => {
16+
/** Avoid `type EventDetail = type EventDetail = …` when re-running the formatter. */
17+
const stripLeadingTypeAlias = (
18+
source: string,
19+
name: "EventDetail" | "SlotProps",
20+
) => {
21+
const re = new RegExp(`^\\s*type\\s+${name}\\s*=\\s*`, "i");
22+
let s = source;
23+
let next = s.replace(re, "").trimStart();
24+
while (next !== s) {
25+
s = next;
26+
next = s.replace(re, "").trimStart();
27+
}
28+
return s;
29+
};
30+
31+
const formatTypeScript = async (value: string) => {
1132
return await format(value, {
1233
parser: "typescript",
1334
plugins: [plugin],
@@ -67,7 +88,16 @@ const modified = {
6788
return event;
6889
}
6990

70-
const normalizedValue = `type EventDetail = ${event.detail}`;
91+
if (!("detail" in event) || typeof event.detail !== "string") {
92+
return event;
93+
}
94+
95+
const detailBody = stripLeadingTypeAlias(event.detail, "EventDetail");
96+
if (!detailBody) {
97+
return event;
98+
}
99+
100+
const normalizedValue = `type EventDetail = ${detailBody}`;
71101

72102
const formatted = (await formatTypeScript(normalizedValue))
73103
// Remove prefix needed for formatting.
@@ -88,7 +118,10 @@ const modified = {
88118
return slot;
89119
}
90120

91-
let normalizedValue = slot.slot_props;
121+
let normalizedValue = stripLeadingTypeAlias(
122+
slot.slot_props,
123+
"SlotProps",
124+
);
92125

93126
if (normalizedValue.startsWith("{")) {
94127
normalizedValue = `type SlotProps = ${normalizedValue}`;
@@ -118,9 +151,6 @@ const modified = {
118151
),
119152
};
120153

121-
await Bun.write(
122-
"./docs/src/COMPONENT_API.json",
123-
JSON.stringify(modified, null, 2),
124-
);
154+
fs.writeFileSync(outFile, JSON.stringify(modified, null, 2));
125155

126156
console.timeEnd("formatComponentApi");

docs/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"baseUrl": ".",
54
"paths": {
65
"carbon-components-svelte": ["../src"],
7-
"carbon-components-svelte/*": ["../*"]
6+
"carbon-components-svelte/*": ["../*"],
7+
"*": ["./*"]
88
}
99
},
1010
"include": ["src/**/*", "scripts/**/*", "*.ts"]

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"test:e2e": "playwright test",
4242
"lint": "biome check --write .",
4343
"build:css": "bun scripts/build-css",
44-
"build:docs": "bun scripts/build-docs && bun scripts/format-component-api",
44+
"build:docs": "bun scripts/build-docs",
4545
"postinstall": "ibmtelemetry --config=telemetry.yml",
4646
"release": "standard-version && bun build:docs"
4747
},
@@ -64,7 +64,6 @@
6464
"culls": "^0.2.0",
6565
"jsdom": "^29.0.1",
6666
"postcss": "^8.5.5",
67-
"prettier": "^3.8.1",
6867
"sass": "^1.98.0",
6968
"standard-version": "^9.5.0",
7069
"sveld": "^0.29.1",

0 commit comments

Comments
 (0)