@behavioral-agentic-design-system/core 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +37 -4
- package/scripts/bads-sync.mjs +81 -0
- package/scripts/build-tokens.cjs +487 -0
- package/src/api/bads-update-check.ts +151 -0
- package/src/api/brand-colors.ts +180 -0
- package/src/api/layers/file.ts +71 -0
- package/src/api/layers.ts +49 -0
- package/src/api/save-tokens.ts +280 -0
- package/src/app/font-ready.tsx +51 -0
- package/src/app/layout.tsx +152 -0
- package/src/app/page.tsx +267 -0
- package/src/app/preview/cards/page.tsx +8 -0
- package/src/app/preview/components/page.tsx +232 -0
- package/src/app/preview/dark-mode-client.tsx +24 -0
- package/src/app/preview/dashboard/page.tsx +4 -0
- package/src/app/preview/global-font-client.tsx +36 -0
- package/src/app/preview/layout.tsx +13 -0
- package/src/app/preview/mail/page.tsx +4 -0
- package/src/app/preview/pricing/page.tsx +4 -0
- package/src/app/preview/typography/page.tsx +11 -0
- package/src/app/providers.tsx +49 -0
- package/src/components/Foundations/Colors.stories.tsx +85 -0
- package/src/components/Foundations/Motion.stories.tsx +169 -0
- package/src/components/Foundations/Radius.stories.tsx +36 -0
- package/src/components/Foundations/Shadows.stories.tsx +30 -0
- package/src/components/Foundations/Spacing.stories.tsx +35 -0
- package/src/components/Foundations/Typography.stories.tsx +68 -0
- package/src/components/bads-update-banner.tsx +120 -0
- package/src/components/customize/design-controls.tsx +165 -0
- package/src/components/customize/font-pickers.tsx +118 -0
- package/src/components/customize/letter-spacing.tsx +105 -0
- package/src/components/customize/save-bar.tsx +379 -0
- package/src/components/customize/theme-io.tsx +245 -0
- package/src/components/customize/type-scale.tsx +134 -0
- package/src/components/customize-tab.tsx +502 -0
- package/src/components/home-tab.tsx +1227 -0
- package/src/components/layers-tab.tsx +225 -0
- package/src/components/ui/alert.tsx +5 -1
- package/src/hooks/use-global-design-controls.ts +117 -0
- package/src/hooks/use-global-mono-font.ts +73 -0
- package/src/hooks/use-global-primary-color.ts +544 -0
- package/src/hooks/use-global-sans-font.ts +98 -0
- package/src/hooks/use-global-serif-font.ts +77 -0
- package/src/hooks/use-global-theme.ts +259 -0
- package/src/hooks/use-global-type-scale.ts +141 -0
- package/src/hooks/use-sync-type-scale.ts +51 -0
- package/src/hooks/use-token-overrides.ts +98 -0
- package/src/styles.css +40 -6
- package/src/tokens.json +86 -5
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { NextResponse } from "next/server";
|
|
6
|
+
|
|
7
|
+
const execAsync = promisify(exec);
|
|
8
|
+
|
|
9
|
+
// Where upstream BADS lives. Hardcoded — clients consuming this template
|
|
10
|
+
// inherit it. Change here if BADS ever moves repos.
|
|
11
|
+
const UPSTREAM = "matthewlarn/behavioral-agentic-design-system";
|
|
12
|
+
const UPSTREAM_BRANCH = "main";
|
|
13
|
+
const UPSTREAM_API = `https://api.github.com/repos/${UPSTREAM}/commits/${UPSTREAM_BRANCH}`;
|
|
14
|
+
const UPSTREAM_COMPARE = (from: string, to: string) =>
|
|
15
|
+
`https://github.com/${UPSTREAM}/compare/${from}...${to}`;
|
|
16
|
+
|
|
17
|
+
type IdentityFile = {
|
|
18
|
+
name?: string;
|
|
19
|
+
slug?: string;
|
|
20
|
+
upstream?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
async function readIdentity(): Promise<IdentityFile | null> {
|
|
24
|
+
try {
|
|
25
|
+
const raw = await fs.readFile(
|
|
26
|
+
path.join(process.cwd(), ".client-identity.json"),
|
|
27
|
+
"utf8",
|
|
28
|
+
);
|
|
29
|
+
return JSON.parse(raw) as IdentityFile;
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function readSyncState(): Promise<string | null> {
|
|
36
|
+
try {
|
|
37
|
+
const raw = await fs.readFile(
|
|
38
|
+
path.join(process.cwd(), ".bads-sync-state.json"),
|
|
39
|
+
"utf8",
|
|
40
|
+
);
|
|
41
|
+
const parsed = JSON.parse(raw) as { upstreamCommit?: string };
|
|
42
|
+
return parsed.upstreamCommit ?? null;
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function readLocalCommit(): Promise<string | null> {
|
|
49
|
+
// Prefer the recorded sync-state (last commit pulled FROM upstream); fall
|
|
50
|
+
// back to the local HEAD commit. The fallback is meaningful for forks that
|
|
51
|
+
// haven't run `pnpm bads:sync` yet — they're still at upstream HEAD as of
|
|
52
|
+
// their last clone.
|
|
53
|
+
const synced = await readSyncState();
|
|
54
|
+
if (synced) return synced;
|
|
55
|
+
try {
|
|
56
|
+
const { stdout } = await execAsync("git rev-parse HEAD", {
|
|
57
|
+
cwd: process.cwd(),
|
|
58
|
+
});
|
|
59
|
+
return stdout.trim() || null;
|
|
60
|
+
} catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function GET() {
|
|
66
|
+
const identity = await readIdentity();
|
|
67
|
+
|
|
68
|
+
// Upstream BADS itself doesn't need an update banner — it IS the upstream.
|
|
69
|
+
// Skip the check when we recognize this is upstream (no client-identity
|
|
70
|
+
// marker AND root package name matches the canonical upstream slug).
|
|
71
|
+
if (!identity) {
|
|
72
|
+
try {
|
|
73
|
+
const pkg = JSON.parse(
|
|
74
|
+
await fs.readFile(path.join(process.cwd(), "package.json"), "utf8"),
|
|
75
|
+
) as { name?: string };
|
|
76
|
+
if (pkg.name === "behavioral-agentic-design-system") {
|
|
77
|
+
return NextResponse.json({ ok: true, upstream: true, skip: "is-upstream" });
|
|
78
|
+
}
|
|
79
|
+
} catch {}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const local = await readLocalCommit();
|
|
83
|
+
if (!local) {
|
|
84
|
+
return NextResponse.json({ ok: true, error: "no-local-commit" });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let remoteSha: string | null = null;
|
|
88
|
+
let remoteShortMessage: string | null = null;
|
|
89
|
+
let remoteDate: string | null = null;
|
|
90
|
+
try {
|
|
91
|
+
const res = await fetch(UPSTREAM_API, {
|
|
92
|
+
headers: { Accept: "application/vnd.github.v3+json" },
|
|
93
|
+
// 5-minute cache so opening the showcase doesn't hammer the API.
|
|
94
|
+
next: { revalidate: 300 },
|
|
95
|
+
});
|
|
96
|
+
if (!res.ok) {
|
|
97
|
+
return NextResponse.json({ ok: true, error: `github-${res.status}` });
|
|
98
|
+
}
|
|
99
|
+
const data = (await res.json()) as {
|
|
100
|
+
sha?: string;
|
|
101
|
+
commit?: { message?: string; author?: { date?: string } };
|
|
102
|
+
};
|
|
103
|
+
remoteSha = data.sha ?? null;
|
|
104
|
+
remoteShortMessage = data.commit?.message?.split("\n")[0] ?? null;
|
|
105
|
+
remoteDate = data.commit?.author?.date ?? null;
|
|
106
|
+
} catch {
|
|
107
|
+
return NextResponse.json({ ok: true, error: "github-unreachable" });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!remoteSha) {
|
|
111
|
+
return NextResponse.json({ ok: true, error: "no-remote-sha" });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// If local matches remote, no update.
|
|
115
|
+
if (remoteSha === local) {
|
|
116
|
+
return NextResponse.json({
|
|
117
|
+
ok: true,
|
|
118
|
+
upToDate: true,
|
|
119
|
+
local,
|
|
120
|
+
remote: remoteSha,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Behind. Get the commits-behind count by counting GitHub's compare API.
|
|
125
|
+
let behind: number | null = null;
|
|
126
|
+
try {
|
|
127
|
+
const res = await fetch(
|
|
128
|
+
`https://api.github.com/repos/${UPSTREAM}/compare/${local}...${remoteSha}`,
|
|
129
|
+
{
|
|
130
|
+
headers: { Accept: "application/vnd.github.v3+json" },
|
|
131
|
+
next: { revalidate: 300 },
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
if (res.ok) {
|
|
135
|
+
const data = (await res.json()) as { ahead_by?: number; behind_by?: number };
|
|
136
|
+
behind = data.ahead_by ?? null;
|
|
137
|
+
}
|
|
138
|
+
} catch {}
|
|
139
|
+
|
|
140
|
+
return NextResponse.json({
|
|
141
|
+
ok: true,
|
|
142
|
+
upToDate: false,
|
|
143
|
+
local,
|
|
144
|
+
remote: remoteSha,
|
|
145
|
+
behind,
|
|
146
|
+
remoteMessage: remoteShortMessage,
|
|
147
|
+
remoteDate,
|
|
148
|
+
compareUrl: UPSTREAM_COMPARE(local, remoteSha),
|
|
149
|
+
releasesUrl: `https://github.com/${UPSTREAM}/releases`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { NextResponse } from "next/server";
|
|
4
|
+
|
|
5
|
+
const COLORS_PATH = path.join(process.cwd(), "brand-colors.json");
|
|
6
|
+
|
|
7
|
+
type BrandColorEntry = {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
primary: string;
|
|
11
|
+
primaryForeground?: string;
|
|
12
|
+
note?: string | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type ColorsFile = {
|
|
16
|
+
$comment?: string;
|
|
17
|
+
colors: BrandColorEntry[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type Op =
|
|
21
|
+
| { action: "pin"; entry: BrandColorEntry }
|
|
22
|
+
| { action: "rename"; id: string; label: string }
|
|
23
|
+
| { action: "remove"; id: string }
|
|
24
|
+
| { action: "reorder"; ids: string[] };
|
|
25
|
+
|
|
26
|
+
async function readFile(): Promise<ColorsFile> {
|
|
27
|
+
const raw = await fs.readFile(COLORS_PATH, "utf8");
|
|
28
|
+
const parsed = JSON.parse(raw) as ColorsFile;
|
|
29
|
+
if (!Array.isArray(parsed.colors)) {
|
|
30
|
+
throw new Error("brand-colors.json missing colors array");
|
|
31
|
+
}
|
|
32
|
+
return parsed;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function writeFile(file: ColorsFile) {
|
|
36
|
+
await fs.writeFile(COLORS_PATH, JSON.stringify(file, null, 2) + "\n", "utf8");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isValidHex(hex: string) {
|
|
40
|
+
return /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/.test(hex.trim());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isValidId(id: string) {
|
|
44
|
+
return /^[a-z][a-z0-9-]{0,63}$/.test(id);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function GET() {
|
|
48
|
+
if (process.env.NODE_ENV !== "development") {
|
|
49
|
+
return NextResponse.json({ ok: false, error: "dev-only" }, { status: 403 });
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const file = await readFile();
|
|
53
|
+
return NextResponse.json({ ok: true, colors: file.colors });
|
|
54
|
+
} catch (err) {
|
|
55
|
+
return NextResponse.json(
|
|
56
|
+
{ ok: false, error: (err as Error).message },
|
|
57
|
+
{ status: 500 },
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function POST(request: Request) {
|
|
63
|
+
if (process.env.NODE_ENV !== "development") {
|
|
64
|
+
return NextResponse.json({ ok: false, error: "dev-only" }, { status: 403 });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let op: Op;
|
|
68
|
+
try {
|
|
69
|
+
op = (await request.json()) as Op;
|
|
70
|
+
} catch {
|
|
71
|
+
return NextResponse.json({ ok: false, error: "Invalid JSON" }, { status: 400 });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let file: ColorsFile;
|
|
75
|
+
try {
|
|
76
|
+
file = await readFile();
|
|
77
|
+
} catch (err) {
|
|
78
|
+
return NextResponse.json(
|
|
79
|
+
{ ok: false, error: `Could not read brand-colors.json: ${(err as Error).message}` },
|
|
80
|
+
{ status: 500 },
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ─── pin: append a new entry (or replace if id collides) ─────────────────
|
|
85
|
+
if (op.action === "pin") {
|
|
86
|
+
const e = op.entry;
|
|
87
|
+
if (!e || !isValidId(e.id) || !isValidHex(e.primary) || !e.label?.trim()) {
|
|
88
|
+
return NextResponse.json(
|
|
89
|
+
{ ok: false, error: "Invalid entry — need id, label, valid primary hex" },
|
|
90
|
+
{ status: 400 },
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (e.primaryForeground && !isValidHex(e.primaryForeground)) {
|
|
94
|
+
return NextResponse.json(
|
|
95
|
+
{ ok: false, error: "Invalid primaryForeground hex" },
|
|
96
|
+
{ status: 400 },
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const cleaned: BrandColorEntry = {
|
|
100
|
+
id: e.id,
|
|
101
|
+
label: e.label.trim(),
|
|
102
|
+
primary: e.primary,
|
|
103
|
+
...(e.primaryForeground ? { primaryForeground: e.primaryForeground } : {}),
|
|
104
|
+
...(e.note ? { note: e.note } : {}),
|
|
105
|
+
};
|
|
106
|
+
const existing = file.colors.findIndex((c) => c.id === cleaned.id);
|
|
107
|
+
if (existing >= 0) {
|
|
108
|
+
file.colors[existing] = cleaned;
|
|
109
|
+
} else {
|
|
110
|
+
file.colors.push(cleaned);
|
|
111
|
+
}
|
|
112
|
+
await writeFile(file);
|
|
113
|
+
return NextResponse.json({ ok: true, colors: file.colors, action: "pin" });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ─── rename: change label of an existing entry ──────────────────────────
|
|
117
|
+
if (op.action === "rename") {
|
|
118
|
+
if (!op.id || !op.label?.trim()) {
|
|
119
|
+
return NextResponse.json(
|
|
120
|
+
{ ok: false, error: "Need id and non-empty label" },
|
|
121
|
+
{ status: 400 },
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const target = file.colors.find((c) => c.id === op.id);
|
|
125
|
+
if (!target) {
|
|
126
|
+
return NextResponse.json(
|
|
127
|
+
{ ok: false, error: `No brand color with id ${op.id}` },
|
|
128
|
+
{ status: 404 },
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
target.label = op.label.trim();
|
|
132
|
+
await writeFile(file);
|
|
133
|
+
return NextResponse.json({ ok: true, colors: file.colors, action: "rename" });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ─── remove: drop an entry ──────────────────────────────────────────────
|
|
137
|
+
if (op.action === "remove") {
|
|
138
|
+
if (!op.id) {
|
|
139
|
+
return NextResponse.json(
|
|
140
|
+
{ ok: false, error: "Need id" },
|
|
141
|
+
{ status: 400 },
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const before = file.colors.length;
|
|
145
|
+
file.colors = file.colors.filter((c) => c.id !== op.id);
|
|
146
|
+
if (file.colors.length === before) {
|
|
147
|
+
return NextResponse.json(
|
|
148
|
+
{ ok: false, error: `No brand color with id ${op.id}` },
|
|
149
|
+
{ status: 404 },
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
await writeFile(file);
|
|
153
|
+
return NextResponse.json({ ok: true, colors: file.colors, action: "remove" });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ─── reorder: full id-list replacement preserving entries ───────────────
|
|
157
|
+
if (op.action === "reorder") {
|
|
158
|
+
if (!Array.isArray(op.ids)) {
|
|
159
|
+
return NextResponse.json({ ok: false, error: "Need ids array" }, { status: 400 });
|
|
160
|
+
}
|
|
161
|
+
const byId = new Map(file.colors.map((c) => [c.id, c]));
|
|
162
|
+
const next: BrandColorEntry[] = [];
|
|
163
|
+
for (const id of op.ids) {
|
|
164
|
+
const entry = byId.get(id);
|
|
165
|
+
if (entry) next.push(entry);
|
|
166
|
+
}
|
|
167
|
+
// Preserve any entries the client didn't include (defensive — shouldn't happen).
|
|
168
|
+
for (const c of file.colors) {
|
|
169
|
+
if (!op.ids.includes(c.id)) next.push(c);
|
|
170
|
+
}
|
|
171
|
+
file.colors = next;
|
|
172
|
+
await writeFile(file);
|
|
173
|
+
return NextResponse.json({ ok: true, colors: file.colors, action: "reorder" });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return NextResponse.json(
|
|
177
|
+
{ ok: false, error: `Unknown action: ${(op as { action?: string }).action}` },
|
|
178
|
+
{ status: 400 },
|
|
179
|
+
);
|
|
180
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
|
|
6
|
+
const execAsync = promisify(exec);
|
|
7
|
+
const ROOT = process.cwd();
|
|
8
|
+
|
|
9
|
+
function isAllowed(rel: string) {
|
|
10
|
+
// Only allow paths inside layer-*/ to prevent traversal abuse.
|
|
11
|
+
const norm = path.normalize(rel);
|
|
12
|
+
if (norm.startsWith("..") || path.isAbsolute(norm)) return false;
|
|
13
|
+
return /^layer-[\w-]+\//.test(norm) && /\.(md|json)$/.test(norm);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function GET(request: Request) {
|
|
17
|
+
if (process.env.NODE_ENV !== "development") {
|
|
18
|
+
return Response.json({ error: "dev-only" }, { status: 403 });
|
|
19
|
+
}
|
|
20
|
+
const url = new URL(request.url);
|
|
21
|
+
const rel = url.searchParams.get("path") ?? "";
|
|
22
|
+
if (!isAllowed(rel)) {
|
|
23
|
+
return Response.json({ error: "invalid path" }, { status: 400 });
|
|
24
|
+
}
|
|
25
|
+
const full = path.join(ROOT, rel);
|
|
26
|
+
if (!fs.existsSync(full)) {
|
|
27
|
+
return Response.json({ error: "not found" }, { status: 404 });
|
|
28
|
+
}
|
|
29
|
+
return Response.json({ path: rel, content: fs.readFileSync(full, "utf8") });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function POST(request: Request) {
|
|
33
|
+
if (process.env.NODE_ENV !== "development") {
|
|
34
|
+
return Response.json({ error: "dev-only" }, { status: 403 });
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const body = (await request.json()) as {
|
|
38
|
+
path: string;
|
|
39
|
+
content: string;
|
|
40
|
+
message?: string;
|
|
41
|
+
commit?: boolean;
|
|
42
|
+
};
|
|
43
|
+
if (!isAllowed(body.path)) {
|
|
44
|
+
return Response.json({ error: "invalid path" }, { status: 400 });
|
|
45
|
+
}
|
|
46
|
+
const full = path.join(ROOT, body.path);
|
|
47
|
+
fs.writeFileSync(full, body.content);
|
|
48
|
+
|
|
49
|
+
if (body.commit !== false) {
|
|
50
|
+
const message = body.message?.trim() || `docs: edit ${body.path}`;
|
|
51
|
+
await execAsync(`git add ${JSON.stringify(body.path)}`, { cwd: ROOT });
|
|
52
|
+
// --allow-empty so saves with no diff don't error; precommit hook still runs.
|
|
53
|
+
try {
|
|
54
|
+
await execAsync(`git commit -m ${JSON.stringify(message)}`, { cwd: ROOT });
|
|
55
|
+
} catch (e) {
|
|
56
|
+
// No diff is fine. Anything else surfaces as an error.
|
|
57
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
58
|
+
if (!/nothing to commit/i.test(msg)) {
|
|
59
|
+
return Response.json({ error: "commit failed", details: msg }, { status: 500 });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return Response.json({ success: true, path: body.path });
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return Response.json(
|
|
67
|
+
{ error: "save failed", details: error instanceof Error ? error.message : String(error) },
|
|
68
|
+
{ status: 500 }
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const ROOT = process.cwd();
|
|
5
|
+
|
|
6
|
+
function listLayers() {
|
|
7
|
+
return fs
|
|
8
|
+
.readdirSync(ROOT)
|
|
9
|
+
.filter((name) => name.startsWith("layer-"))
|
|
10
|
+
.filter((name) => fs.statSync(path.join(ROOT, name)).isDirectory())
|
|
11
|
+
.sort((a, b) => {
|
|
12
|
+
const na = parseInt(a.split("-")[1], 10) || 0;
|
|
13
|
+
const nb = parseInt(b.split("-")[1], 10) || 0;
|
|
14
|
+
return na - nb;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function listFiles(dir: string): string[] {
|
|
19
|
+
const out: string[] = [];
|
|
20
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
21
|
+
const full = path.join(dir, entry);
|
|
22
|
+
const stat = fs.statSync(full);
|
|
23
|
+
if (stat.isDirectory()) {
|
|
24
|
+
out.push(...listFiles(full));
|
|
25
|
+
} else if (/\.(md|json)$/.test(entry)) {
|
|
26
|
+
out.push(path.relative(ROOT, full));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function prettifyLayerName(slug: string) {
|
|
33
|
+
// "layer-2-ethics-responsibility" → "2 · Ethics & Responsibility"
|
|
34
|
+
const [, num, ...rest] = slug.split("-");
|
|
35
|
+
const label = rest.map((w) => w[0].toUpperCase() + w.slice(1)).join(" ");
|
|
36
|
+
return `${num} · ${label.replace(/\bAnd\b/g, "&")}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function GET() {
|
|
40
|
+
if (process.env.NODE_ENV !== "development") {
|
|
41
|
+
return Response.json({ error: "dev-only" }, { status: 403 });
|
|
42
|
+
}
|
|
43
|
+
const layers = listLayers().map((slug) => ({
|
|
44
|
+
slug,
|
|
45
|
+
label: prettifyLayerName(slug),
|
|
46
|
+
files: listFiles(path.join(ROOT, slug)).sort(),
|
|
47
|
+
}));
|
|
48
|
+
return Response.json({ layers });
|
|
49
|
+
}
|