-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-docs.py
More file actions
executable file
·367 lines (299 loc) · 12.7 KB
/
Copy pathmigrate-docs.py
File metadata and controls
executable file
·367 lines (299 loc) · 12.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""
migrate-docs.py — converts an old standalone Astro/Starlight docs/ setup to trickfire-docs
Old layout:
docs/
astro.config.mjs (Astro + Starlight config)
content/docs/**/*.md (the actual pages)
assets/, components/, styles/, public/, ...
(no docs.config.ts at repo root)
New layout (trickfire-docs framework):
docs.config.ts (created by this script)
package.json (created or updated with trickfire-docs dep)
docs/
**/*.md (pages, moved from docs/content/docs/)
Usage:
cd path/to/repo
python3 migrate-docs.py
"""
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
import textwrap
from pathlib import Path
def die(msg: str) -> None:
print(f"Error: {msg}", file=sys.stderr)
sys.exit(1)
def extract_sidebar(src: str) -> str:
idx = src.find("sidebar: [")
if idx == -1:
return "[]"
pos = idx + len("sidebar: ")
depth = 0
for i, c in enumerate(src[pos:], pos):
if c == "[":
depth += 1
elif c == "]":
depth -= 1
if depth == 0:
return src[pos : i + 1]
return "[]"
def js_to_ts_strings(js: str) -> str:
"""Replace JS single-quoted strings with double-quoted ones."""
return re.sub(
r"'([^'\\]*(?:\\.[^'\\]*)*)'",
lambda m: '"' + m.group(1).replace('"', '\\"') + '"',
js,
)
def build_landing(sidebar_raw: str) -> list[dict]:
group_pat = re.compile(
r'\{\s*label:\s*[\'"]([^\'"]+)[\'"][^{}]*items:\s*\[', re.DOTALL
)
slug_pat = re.compile(r"slug:\s*['\"]([^'\"]+)['\"]")
landing = []
for gm in group_pat.finditer(sidebar_raw):
label = gm.group(1)
slug_m = slug_pat.search(sidebar_raw[gm.end() :])
slug = slug_m.group(1) if slug_m else label.lower().replace(" ", "-")
landing.append({
"title": label,
"description": f"TODO: Add a description for {label}.",
"link": f"/{slug}/",
})
# Fallback: top-level link items
if not landing:
for lm in re.finditer(
r'\{\s*label:\s*[\'"]([^\'"]+)[\'"][^{}]*slug:\s*[\'"]([^\'"]+)[\'"]',
sidebar_raw,
):
landing.append({
"title": lm.group(1),
"description": "TODO: Add a description.",
"link": f"/{lm.group(2)}/",
})
if len(landing) >= 4:
break
if not landing:
landing = [{
"title": "Getting Started",
"description": "TODO: Add a description.",
"link": "/getting-started/",
}]
return landing[:4]
def format_landing_ts(items: list[dict]) -> str:
lines = ["["]
for item in items:
lines.append(" {")
lines.append(f" title: {json.dumps(item['title'])},")
lines.append(f" description: {json.dumps(item['description'])},")
lines.append(f" link: {json.dumps(item['link'])},")
lines.append(" },")
lines.append(" ]")
return "\n".join(lines)
def main() -> None:
repo_root = Path(os.getcwd()).resolve()
docs_dir = repo_root / "docs"
config_mjs = docs_dir / "astro.config.mjs"
old_content = docs_dir / "content" / "docs"
config_dest = repo_root / "docs.config.ts"
pkg_json_path = repo_root / "package.json"
print(f"==> Migrating: {repo_root}")
# ---- Pre-flight checks ------------------------------------------------
if not config_mjs.exists():
die("docs/astro.config.mjs not found — not an old-style docs repo?")
if not old_content.is_dir():
die("docs/content/docs/ not found — unexpected structure.")
if config_dest.exists():
die("docs.config.ts already exists — already migrated?")
# ---- Parse old astro.config.mjs ---------------------------------------
src = config_mjs.read_text()
title_m = re.search(r"title:\s*['\"]([^'\"]+)['\"]", src)
title = title_m.group(1) if title_m else "My Project"
sidebar_raw = extract_sidebar(src)
print(f" title : {title}")
# ---- Determine repo/package name --------------------------------------
repo_name = ""
if pkg_json_path.exists():
repo_name = json.loads(pkg_json_path.read_text()).get("name", "")
if not repo_name:
base_m = re.search(r"base:\s*['\"]/?([^'\"]+)['\"]", src)
repo_name = base_m.group(1).strip("/") if base_m else repo_root.name
print(f" repo name: {repo_name}")
# ---- Extract description and landing cards from old index.mdx ----------
description: str | None = None
landing_from_index: list[dict] | None = None
index_mdx = old_content / "index.mdx"
if index_mdx.exists():
mdx = index_mdx.read_text()
# tagline is the visible hero text; description is just the SEO meta field
for field in ("tagline", "description"):
m = re.search(rf"^\s*{field}:\s*(.+)$", mdx, re.MULTILINE)
if m:
description = m.group(1).strip().strip("\"'")
break
# LinkCard components → landing array
def _attr(block: str, name: str) -> str | None:
m = re.search(rf'{name}=["\']([^"\']+)["\']', block)
return m.group(1) if m else None
cards = []
for card_m in re.finditer(r"<LinkCard\b(.*?)/>", mdx, re.DOTALL):
block = card_m.group(1)
t, d, h = _attr(block, "title"), _attr(block, "description"), _attr(block, "href")
if t and d and h:
# Strip the base-path prefix the old config baked into hrefs
h = re.sub(rf"^/{re.escape(repo_name)}/", "/", h)
cards.append({"title": t, "description": d, "link": h})
if cards:
landing_from_index = cards[:4]
# ---- Identify user assets to preserve ---------------------------------
# Framework provides these; everything else in assets/ and public/ is user content.
FRAMEWORK_FILES = {"nav-logo.png", "favicon.ico", "logo-small.png"}
user_assets: dict[Path, Path] = {} # dest (relative to docs_dir) -> source
for subdir in ("assets", "public"):
src_dir = docs_dir / subdir
if src_dir.is_dir():
for entry in src_dir.iterdir():
if entry.name not in FRAMEWORK_FILES:
user_assets[Path(subdir) / entry.name] = entry
# ---- Migrate content --------------------------------------------------
tmp = Path(tempfile.mkdtemp(prefix="tf_docs_migrate_"))
try:
tmp_content = tmp / "content"
shutil.copytree(old_content, tmp_content)
old_index = tmp_content / "index.mdx"
if old_index.exists():
old_index.unlink()
print(" removed : docs/content/docs/index.mdx (framework regenerates it)")
# Stash user assets into temp dir before docs/ is deleted
tmp_assets = tmp / "assets_stash"
for rel, src_path in user_assets.items():
dest = tmp_assets / rel
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, dest)
print(" removing : old docs/ infrastructure…")
shutil.rmtree(docs_dir)
shutil.copytree(tmp_content, docs_dir)
for rel in user_assets:
src_path = tmp_assets / rel
dest = docs_dir / rel
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_path, dest)
print(f" kept : docs/{rel}")
finally:
shutil.rmtree(tmp, ignore_errors=True)
# ---- Fix image paths in migrated content ------------------------------
# Old content lived at docs/content/docs/, 2 levels deeper than the new
# docs/ root. Any relative image path pointing at docs/assets/ had 2 extra
# "../" (e.g. "../../../assets/X" from docs/content/docs/cat/). Strip them.
img_pat = re.compile(r'(!\[[^\]]*\]\()((?:\.\.\/){2,})(assets\/[^)]+\))')
for md_file in docs_dir.rglob("*.md"):
text = md_file.read_text()
def _fix(m: re.Match) -> str:
return m.group(1) + m.group(2)[6:] + m.group(3)
new_text = img_pat.sub(_fix, text)
if new_text != text:
md_file.write_text(new_text)
print(f" fixed : image paths in docs/{md_file.relative_to(docs_dir)}")
# ---- Generate docs.config.ts ------------------------------------------
landing = landing_from_index or build_landing(sidebar_raw)
desc = description or f"TODO: One-line description of {title}."
sidebar_ts = js_to_ts_strings(sidebar_raw)
config_src = textwrap.dedent(f"""\
import {{ defineConfig }} from "trickfire-docs";
export default defineConfig({{
name: {json.dumps(title)},
description: {json.dumps(desc)},
landing: {format_landing_ts(landing)},
sidebar: {sidebar_ts},
}});
""")
config_dest.write_text(config_src)
print(" created : docs.config.ts")
# ---- Create or update package.json ------------------------------------
if pkg_json_path.exists():
pkg = json.loads(pkg_json_path.read_text())
pkg.setdefault("dependencies", {})["trickfire-docs"] = "latest"
# Remove stale pnpm key if present — these settings moved to pnpm-workspace.yaml
pkg.pop("pnpm", None)
pkg_json_path.write_text(json.dumps(pkg, indent=4) + "\n")
print(" updated : package.json (added trickfire-docs dependency)")
else:
pkg = {
"name": repo_name,
"type": "module",
"private": True,
"scripts": {
"docs:dev": "trickfire-docs dev",
"docs:build": "trickfire-docs build",
},
"dependencies": {
"trickfire-docs": "latest",
},
}
pkg_json_path.write_text(json.dumps(pkg, indent=4) + "\n")
print(f" created : package.json (name={repo_name})")
# ---- Write pnpm-workspace.yaml ----------------------------------------
# pnpm 11 reads build-script approvals and supply-chain policy from
# pnpm-workspace.yaml. allowBuilds approves build scripts per package;
# minimumReleaseAgeExclude skips the release-age check for first-party packages.
NEEDS_BUILD = ["esbuild", "sharp"]
workspace_yaml = repo_root / "pnpm-workspace.yaml"
def yaml_ensure_map_entry(content: str, key: str, field: str, value: str) -> str:
"""Ensure `field: value` exists under the `key:` map block."""
entry = f" {field}: {value}"
if f"{field}: {value}" in content:
return content
# Update an existing placeholder for this field
import re as _re
content = _re.sub(rf" {field}:.*", entry, content)
if entry in content:
return content
# Append under the key block if it already exists
if f"{key}:" in content:
return content.replace(f"{key}:\n", f"{key}:\n{entry}\n", 1)
return content + f"\n{key}:\n{entry}\n"
def yaml_ensure_list_entry(content: str, key: str, value: str) -> str:
entry = f" - {value}"
if value in content:
return content
if f"{key}:" in content:
return content.replace(f"{key}:\n", f"{key}:\n{entry}\n", 1)
return content + f"\n{key}:\n{entry}\n"
if workspace_yaml.exists():
content = workspace_yaml.read_text()
for dep in NEEDS_BUILD:
content = yaml_ensure_map_entry(content, "allowBuilds", dep, "true")
content = yaml_ensure_list_entry(content, "minimumReleaseAgeExclude", "trickfire-docs")
workspace_yaml.write_text(content)
print(" updated : pnpm-workspace.yaml")
else:
build_entries = "\n".join(f" {d}: true" for d in NEEDS_BUILD)
workspace_yaml.write_text(
f"allowBuilds:\n{build_entries}\n"
f"\nminimumReleaseAgeExclude:\n - trickfire-docs\n"
)
print(" created : pnpm-workspace.yaml")
# Delete any stale lockfile so pnpm re-resolves against the updated policy.
lockfile = repo_root / "pnpm-lock.yaml"
if lockfile.exists():
lockfile.unlink()
print(" removed : stale pnpm-lock.yaml")
# ---- Install dependencies ---------------------------------------------
print()
print("==> Running pnpm install…")
subprocess.run(["pnpm", "install"], cwd=repo_root, check=True)
print()
print("Migration complete.")
print()
print("TODOs in docs.config.ts:")
print(" • Fill in the description field")
print(" • Update landing card descriptions")
print()
print("To preview:")
print(" pnpm exec trickfire-docs dev")
if __name__ == "__main__":
main()