1 import path from "path"
2 import {
3 ChangedPackage,
4 DocProject,
5 DocProjectMapping,
6 PackageKind,
7 } from "../types/index.js"
8 import getMonorepoRoot from "../utils/get-monorepo-root.js"
9
10 const KIND_TO_PROJECTS: Record<PackageKind, DocProject[]> = {
11 module: ["book", "resources"],
12 "core-flow": ["book", "resources"],
13 framework: ["book", "resources"],
14 // admin casts wide: book+resources for widget/route extension docs,
15 // user-guide for UI behavior. Claude decides which applies.
16 admin: ["book", "resources", "user-guide"],
17 // design-system casts wide: book+resources if component API changed,
18 // ui always. Claude decides which applies.
19 "design-system": ["book", "resources", "ui"],
20 // CLI tools primarily affect resources docs.
21 cli: ["resources"],
22 // "other" is included as advisory context — Claude decides if updates
23 // are needed. Map to all projects so the writable dirs are available.
24 other: ["book", "resources", "user-guide"],
25 }
26
27 const WRITABLE_DIRS: Record<DocProject, string[]> = {
28 book: ["www/apps/book/app"],
29 resources: ["www/apps/resources/app"],
30 ui: ["www/apps/ui/app", "www/apps/ui/specs/examples"],
31 "user-guide": ["www/apps/user-guide/app"],
32 }
33
34 export class DocMapper {
35 private monorepoRoot: string
36
37 constructor() {
38 this.monorepoRoot = getMonorepoRoot()
39 }
40
41 map(changedPackages: ChangedPackage[]): DocProjectMapping[] {
42 const projectSet = new Set<DocProject>()
43
44 for (const pkg of changedPackages) {
45 const projects = KIND_TO_PROJECTS[pkg.kind] ?? []
46 for (const project of projects) {
47 projectSet.add(project)
48 }
49 }
50
51 return [...projectSet].map((project) =>
52 this.buildMapping(project, changedPackages)
53 )
54 }
55
56 private buildMapping(
57 project: DocProject,
58 changedPackages: ChangedPackage[]
59 ): DocProjectMapping {
60 const relevantPackages = changedPackages.filter((pkg) =>
61 (KIND_TO_PROJECTS[pkg.kind] ?? []).includes(project)
62 )
63 const reason = relevantPackages
64 .map((pkg) => `${pkg.name} (${pkg.kind})`)
65 .join(", ")
66
67 return {
68 project,
69 projectPath: path.join(this.monorepoRoot, "www", "apps", project),
70 writableContentDirs: WRITABLE_DIRS[project].map((dir) =>
71 path.join(this.monorepoRoot, dir)
72 ),
73 reason: `Changes in: ${reason}`,
74 }
75 }
76 }