@agent-scope/cli 1.20.0 → 1.20.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/dist/index.js CHANGED
@@ -1,11 +1,12 @@
1
- import { existsSync, writeFileSync, mkdirSync, readFileSync, statSync, appendFileSync, readdirSync, rmSync, createReadStream } from 'fs';
2
- import { resolve, join, extname, dirname } from 'path';
1
+ import { existsSync, writeFileSync, mkdirSync, readFileSync, statSync, appendFileSync, readdirSync, rmSync, createReadStream, constants, watch } from 'fs';
2
+ import { resolve, join, dirname, extname } from 'path';
3
3
  import { generateManifest } from '@agent-scope/manifest';
4
4
  import { SpriteSheetGenerator, safeRender, BrowserPool, ALL_CONTEXT_IDS, contextAxis, stressAxis, ALL_STRESS_IDS, RenderMatrix, SatoriRenderer } from '@agent-scope/render';
5
5
  import { TokenResolver, ComplianceEngine, parseTokenFileSync, ThemeResolver, exportTokens, validateTokenFile, TokenValidationError, TokenParseError, ImpactAnalyzer } from '@agent-scope/tokens';
6
6
  import { Command } from 'commander';
7
7
  import * as esbuild2 from 'esbuild';
8
8
  import { createRequire } from 'module';
9
+ import { mkdir, access, writeFile } from 'fs/promises';
9
10
  import * as readline from 'readline';
10
11
  import { loadTrace, generateTest, getBrowserEntryScript } from '@agent-scope/playwright';
11
12
  import { chromium } from 'playwright';
@@ -13,10 +14,15 @@ import { tmpdir } from 'os';
13
14
  import { createServer } from 'http';
14
15
  import { buildSite } from '@agent-scope/site';
15
16
 
16
- // src/ci/commands.ts
17
- async function buildComponentHarness(filePath, componentName, props, viewportWidth, projectCss, wrapperScript) {
17
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
18
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
19
+ }) : x)(function(x) {
20
+ if (typeof require !== "undefined") return require.apply(this, arguments);
21
+ throw Error('Dynamic require of "' + x + '" is not supported');
22
+ });
23
+ async function buildComponentHarness(filePath, componentName, props, viewportWidth, projectCss, wrapperScript, screenshotPadding = 0) {
18
24
  const bundledScript = await bundleComponentToIIFE(filePath, componentName, props);
19
- return wrapInHtml(bundledScript, viewportWidth, projectCss, wrapperScript);
25
+ return wrapInHtml(bundledScript, viewportWidth, projectCss, wrapperScript, screenshotPadding);
20
26
  }
21
27
  async function bundleComponentToIIFE(filePath, componentName, props) {
22
28
  const propsJson = JSON.stringify(props).replace(/<\/script>/gi, "<\\/script>");
@@ -122,7 +128,7 @@ ${msg}`);
122
128
  }
123
129
  return outputFile.text;
124
130
  }
125
- function wrapInHtml(bundledScript, viewportWidth, projectCss, wrapperScript) {
131
+ function wrapInHtml(bundledScript, viewportWidth, projectCss, wrapperScript, screenshotPadding = 0) {
126
132
  const projectStyleBlock = projectCss != null && projectCss.length > 0 ? `<style id="scope-project-css">
127
133
  ${projectCss.replace(/<\/style>/gi, "<\\/style>")}
128
134
  </style>` : "";
@@ -132,10 +138,17 @@ ${projectCss.replace(/<\/style>/gi, "<\\/style>")}
132
138
  <head>
133
139
  <meta charset="UTF-8" />
134
140
  <meta name="viewport" content="width=${viewportWidth}, initial-scale=1.0" />
141
+ <script>
142
+ // Reset globals that persist on window across page.setContent() calls
143
+ // (document.open/write/close clears the DOM but NOT the JS global scope)
144
+ window.__SCOPE_WRAPPER__ = null;
145
+ window.__SCOPE_RENDER_COMPLETE__ = false;
146
+ window.__SCOPE_RENDER_ERROR__ = null;
147
+ </script>
135
148
  <style>
136
149
  *, *::before, *::after { box-sizing: border-box; }
137
150
  html, body { margin: 0; padding: 0; background: #fff; font-family: system-ui, sans-serif; }
138
- #scope-root { display: inline-block; min-width: 1px; min-height: 1px; }
151
+ #scope-root { display: inline-block; min-width: 1px; min-height: 1px; margin: ${screenshotPadding}px; }
139
152
  </style>
140
153
  ${projectStyleBlock}
141
154
  </head>
@@ -517,22 +530,22 @@ async function getTailwindCompiler(cwd) {
517
530
  from: entryPath,
518
531
  loadStylesheet
519
532
  });
520
- const build3 = result.build.bind(result);
521
- compilerCache = { cwd, build: build3 };
522
- return build3;
533
+ const build4 = result.build.bind(result);
534
+ compilerCache = { cwd, build: build4 };
535
+ return build4;
523
536
  }
524
537
  async function getCompiledCssForClasses(cwd, classes) {
525
- const build3 = await getTailwindCompiler(cwd);
526
- if (build3 === null) return null;
538
+ const build4 = await getTailwindCompiler(cwd);
539
+ if (build4 === null) return null;
527
540
  const deduped = [...new Set(classes)].filter(Boolean);
528
541
  if (deduped.length === 0) return null;
529
- return build3(deduped);
542
+ return build4(deduped);
530
543
  }
531
544
  async function compileGlobalCssFile(cssFilePath, cwd) {
532
- const { existsSync: existsSync16, readFileSync: readFileSync14 } = await import('fs');
545
+ const { existsSync: existsSync18, readFileSync: readFileSync18 } = await import('fs');
533
546
  const { createRequire: createRequire3 } = await import('module');
534
- if (!existsSync16(cssFilePath)) return null;
535
- const raw = readFileSync14(cssFilePath, "utf-8");
547
+ if (!existsSync18(cssFilePath)) return null;
548
+ const raw = readFileSync18(cssFilePath, "utf-8");
536
549
  const needsCompile = /@tailwind|@import\s+['"]tailwindcss/.test(raw);
537
550
  if (!needsCompile) {
538
551
  return raw;
@@ -615,8 +628,17 @@ async function shutdownPool() {
615
628
  }
616
629
  }
617
630
  async function renderComponent(filePath, componentName, props, viewportWidth, viewportHeight) {
631
+ const PAD = 24;
618
632
  const pool = await getPool(viewportWidth, viewportHeight);
619
- const htmlHarness = await buildComponentHarness(filePath, componentName, props, viewportWidth);
633
+ const htmlHarness = await buildComponentHarness(
634
+ filePath,
635
+ componentName,
636
+ props,
637
+ viewportWidth,
638
+ void 0,
639
+ void 0,
640
+ PAD
641
+ );
620
642
  const slot = await pool.acquire();
621
643
  const { page } = slot;
622
644
  try {
@@ -656,7 +678,6 @@ async function renderComponent(filePath, componentName, props, viewportWidth, vi
656
678
  `Component "${componentName}" rendered with zero bounding box \u2014 it may be invisible or not mounted`
657
679
  );
658
680
  }
659
- const PAD = 24;
660
681
  const MIN_W = 320;
661
682
  const MIN_H = 200;
662
683
  const clipX = Math.max(0, boundingBox.x - PAD);
@@ -1001,6 +1022,124 @@ function createCiCommand() {
1001
1022
  }
1002
1023
  );
1003
1024
  }
1025
+ var PLAYWRIGHT_BROWSER_HINTS = [
1026
+ "executable doesn't exist",
1027
+ "browserType.launch",
1028
+ "looks like playwright was just installed or updated",
1029
+ "please run the following command to download new browsers",
1030
+ "could not find chromium"
1031
+ ];
1032
+ var MISSING_DEPENDENCY_HINTS = ["could not resolve", "cannot find module", "module not found"];
1033
+ var REQUIRED_HARNESS_DEPENDENCIES = ["react", "react-dom", "react/jsx-runtime"];
1034
+ function getEffectivePlaywrightBrowsersPath() {
1035
+ const value = process.env.PLAYWRIGHT_BROWSERS_PATH;
1036
+ return typeof value === "string" && value.length > 0 ? value : null;
1037
+ }
1038
+ function getPlaywrightBrowserRemediation(status) {
1039
+ const effectivePath = status?.effectiveBrowserPath ?? getEffectivePlaywrightBrowsersPath();
1040
+ if (effectivePath !== null) {
1041
+ const pathProblem = status?.browserPathExists === false ? "missing" : status?.browserPathWritable === false ? "unwritable" : "unavailable";
1042
+ return `PLAYWRIGHT_BROWSERS_PATH is set to ${effectivePath}, but that browser cache is ${pathProblem}. Unset PLAYWRIGHT_BROWSERS_PATH and run \`bunx playwright install chromium\`, or install and run with the same writable path: \`PLAYWRIGHT_BROWSERS_PATH=${effectivePath} bunx playwright install chromium\`.`;
1043
+ }
1044
+ return "Run `bunx playwright install chromium` in this sandbox, then retry the Scope command.";
1045
+ }
1046
+ function diagnoseScopeError(error, cwd = process.cwd()) {
1047
+ const message = error instanceof Error ? error.message : String(error);
1048
+ const normalized = message.toLowerCase();
1049
+ if (PLAYWRIGHT_BROWSER_HINTS.some((hint) => normalized.includes(hint))) {
1050
+ const browserPath = extractPlaywrightBrowserPath(message);
1051
+ const browserPathHint = browserPath === null ? "" : ` Scope tried to launch Chromium from ${browserPath}.`;
1052
+ return {
1053
+ code: "PLAYWRIGHT_BROWSERS_MISSING",
1054
+ message: "Playwright Chromium is unavailable for Scope browser rendering.",
1055
+ recovery: getPlaywrightBrowserRemediation() + browserPathHint + " Use `scope doctor --json` to verify the browser check passes before rerunning render/site/instrument."
1056
+ };
1057
+ }
1058
+ if (MISSING_DEPENDENCY_HINTS.some((hint) => normalized.includes(hint))) {
1059
+ const packageManager = detectPackageManager(cwd);
1060
+ return {
1061
+ code: "TARGET_PROJECT_DEPENDENCIES_MISSING",
1062
+ message: "The target project's dependencies appear to be missing or incomplete.",
1063
+ recovery: `Run \`${packageManager} install\` in ${cwd}, then rerun \`scope doctor\` and retry the Scope command.`
1064
+ };
1065
+ }
1066
+ return null;
1067
+ }
1068
+ function formatScopeDiagnostic(error, cwd = process.cwd()) {
1069
+ const message = error instanceof Error ? error.message : String(error);
1070
+ const diagnostic = diagnoseScopeError(error, cwd);
1071
+ if (diagnostic === null) return `Error: ${message}`;
1072
+ return `Error [${diagnostic.code}]: ${diagnostic.message}
1073
+ Recovery: ${diagnostic.recovery}
1074
+ Cause: ${message}`;
1075
+ }
1076
+ async function getPlaywrightBrowserStatus(cwd = process.cwd()) {
1077
+ const effectiveBrowserPath = getEffectivePlaywrightBrowsersPath();
1078
+ const executablePath = getPlaywrightChromiumExecutablePath(cwd);
1079
+ const available = executablePath !== null && existsSync(executablePath);
1080
+ const browserPathExists = effectiveBrowserPath === null ? null : existsSync(effectiveBrowserPath);
1081
+ const browserPathWritable = effectiveBrowserPath === null ? null : await isWritableBrowserPath(effectiveBrowserPath);
1082
+ return {
1083
+ effectiveBrowserPath,
1084
+ executablePath,
1085
+ available,
1086
+ browserPathExists,
1087
+ browserPathWritable,
1088
+ remediation: getPlaywrightBrowserRemediation({
1089
+ effectiveBrowserPath,
1090
+ browserPathExists,
1091
+ browserPathWritable
1092
+ })
1093
+ };
1094
+ }
1095
+ function getPlaywrightChromiumExecutablePath(cwd = process.cwd()) {
1096
+ try {
1097
+ const packageJsonPath = __require.resolve("playwright/package.json", { paths: [cwd] });
1098
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
1099
+ if (!packageJson.version) return null;
1100
+ const playwrightPath = __require.resolve("playwright", { paths: [cwd] });
1101
+ const { chromium: chromium5 } = __require(playwrightPath);
1102
+ const executablePath = chromium5?.executablePath?.();
1103
+ if (typeof executablePath !== "string" || executablePath.length === 0) return null;
1104
+ return executablePath;
1105
+ } catch {
1106
+ return null;
1107
+ }
1108
+ }
1109
+ async function isWritableBrowserPath(browserPath) {
1110
+ const candidate = existsSync(browserPath) ? browserPath : dirname(browserPath);
1111
+ try {
1112
+ await access(candidate, constants.W_OK);
1113
+ return true;
1114
+ } catch {
1115
+ return false;
1116
+ }
1117
+ }
1118
+ function detectPackageManager(cwd = process.cwd()) {
1119
+ if (existsSync(join(cwd, "bun.lock")) || existsSync(join(cwd, "bun.lockb"))) return "bun";
1120
+ if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
1121
+ if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
1122
+ return "npm";
1123
+ }
1124
+ function hasLikelyInstalledDependencies(cwd = process.cwd()) {
1125
+ return existsSync(join(cwd, "node_modules"));
1126
+ }
1127
+ function getMissingHarnessDependencies(cwd = process.cwd()) {
1128
+ return REQUIRED_HARNESS_DEPENDENCIES.filter((dependencyName) => {
1129
+ try {
1130
+ __require.resolve(dependencyName, { paths: [cwd] });
1131
+ return false;
1132
+ } catch {
1133
+ return true;
1134
+ }
1135
+ });
1136
+ }
1137
+ function extractPlaywrightBrowserPath(message) {
1138
+ const match = message.match(/Executable doesn't exist at\s+([^\n]+)/i);
1139
+ return match?.[1]?.trim() ?? null;
1140
+ }
1141
+
1142
+ // src/doctor-commands.ts
1004
1143
  function collectSourceFiles(dir) {
1005
1144
  if (!existsSync(dir)) return [];
1006
1145
  const results = [];
@@ -1014,13 +1153,43 @@ function collectSourceFiles(dir) {
1014
1153
  }
1015
1154
  return results;
1016
1155
  }
1156
+ var TAILWIND_CONFIG_FILES = [
1157
+ "tailwind.config.js",
1158
+ "tailwind.config.cjs",
1159
+ "tailwind.config.mjs",
1160
+ "tailwind.config.ts",
1161
+ "postcss.config.js",
1162
+ "postcss.config.cjs",
1163
+ "postcss.config.mjs",
1164
+ "postcss.config.ts"
1165
+ ];
1166
+ function hasTailwindSetup(cwd) {
1167
+ if (TAILWIND_CONFIG_FILES.some((file) => existsSync(resolve(cwd, file)))) {
1168
+ return true;
1169
+ }
1170
+ const packageJsonPath = resolve(cwd, "package.json");
1171
+ if (!existsSync(packageJsonPath)) return false;
1172
+ try {
1173
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
1174
+ return [pkg.dependencies, pkg.devDependencies].some(
1175
+ (deps) => deps && Object.keys(deps).some(
1176
+ (name) => name === "tailwindcss" || name.startsWith("@tailwindcss/")
1177
+ )
1178
+ );
1179
+ } catch {
1180
+ return false;
1181
+ }
1182
+ }
1183
+ function getPlaywrightInstallCommand(effectiveBrowserPath) {
1184
+ return effectiveBrowserPath === null ? "bunx playwright install chromium" : `PLAYWRIGHT_BROWSERS_PATH=${effectiveBrowserPath} bunx playwright install chromium`;
1185
+ }
1017
1186
  function checkConfig(cwd) {
1018
1187
  const configPath = resolve(cwd, "reactscope.config.json");
1019
1188
  if (!existsSync(configPath)) {
1020
1189
  return {
1021
1190
  name: "config",
1022
1191
  status: "error",
1023
- message: "reactscope.config.json not found \u2014 run `scope init`"
1192
+ message: "reactscope.config.json not found \u2014 run `scope init` in the target project root"
1024
1193
  };
1025
1194
  }
1026
1195
  try {
@@ -1068,6 +1237,13 @@ function checkGlobalCss(cwd) {
1068
1237
  }
1069
1238
  }
1070
1239
  if (globalCss.length === 0) {
1240
+ if (!hasTailwindSetup(cwd)) {
1241
+ return {
1242
+ name: "globalCSS",
1243
+ status: "ok",
1244
+ message: "No globalCSS configured \u2014 skipping CSS injection for this non-Tailwind project"
1245
+ };
1246
+ }
1071
1247
  return {
1072
1248
  name: "globalCSS",
1073
1249
  status: "warn",
@@ -1094,7 +1270,7 @@ function checkManifest(cwd) {
1094
1270
  return {
1095
1271
  name: "manifest",
1096
1272
  status: "warn",
1097
- message: "Manifest not found \u2014 run `scope manifest generate`"
1273
+ message: "Manifest not found \u2014 run `scope manifest generate` in the target project root"
1098
1274
  };
1099
1275
  }
1100
1276
  const manifestMtime = statSync(manifestPath).mtimeMs;
@@ -1111,6 +1287,54 @@ function checkManifest(cwd) {
1111
1287
  return { name: "manifest", status: "ok", message: "Manifest present and up to date" };
1112
1288
  }
1113
1289
  var ICONS = { ok: "\u2713", warn: "!", error: "\u2717" };
1290
+ function checkDependencies(cwd) {
1291
+ const packageManager = detectPackageManager(cwd);
1292
+ if (!hasLikelyInstalledDependencies(cwd)) {
1293
+ return {
1294
+ name: "dependencies",
1295
+ status: "error",
1296
+ remediationCode: "TARGET_PROJECT_DEPENDENCIES_MISSING",
1297
+ fixCommand: `${packageManager} install`,
1298
+ message: `node_modules not found \u2014 run \`${packageManager} install\` in ${cwd} before render/site/instrument`
1299
+ };
1300
+ }
1301
+ const missingHarnessDependencies = getMissingHarnessDependencies(cwd);
1302
+ if (missingHarnessDependencies.length > 0) {
1303
+ return {
1304
+ name: "dependencies",
1305
+ status: "error",
1306
+ remediationCode: "TARGET_PROJECT_HARNESS_DEPENDENCIES_MISSING",
1307
+ fixCommand: `${packageManager} install`,
1308
+ message: `Missing React harness dependencies: ${missingHarnessDependencies.join(", ")}. Run \`${packageManager} install\` in ${cwd}, then retry render/site/instrument.`
1309
+ };
1310
+ }
1311
+ return {
1312
+ name: "dependencies",
1313
+ status: "ok",
1314
+ message: "node_modules and React harness dependencies present"
1315
+ };
1316
+ }
1317
+ async function checkPlaywright(cwd) {
1318
+ const status = await getPlaywrightBrowserStatus(cwd);
1319
+ const pathDetails = status.effectiveBrowserPath === null ? "PLAYWRIGHT_BROWSERS_PATH is unset" : `PLAYWRIGHT_BROWSERS_PATH=${status.effectiveBrowserPath}; exists=${status.browserPathExists}; writable=${status.browserPathWritable}`;
1320
+ if (status.available) {
1321
+ return {
1322
+ name: "playwright",
1323
+ status: "ok",
1324
+ message: `Playwright package available (${pathDetails})`
1325
+ };
1326
+ }
1327
+ return {
1328
+ name: "playwright",
1329
+ status: "error",
1330
+ remediationCode: "PLAYWRIGHT_BROWSERS_MISSING",
1331
+ fixCommand: getPlaywrightInstallCommand(status.effectiveBrowserPath),
1332
+ message: `Playwright Chromium unavailable (${pathDetails}) \u2014 ${status.remediation}`
1333
+ };
1334
+ }
1335
+ function collectFixCommands(checks) {
1336
+ return checks.filter((check) => check.status === "error" && check.fixCommand !== void 0).map((check) => check.fixCommand).filter((command, index, commands) => commands.indexOf(command) === index);
1337
+ }
1114
1338
  function formatCheck(check) {
1115
1339
  return ` [${ICONS[check.status]}] ${check.name.padEnd(12)} ${check.message}`;
1116
1340
  }
@@ -1123,6 +1347,8 @@ CHECKS PERFORMED:
1123
1347
  tokens reactscope.tokens.json exists and passes validation
1124
1348
  css globalCSS files referenced in config actually exist
1125
1349
  manifest .reactscope/manifest.json exists and is not stale
1350
+ dependencies node_modules exists in the target project root
1351
+ playwright Playwright browser runtime is available
1126
1352
  (stale = source files modified after last generate)
1127
1353
 
1128
1354
  STATUS LEVELS: ok | warn | error
@@ -1132,20 +1358,34 @@ Run this first whenever renders fail or produce unexpected output.
1132
1358
  Examples:
1133
1359
  scope doctor
1134
1360
  scope doctor --json
1361
+ scope doctor --print-fix-commands
1135
1362
  scope doctor --json | jq '.checks[] | select(.status == "error")'`
1136
- ).option("--json", "Emit structured JSON output", false).action((opts) => {
1363
+ ).option("--json", "Emit structured JSON output", false).option(
1364
+ "--print-fix-commands",
1365
+ "Print deduplicated shell remediation commands for failing checks",
1366
+ false
1367
+ ).action(async (opts) => {
1137
1368
  const cwd = process.cwd();
1138
1369
  const checks = [
1139
1370
  checkConfig(cwd),
1140
1371
  checkTokens(cwd),
1141
1372
  checkGlobalCss(cwd),
1142
- checkManifest(cwd)
1373
+ checkManifest(cwd),
1374
+ checkDependencies(cwd),
1375
+ await checkPlaywright(cwd)
1143
1376
  ];
1144
1377
  const errors = checks.filter((c) => c.status === "error").length;
1145
1378
  const warnings = checks.filter((c) => c.status === "warn").length;
1379
+ const fixCommands = collectFixCommands(checks);
1380
+ if (opts.printFixCommands) {
1381
+ process.stdout.write(`${JSON.stringify({ cwd, fixCommands }, null, 2)}
1382
+ `);
1383
+ if (errors > 0) process.exit(1);
1384
+ return;
1385
+ }
1146
1386
  if (opts.json) {
1147
1387
  process.stdout.write(
1148
- `${JSON.stringify({ passed: checks.length - errors - warnings, warnings, errors, checks }, null, 2)}
1388
+ `${JSON.stringify({ passed: checks.length - errors - warnings, warnings, errors, fixCommands, checks }, null, 2)}
1149
1389
  `
1150
1390
  );
1151
1391
  if (errors > 0) process.exit(1);
@@ -1172,12 +1412,12 @@ Examples:
1172
1412
  }
1173
1413
 
1174
1414
  // src/skill-content.ts
1175
- var SKILL_CONTENT = '# Scope \u2014 Agent Skill\n\n## TLDR\nScope is a React codebase introspection toolkit. Use it to answer questions about component structure, props, context dependencies, side effects, and visual output \u2014 without running the app.\n\n**When to reach for it:** Any task requiring "which components use X", "what props does Y accept", "render Z for visual verification", "does this component depend on a provider", or "what design tokens are in use".\n\n**3-command workflow:**\n```\nscope init # scaffold config + auto-generate manifest\nscope manifest query --context ThemeContext # ask questions about the codebase\nscope render Button # produce a PNG of a component\n```\n\n---\n\n---\n\n## Mental Model\n\nUnderstanding how Scope\'s data flows is the key to using it effectively as an agent.\n\n```\nSource TypeScript files\n \u2193 (ts-morph AST parse)\n manifest.json \u2190 structural facts: props, hooks, contexts, complexity\n \u2193 (esbuild + Playwright)\n renders/*.json \u2190 visual facts: screenshot, computedStyles, dom, a11y\n \u2193 (token engine)\n compliance-styles.json \u2190 audit facts: which CSS values match tokens, which don\'t\n \u2193 (site generator)\n site/ \u2190 human-readable docs combining all of the above\n```\n\nEach layer depends on the previous. If you\'re getting unexpected results, check whether the earlier layers are stale (run `scope doctor` to diagnose).\n\n---\n\n## The Four Subsystems\n\n### 1. Manifest (`scope manifest *`)\nThe manifest is a static analysis snapshot of your TypeScript source. It tells you:\n- What components exist, where they live, and how they\'re exported\n- What props each component accepts (types, defaults, required/optional)\n- What React hooks they call (`detectedHooks`)\n- What contexts they consume (`requiredContexts`) \u2014 must be provided for a render to succeed\n- Whether they compose other components (`composes` / `composedBy`)\n- Their **complexity class** \u2014 `"simple"` or `"complex"` \u2014 which determines the render engine\n\nThe manifest never runs your code. It only reads TypeScript. This means it\'s fast and safe, but it can\'t know about runtime values.\n\n### 2. Render Engine (`scope render *`)\nThe render engine compiles components with esbuild and renders them in Chromium (Playwright). Two paths exist:\n\n| Path | When | Speed | Capability |\n|------|------|-------|------------|\n| **Satori** | `complexityClass: "simple"` | ~8ms | Flexbox only, no JS, no CSS-in-JS |\n| **BrowserPool** | `complexityClass: "complex"` | ~200\u2013800ms | Full DOM, CSS, Tailwind, animations |\n\nMost real-world components route through BrowserPool. Scope defaults to `"complex"` when uncertain (safe fallback).\n\nEach render produces:\n- `screenshot` \u2014 retina-quality PNG (2\xD7 `deviceScaleFactor`; display at CSS px dimensions)\n- `width` / `height` \u2014 CSS pixel dimensions of the component root\n- `computedStyles` \u2014 per-node computed CSS keyed by `#node-0`, `#node-1`, etc.\n- `dom` \u2014 full DOM tree with bounding boxes (BrowserPool only)\n- `accessibility` \u2014 role, aria-name, violation list (BrowserPool only)\n- `renderTimeMs` \u2014 wall-clock render duration\n\n### 3. Scope Files (`.scope.tsx`)\nScope files let you define **named rendering scenarios** for a component alongside it in the source tree. They are the primary way to ensure `render all` produces meaningful screenshots.\n\n```tsx\n// Button.scope.tsx\nimport type { ScopeFile } from \'@agent-scope/cli\';\nimport { Button } from \'./Button\';\n\nexport default {\n default: { variant: \'primary\', children: \'Click me\' },\n ghost: { variant: \'ghost\', children: \'Cancel\' },\n danger: { variant: \'danger\', children: \'Delete\' },\n disabled: { variant: \'primary\', children: \'Disabled\', disabled: true },\n} satisfies ScopeFile<typeof Button>;\n```\n\nKey rules:\n- The file must be named `<ComponentName>.scope.tsx` in the same directory\n- Export a default object where keys are scenario names and values are props\n- `render all` uses the `default` scenario (or first defined) as the primary screenshot\n- If 2+ scenarios exist, `render all` automatically runs a matrix and merges cells into the component JSON\n- Scenarios also feed the interactive Playground in the docs site\n\nWhen a component renders blank with `{}` props, **the fix is usually to create a `.scope.tsx` file** with real props.\n\n### 4. Token Compliance\nThe compliance pipeline:\n1. `scope render all` captures `computedStyles` for every element in every component\n2. These are written to `.reactscope/compliance-styles.json`\n3. The token engine compares each computed CSS value against your `reactscope.tokens.json`\n4. `scope tokens compliance` reports the aggregate on-system percentage\n5. `scope ci` fails if the percentage is below `complianceThreshold` (default 90%)\n\n**On-system** means the value exactly matches a resolved token value. Off-system means it\'s a hardcoded value with no token backing it.\n\n---\n\n## Complexity Classes \u2014 Practical Guide\n\nThe `complexityClass` field determines which render engine runs. Scope auto-detects it, but agents should understand it:\n\n**`"simple"` components:**\n- Pure presentational, flexbox layout only\n- No CSS grid, no absolute/fixed/sticky positioning\n- No CSS animations, transitions, or transforms\n- No `className` values Scope can\'t statically trace (e.g. dynamic Tailwind classes)\n- Renders in ~8ms via Satori (SVG-based, no browser needed)\n\n**`"complex"` components:**\n- Anything using Tailwind (CSS injection required)\n- CSS grid, positioned elements, overflow, z-index\n- Components that read from context at render time\n- Any component Scope isn\'t sure about (conservative default)\n- Renders in ~200\u2013800ms via Playwright BrowserPool\n\nWhen in doubt: complex is always safe. Simple is an optimization.\n\n---\n\n## Required Contexts \u2014 Why Renders Fail\n\nIf `requiredContexts` is non-empty, the component calls `useContext` on one or more contexts. Without a provider, it will either render broken or throw entirely.\n\nTwo ways to fix:\n1. **Provider presets in config** (recommended): add provider names to `reactscope.config.json \u2192 components.wrappers.providers`\n2. **Scope file with wrapper**: wrap the component in a provider in the scenario itself\n\nBuilt-in mocks (always provided): `ThemeContext \u2192 { theme: \'light\' }`, `LocaleContext \u2192 { locale: \'en-US\' }`.\n\n---\n\n## `scope doctor` \u2014 Always Run This First\n\nBefore debugging any render issue, run:\n```bash\nscope doctor\n```\n\nIt checks:\n- `reactscope.config.json` is valid JSON\n- Token file exists and has a `version` field\n- Every path in `globalCSS` resolves on disk\n- Manifest is present and up to date (not stale relative to source)\n\n**If `globalCSS` is empty or missing**: Tailwind styles won\'t apply to renders. Every component will look unstyled. This is the most common footgun. Fix: add your CSS entry file (the one with `@tailwind base; @tailwind components; @tailwind utilities;`) to `globalCSS` in config.\n\n---\n\n## Agent Decision Tree\n\n**"I want to know what props Component X accepts"**\n\u2192 `scope manifest get X --format json | jq \'.props\'`\n\n**"I want to know which components will break if I change a context"**\n\u2192 `scope manifest query --context MyContext --format json`\n\n**"I want to render a component to verify visual output"**\n\u2192 Create a `.scope.tsx` file with real props first, then `scope render X`\n\n**"I want to render all variants of a component"**\n\u2192 Define all variants in `.scope.tsx`, then `scope render all` (auto-matrix)\n\u2192 Or: `scope render matrix X --axes \'variant:primary,secondary,danger\'`\n\n**"I want to audit token compliance"**\n\u2192 `scope render all` first (populates computedStyles), then `scope tokens compliance`\n\n**"Renders look unstyled / blank"**\n\u2192 Run `scope doctor` \u2014 likely missing `globalCSS`\n\u2192 If props are the issue: create/update the `.scope.tsx` file\n\n**"I want to understand blast radius of a token change"**\n\u2192 `scope tokens impact color.primary.500 --new-value \'#0077dd\'`\n\u2192 `scope tokens preview color.primary.500 --new-value \'#0077dd\'` for visual diff\n\n**"I need to set up Scope in a new project"**\n\u2192 `scope init --yes` (auto-detects Tailwind + CSS, generates manifest automatically)\n\u2192 `scope doctor` to validate\n\u2192 Create `.scope.tsx` files for key components\n\u2192 `scope render all`\n\n**"I want to run Scope in CI"**\n\u2192 `scope ci --json --output ci-result.json`\n\u2192 Exit code 0 = pass, non-zero = specific failure type\n\n---\n\n\n## Installation\n\n```bash\nnpm install -g @agent-scope/cli # global\nnpm install --save-dev @agent-scope/cli # per-project\n```\n\nBinary: `scope`\n\n---\n\n## Core Workflow\n\n```\ninit \u2192 manifest generate \u2192 manifest query/get/list \u2192 render \u2192 (token audit) \u2192 ci\n```\n\n- **init**: Scaffold `reactscope.config.json` + token stub, auto-detect framework/globalCSS, **immediately runs `manifest generate`** so you see results right away.\n- **doctor**: Health-check command \u2014 validates config, token file, globalCSS presence, and manifest staleness.\n- **generate**: Parse TypeScript AST and emit `.reactscope/manifest.json`. Run once per codebase change (or automatically via `scope init`).\n- **query / get / list**: Ask structural questions. No network required. Works from manifest alone. Supports filtering by `--collection` and `--internal`.\n- **render**: Produce PNGs of components via esbuild + Playwright (BrowserPool). Requires manifest for file paths. Auto-injects required prop defaults and globalCSS.\n- **token audit**: Validate design tokens via `@scope/tokens` CLI commands (`tokens list`, `tokens compliance`, `tokens impact`, `tokens preview`, `tokens export`).\n- **ci**: Run compliance checks and exit with code 0/1 for CI pipelines. `report pr-comment` posts results to GitHub PRs.\n\n---\n\n## Full CLI Reference\n\n### `scope init`\nScaffold config, detect framework, extract Tailwind tokens, detect globalCSS files, and **automatically run `scope manifest generate`**.\n\n```bash\nscope init\nscope init --force # overwrite existing config\n```\n\nAfter init completes, the manifest is already written \u2014 no manual `scope manifest generate` step needed.\n\n**Tailwind token extraction**: reads `tailwind.config.js`, extracts colors (with nested scale support), spacing, fontFamily, borderRadius. Stored in `reactscope.tokens.json`.\n\n**globalCSS detection**: checks 9 common patterns (`src/styles.css`, `src/index.css`, `app/globals.css`, etc.). Stored in `components.wrappers.globalCSS` in config.\n\n---\n\n### `scope doctor`\nValidate the Scope setup. Exits non-zero on errors, zero on warnings-only.\n\n```bash\nscope doctor\nscope doctor --json\n```\n\nChecks:\n- `config` \u2014 `reactscope.config.json` is valid JSON with required fields\n- `tokens` \u2014 token file is present and has a valid `version` field\n- `globalCSS` \u2014 globalCSS files listed in config exist on disk\n- `manifest` \u2014 manifest exists and is not stale (compares source file mtimes)\n\n```\n$ scope doctor\nScope Doctor\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n [\u2713] config reactscope.config.json valid\n [\u2713] tokens Token file valid\n [\u2713] globalCSS 1 globalCSS file(s) present\n [!] manifest Manifest may be stale \u2014 5 source file(s) modified since last generate\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n 1 warning(s) \u2014 everything works but could be better\n```\n\n---\n\n### `scope capture <url>`\nCapture a live React component tree from a running app URL.\n\n```bash\nscope capture http://localhost:3000\nscope capture http://localhost:3000 --output report.json --pretty\nscope capture http://localhost:3000 --timeout 15000 --wait 2000\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `-o, --output <path>` | string | stdout | Write JSON to file |\n| `--pretty` | bool | false | Pretty-print JSON |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\nOutput (stdout): serialized PageReport JSON (or path when `--output` is set)\n\n---\n\n### `scope tree <url>`\nPrint the React component tree from a live URL.\n\n```bash\nscope tree http://localhost:3000\nscope tree http://localhost:3000 --depth 3 --show-props --show-hooks\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--depth <n>` | number | unlimited | Max depth to display |\n| `--show-props` | bool | false | Include prop names next to components |\n| `--show-hooks` | bool | false | Show hook counts per component |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\n---\n\n### `scope report <url>`\nCapture and print a human-readable summary of a React app.\n\n```bash\nscope report http://localhost:3000\nscope report http://localhost:3000 --json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--json` | bool | false | Emit structured JSON instead of text |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\n---\n\n### `scope report baseline`\nSave a baseline snapshot for future diff comparisons.\n\n```bash\nscope report baseline\nscope report baseline --output baselines/my-baseline.json\n```\n\n---\n\n### `scope report diff`\nDiff the current app state against a saved baseline.\n\n```bash\nscope report diff\nscope report diff --baseline baselines/my-baseline.json\nscope report diff --json\n```\n\n---\n\n### `scope report pr-comment`\nPost a Scope CI report as a GitHub PR comment. Used in CI pipelines via the reusable `scope-ci` workflow.\n\n```bash\nscope report pr-comment --report-path scope-ci-report.json\n```\n\nRequires `GITHUB_TOKEN`, `GITHUB_REPOSITORY`, and `GITHUB_PR_NUMBER` in environment.\n\n---\n\n### `scope manifest generate`\nScan source files and write `.reactscope/manifest.json`.\n\n```bash\nscope manifest generate\nscope manifest generate --root ./packages/ui\nscope manifest generate --include "src/**/*.tsx" --exclude "**/*.test.tsx"\nscope manifest generate --output custom/manifest.json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--root <path>` | string | cwd | Project root directory |\n| `--output <path>` | string | `.reactscope/manifest.json` | Output path |\n| `--include <globs>` | string | `src/**/*.tsx,src/**/*.ts` | Comma-separated include globs |\n| `--exclude <globs>` | string | `**/node_modules/**,...` | Comma-separated exclude globs |\n\n---\n\n### `scope manifest list`\nList all components in the manifest.\n\n```bash\nscope manifest list\nscope manifest list --filter "Button*"\nscope manifest list --format json\nscope manifest list --collection Forms # filter to named collection\nscope manifest list --internal # only internal components\nscope manifest list --no-internal # hide internal components\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--format <fmt>` | `json\\|table` | auto (TTY\u2192table, pipe\u2192json) | Output format |\n| `--filter <glob>` | string | \u2014 | Filter component names by glob |\n| `--collection <name>` | string | \u2014 | Filter to named collection |\n| `--internal` | bool | false | Show only internal components |\n| `--no-internal` | bool | false | Hide internal components |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\nTTY table output (includes COLLECTION and INTERNAL columns):\n```\nNAME FILE COMPLEXITY HOOKS CONTEXTS COLLECTION INTERNAL\n------------ --------------------------- ---------- ----- -------- ---------- --------\nButton src/components/Button.tsx simple 1 0 \u2014 no\nThemeToggle src/components/Toggle.tsx complex 3 1 Forms no\n```\n\n---\n\n### `scope manifest get <name>`\nGet full details of a single component.\n\n```bash\nscope manifest get Button\nscope manifest get Button --format json\n```\n\nJSON output includes `collection` and `internal` fields:\n```json\n{\n "name": "Button",\n "filePath": "src/components/Button.tsx",\n "collection": "Primitives",\n "internal": false,\n ...\n}\n```\n\n---\n\n### `scope manifest query`\nQuery components by attributes.\n\n```bash\nscope manifest query --context ThemeContext\nscope manifest query --hook useEffect\nscope manifest query --complexity complex\nscope manifest query --side-effects\nscope manifest query --has-fetch\nscope manifest query --has-prop <propName>\nscope manifest query --composed-by <ComponentName>\nscope manifest query --internal\nscope manifest query --collection Forms\nscope manifest query --context ThemeContext --format json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--context <name>` | string | \u2014 | Find components consuming a context by name |\n| `--hook <name>` | string | \u2014 | Find components using a specific hook |\n| `--complexity <class>` | `simple\\|complex` | \u2014 | Filter by complexity class |\n| `--side-effects` | bool | false | Any side effects detected |\n| `--has-fetch` | bool | false | Components with fetch calls specifically |\n| `--has-prop <name>` | string | \u2014 | Components that accept a specific prop |\n| `--composed-by <name>` | string | \u2014 | Components rendered inside a specific parent |\n| `--internal` | bool | false | Only internal components |\n| `--collection <name>` | string | \u2014 | Filter to named collection |\n| `--format <fmt>` | `json\\|table` | auto | Output format |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\n---\n\n### `scope render <component>`\nRender a single component to PNG (TTY) or JSON (pipe).\n\n**Auto prop defaults**: if `--props` is omitted, Scope injects sensible defaults so required props don\'t produce blank renders: strings/nodes \u2192 component name, unions \u2192 first value, booleans \u2192 `false`, numbers \u2192 `0`.\n\n**globalCSS auto-injection**: reads `components.wrappers.globalCSS` from config and compiles/injects CSS (supports Tailwind v3 via PostCSS) into the render harness. A warning is printed to stderr if no globalCSS is configured (common cause of unstyled renders).\n\n```bash\nscope render Button\nscope render Button --props \'{"variant":"primary","children":"Click me"}\'\nscope render Button --viewport 375x812\nscope render Button --output button.png\nscope render Button --format json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--props <json>` | string | `{}` | Inline props as JSON string |\n| `--viewport <WxH>` | string | `375x812` | Viewport size |\n| `--theme <name>` | string | \u2014 | Theme name from token system |\n| `-o, --output <path>` | string | \u2014 | Write PNG to specific path |\n| `--format <fmt>` | `png\\|json` | auto (TTY\u2192file, pipe\u2192json) | Output format |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\n---\n\n### `scope render matrix <component>`\nRender across a Cartesian product of prop axes. Accepts both `key:v1,v2` and `{"key":["v1","v2"]}` JSON format for `--axes`.\n\n```bash\nscope render matrix Button --axes \'variant:primary,secondary,danger\'\nscope render matrix Button --axes \'{"variant":["primary","secondary"]}\'\nscope render matrix Button --axes \'variant:primary,secondary size:sm,md,lg\'\nscope render matrix Button --sprite button-matrix.png --format json\n```\n\n---\n\n### `scope render all`\nRender every component in the manifest.\n\n```bash\nscope render all\nscope render all --concurrency 4 --output-dir renders/\n```\n\nHandles imports of CSS files in components (maps to empty loader so styles are injected at page level). SVG and font imports are handled via dataurl loaders.\n\n---\n\n### `scope instrument tree`\nCapture the live React component tree with instrumentation metadata.\n\n```bash\nscope instrument tree http://localhost:3000\nscope instrument tree http://localhost:3000 --depth 5 --show-props\n```\n\n**Implementation note**: uses a fresh `chromium.launch()` + `newContext()` + `newPage()` per call (not BrowserPool), with `addInitScript` called before `setContent` to ensure the Scope runtime is injected at document-start before React loads.\n\n---\n\n### `scope instrument hooks`\nProfile hook execution in live components.\n\n```bash\nscope instrument hooks http://localhost:3000\nscope instrument hooks http://localhost:3000 --component Button\n```\n\n**Implementation note**: requires `addInitScript({ content: getBrowserEntryScript() })` before `setContent` so `__REACT_DEVTOOLS_GLOBAL_HOOK__` is present when React loads its renderer.\n\n---\n\n### `scope instrument profile`\nProfile render performance of live components.\n\n```bash\nscope instrument profile http://localhost:3000\n```\n\n---\n\n### `scope instrument renders`\nRe-render causality analysis \u2014 what triggered each render.\n\n```bash\nscope instrument renders http://localhost:3000\n```\n\n---\n\n### `scope tokens get <name>`\nGet details of a single design token.\n\n### `scope tokens list`\nList all tokens. Token file must have a `version` field (written by `scope init`).\n\n```bash\nscope tokens list\nscope tokens list --type color\nscope tokens list --format json\n```\n\n### `scope tokens search <query>`\nFull-text search across token names/values.\n\n### `scope tokens resolve <value>`\nResolve a CSS value or alias back to its token name.\n\n### `scope tokens validate`\nValidate token file schema.\n\n### `scope tokens compliance`\nCheck rendered components for design token compliance.\n\n```bash\nscope tokens compliance\nscope tokens compliance --threshold 95\n```\n\n### `scope tokens impact <token>`\nAnalyze impact of changing a token \u2014 which components use it.\n\n```bash\nscope tokens impact --token color.primary.500\n```\n\n### `scope tokens preview <token>`\nPreview a token value change visually before committing.\n\n### `scope tokens export`\nExport tokens in multiple formats.\n\n```bash\nscope tokens export --format flat-json\nscope tokens export --format css\nscope tokens export --format scss\nscope tokens export --format ts\nscope tokens export --format tailwind\nscope tokens export --format style-dictionary\n```\n\n**Format aliases** (auto-corrected with "Did you mean?" hint):\n- `json` \u2192 `flat-json`\n- `js` \u2192 `ts`\n- `sass` \u2192 `scss`\n- `tw` \u2192 `tailwind`\n\n---\n\n### `scope ci`\nRun all CI checks (compliance, accessibility, console errors) and exit 0/1.\n\n```bash\nscope ci\nscope ci --json\nscope ci --threshold 90 # compliance threshold (default: 90)\n```\n\n```\n$ scope ci --json\n\u2192 CI passed in 3.2s\n\u2192 Compliance 100.0% >= threshold 90.0% \u2705\n\u2192 Accessibility audit not yet implemented \u2014 skipped \u2705\n\u2192 No console errors detected \u2705\n\u2192 Exit code 0\n```\n\nThe `scope-ci` **reusable GitHub Actions workflow** is available at `.github/workflows/scope-ci.yml` and can be included in any repo\'s CI to run `scope ci` and post results as a PR comment via `scope report pr-comment`.\n\n---\n\n### `scope site build`\nGenerate a static HTML component gallery site from the manifest.\n\n```bash\nscope site build\nscope site build --output ./dist/site\n```\n\n**Collections support**: components are grouped under named collection sections in the sidebar and index grid. Internal components are hidden from the sidebar and card grid but appear in composition detail sections with an `internal` badge.\n\n**Collection display rules**:\n- Sidebar: one section divider per collection + an "Ungrouped" section; internal components excluded\n- Index page: named sections with heading + optional description; internal components excluded\n- Component detail page: Composes/Composed By lists ALL components including internal ones (with subtle badge)\n- Falls back to flat list when no collections configured (backwards-compatible)\n\n### `scope site serve`\nServe the generated site locally.\n\n```bash\nscope site serve\nscope site serve --port 4000\n```\n\n---\n\n## Collections & Internal Components\n\nComponents can be organized into named **collections** and flagged as **internal** (library implementation details not shown in the public gallery).\n\n### Defining collections\n\n**1. TSDoc tag** (highest precedence):\n```tsx\n/**\n * @collection Forms\n */\nexport function Input() { ... }\n```\n\n**2. `.scope.ts` co-located file**:\n```ts\n// Input.scope.ts\nexport const collection = "Forms"\n```\n\n**3. Config-level glob patterns**:\n```json\n// reactscope.config.json\n{\n "collections": [\n { "name": "Forms", "description": "Form inputs and controls", "patterns": ["src/forms/**"] },\n { "name": "Primitives", "patterns": ["src/primitives/**"] }\n ]\n}\n```\n\nResolution precedence: TSDoc `@collection` > `.scope.ts` export > config pattern.\n\n### Flagging internal components\n\n**TSDoc tag**:\n```tsx\n/**\n * @internal\n */\nexport function InternalHelperButton() { ... }\n```\n\n**Config glob patterns**:\n```json\n{\n "internalPatterns": ["src/internal/**", "src/**/*Internal*"]\n}\n```\n\n---\n\n## Manifest Output Schema\n\nFile: `.reactscope/manifest.json`\n\n```typescript\n{\n version: "0.1",\n generatedAt: string, // ISO 8601\n collections: CollectionConfig[], // echoes config.collections, [] when not set\n components: Record<string, ComponentDescriptor>,\n tree: Record<string, { children: string[], parents: string[] }>\n}\n```\n\n### `ComponentDescriptor` fields\n\n| Field | Type | Description |\n|-------|------|-------------|\n| `filePath` | `string` | Relative path from project root to source file |\n| `exportType` | `"named" \\| "default" \\| "none"` | How the component is exported |\n| `displayName` | `string` | `displayName` if set, else function/class name |\n| `collection` | `string?` | Resolved collection name (`undefined` = ungrouped) |\n| `internal` | `boolean` | `true` if flagged as internal (default: `false`) |\n| `props` | `Record<string, PropDescriptor>` | Extracted prop types keyed by prop name |\n| `composes` | `string[]` | Components this one renders in its JSX |\n| `composedBy` | `string[]` | Components that render this one in their JSX |\n| `complexityClass` | `"simple" \\| "complex"` | Render path: simple = Satori-safe, complex = requires BrowserPool |\n| `requiredContexts` | `string[]` | React context names consumed |\n| `detectedHooks` | `string[]` | All hooks called, sorted alphabetically |\n| `sideEffects` | `SideEffects` | Side effect categories detected |\n| `memoized` | `boolean` | Wrapped with `React.memo` |\n| `forwardedRef` | `boolean` | Wrapped with `React.forwardRef` |\n| `hocWrappers` | `string[]` | HOC wrapper names (excluding memo/forwardRef) |\n| `loc` | `{ start: number, end: number }` | Line numbers in source file |\n\n---\n\n## Common Agent Workflows\n\n### Structural queries\n\n```bash\n# Which components use ThemeContext?\nscope manifest query --context ThemeContext\n\n# What props does Button accept?\nscope manifest get Button --format json | jq \'.props\'\n\n# Which components are safe to render without a provider?\nscope manifest query --complexity simple # + check requiredContexts === []\n\n# Show all components with side effects\nscope manifest query --side-effects\n\n# Which components make fetch calls?\nscope manifest query --has-fetch\n\n# Which components use useEffect?\nscope manifest query --hook useEffect\n\n# Which components accept a disabled prop?\nscope manifest query --has-prop disabled\n\n# Which components are composed inside Modal?\nscope manifest query --composed-by Modal\n\n# All components in the Forms collection\nscope manifest list --collection Forms\n\n# All internal components (library implementation details)\nscope manifest list --internal\n\n# Public components only (hide internals)\nscope manifest list --no-internal\n```\n\n### Render workflows\n\n```bash\n# Render Button in all variants (auto-defaults props if not provided)\nscope render matrix Button --axes \'variant:primary,secondary,danger\'\n\n# Render with JSON axes format\nscope render matrix Button --axes \'{"variant":["primary","secondary"]}\'\n\n# Render with explicit props\nscope render Button --props \'{"variant":"primary","disabled":true}\'\n\n# Render all components (handles CSS/SVG/font imports automatically)\nscope render all --concurrency 8\n\n# Get render as JSON\nscope render Button --format json | jq \'.screenshot\' | base64 -d > button.png\n```\n\n### Token workflows\n\n```bash\n# List all tokens\nscope tokens list\n\n# Check compliance\nscope tokens compliance --threshold 95\n\n# See what a token change impacts\nscope tokens impact --token color.primary.500\n\n# Export for Tailwind\nscope tokens export --format tailwind\n```\n\n### CI workflow\n\n```bash\n# Full compliance check\nscope ci --json\n\n# In GitHub Actions \u2014 use the reusable workflow\n# .github/workflows/ci.yml:\n# uses: FlatFilers/Scope/.github/workflows/scope-ci.yml@main\n```\n\n---\n\n## Error Patterns\n\n| Error | Cause | Fix |\n|-------|-------|-----|\n| `"React root not found"` | App not running, wrong URL, or Vite HMR interfering | Use `scope capture --wait 2000` |\n| `"Component not in manifest"` | Manifest is stale | Run `scope manifest generate` first |\n| `"Manifest not found"` | Missing manifest | Run `scope init` or `scope manifest generate` |\n| `"requiredContexts missing"` | Component needs a provider | Add provider presets to `reactscope.config.json` |\n| Blank PNG / 16\xD76px renders | No globalCSS injected (common with Tailwind) | Set `components.wrappers.globalCSS` in config; run `scope doctor` to verify |\n| `"Invalid props JSON"` | Malformed JSON in `--props` | Use single outer quotes: `--props \'{"key":"val"}\'` |\n| `"SCOPE_CAPTURE_JSON not available"` | Scope runtime not injected before React loaded | Fixed in PR #83 \u2014 update CLI |\n| `"No React DevTools hook found"` | Hook instrumentation init order bug | Fixed in PR #83 \u2014 update CLI |\n| `"ERR_MODULE_NOT_FOUND"` after tokens commands | Old Node shebang in CLI binary | Fixed in PR #90 \u2014 CLI now uses `#!/usr/bin/env bun` |\n| `"version" field missing in tokens` | Token stub written by old `scope init` | Re-run `scope init --force` or add `"version": "1"` to token file |\n| `"unknown option --has-prop"` | Old CLI version | Fixed in PR #90 \u2014 update CLI |\n| Format alias error (`json`, `js`, `sass`, `tw`) | Wrong format name for `tokens export` | Use `flat-json`, `ts`, `scss`, `tailwind`; CLI shows "Did you mean?" hint |\n\n---\n\n## `reactscope.config.json`\n\n```json\n{\n "components": {\n "wrappers": {\n "globalCSS": ["src/styles.css"]\n }\n },\n "tokens": {\n "file": "reactscope.tokens.json"\n },\n "collections": [\n { "name": "Forms", "description": "Form inputs and controls", "patterns": ["src/forms/**"] },\n { "name": "Primitives", "patterns": ["src/primitives/**"] }\n ],\n "internalPatterns": ["src/internal/**"],\n "providers": {\n "theme": { "component": "ThemeProvider", "props": { "theme": "light" } },\n "router": { "component": "MemoryRouter", "props": { "initialEntries": ["/"] } }\n }\n}\n```\n\n**Built-in mock providers** (always available, no config needed):\n- `ThemeContext` \u2192 `{ theme: \'light\' }` (or `--theme <name>`)\n- `LocaleContext` \u2192 `{ locale: \'en-US\' }`\n\n---\n\n## What Scope Cannot Do\n\n- **Runtime state**: `useState` values after user interaction\n- **Network requests**: `fetch`, `XHR`, `WebSocket`\n- **User interactions**: click, type, hover, drag\n- **Auth/session-gated components**: components that redirect or throw without a session\n- **Server components (RSC)**: React Server Components\n- **Dynamic CSS**: CSS-in-JS styles computed at runtime from props Scope can\'t infer\n\n---\n\n## Version History\n\n| Version | Date | Summary |\n|---------|------|---------|\n| v1.0 | 2026-03-11 | Initial SKILL.md (PR #36) \u2014 manifest, render, capture, tree, report, tokens, ci commands |\n| v1.1 | 2026-03-11 | Updated through PR #82 \u2014 Phase 2 CLI commands complete |\n| v1.2 | 2026-03-13 | PRs #83\u2013#95: runtime injection fix, dogfooding fixes (12 bugs), `scope doctor`, `scope init` auto-manifest, globalCSS render warning, collections & internal components feature |\n';
1415
+ var SKILL_CONTENT = '# Scope \u2014 Agent Skill\n\n## TLDR\nScope is a React codebase introspection toolkit. Use it to answer questions about component structure, props, context dependencies, side effects, and visual output \u2014 without running the app.\n\n**When to reach for it:** Any task requiring "which components use X", "what props does Y accept", "render Z for visual verification", "does this component depend on a provider", or "what design tokens are in use".\n\n**Canonical agent workflow:**\n```\nscope get-skill > /tmp/scope-skill.md # bootstrap command semantics into agent context\nscope init --yes # scaffold config + auto-generate manifest\nscope doctor --json # validate config, tokens, globalCSS, manifest freshness, deps, Playwright\nscope manifest query --context ThemeContext --format json\nscope render all --format json --output-dir .reactscope/renders\nscope site build --output .reactscope/site\n```\n\n---\n\n---\n\n## Mental Model\n\nUnderstanding how Scope\'s data flows is the key to using it effectively as an agent.\n\n```\nSource TypeScript files\n \u2193 (ts-morph AST parse)\n manifest.json \u2190 structural facts: props, hooks, contexts, complexity\n \u2193 (esbuild + Playwright)\n renders/*.json \u2190 visual facts: screenshot, computedStyles, dom, a11y\n \u2193 (token engine)\n compliance-styles.json \u2190 audit facts: which CSS values match tokens, which don\'t\n \u2193 (site generator)\n site/ \u2190 human-readable docs combining all of the above\n```\n\nEach layer depends on the previous. If you\'re getting unexpected results, check whether the earlier layers are stale (run `scope doctor` to diagnose).\n\n---\n\n## The Four Subsystems\n\n### 1. Manifest (`scope manifest *`)\nThe manifest is a static analysis snapshot of your TypeScript source. It tells you:\n- What components exist, where they live, and how they\'re exported\n- What props each component accepts (types, defaults, required/optional)\n- What React hooks they call (`detectedHooks`)\n- What contexts they consume (`requiredContexts`) \u2014 must be provided for a render to succeed\n- Whether they compose other components (`composes` / `composedBy`)\n- Their **complexity class** \u2014 `"simple"` or `"complex"` \u2014 which determines the render engine\n\nThe manifest never runs your code. It only reads TypeScript. This means it\'s fast and safe, but it can\'t know about runtime values.\n\n### 2. Render Engine (`scope render *`)\nThe render engine compiles components with esbuild and renders them in Chromium (Playwright). Two paths exist:\n\n| Path | When | Speed | Capability |\n|------|------|-------|------------|\n| **Satori** | `complexityClass: "simple"` | ~8ms | Flexbox only, no JS, no CSS-in-JS |\n| **BrowserPool** | `complexityClass: "complex"` | ~200\u2013800ms | Full DOM, CSS, Tailwind, animations |\n\nMost real-world components route through BrowserPool. Scope defaults to `"complex"` when uncertain (safe fallback).\n\nEach render produces:\n- `screenshot` \u2014 retina-quality PNG (2\xD7 `deviceScaleFactor`; display at CSS px dimensions)\n- `width` / `height` \u2014 CSS pixel dimensions of the component root\n- `computedStyles` \u2014 per-node computed CSS keyed by `#node-0`, `#node-1`, etc.\n- `dom` \u2014 full DOM tree with bounding boxes (BrowserPool only)\n- `accessibility` \u2014 role, aria-name, violation list (BrowserPool only)\n- `renderTimeMs` \u2014 wall-clock render duration\n\n### 3. Scope Files (`.scope.tsx` / `.scope.ts`)\nScope files define **named rendering scenarios** and optional provider wrappers next to a component. They are the primary way to ensure `render all` produces meaningful screenshots.\n\nDiscovery is deterministic: for `Button.tsx`, Scope checks `Button.scope.tsx`, then `.scope.ts`, `.scope.jsx`, `.scope.js` in the same directory and uses the first file that exists.\n\n```tsx\n// Button.scope.tsx\nexport const scenarios = {\n default: { variant: \'primary\', children: \'Click me\' },\n ghost: { variant: \'ghost\', children: \'Cancel\' },\n danger: { variant: \'danger\', children: \'Delete\' },\n disabled: { variant: \'primary\', children: \'Disabled\', disabled: true },\n} satisfies Record<string, Record<string, unknown>>;\n```\n\nOptional wrapper:\n\n```tsx\nimport type { ReactNode } from \'react\';\nimport { ThemeProvider } from \'../providers/ThemeProvider\';\n\nexport function wrapper({ children }: { children: ReactNode }) {\n return <ThemeProvider theme="dark">{children}</ThemeProvider>;\n}\n```\n\nContract:\n- Export `scenarios` as a plain object of scenario-name \u2192 props-object, either as a named export or under `default.scenarios`\n- Export `wrapper` as a function, either as a named export or under `default.wrapper`\n- Non-object scenario values are skipped with a warning\n- If no scope file or no valid scenarios exist, Scope falls back to one bare render and inferred required-prop defaults\n- If 2+ scenarios exist, `render all` automatically runs a matrix and merges cells into each component JSON\n\nWhen a component renders blank with `{}` props, **the fix is usually to create a `.scope.tsx` file** with real props.\n\n### 4. Token Compliance\nThe compliance pipeline:\n1. `scope render all` captures `computedStyles` for every element in every component\n2. These are written to `.reactscope/compliance-styles.json`\n3. The token engine compares each computed CSS value against your `reactscope.tokens.json`\n4. `scope tokens compliance` reports the aggregate on-system percentage\n5. `scope ci` fails if the percentage is below `complianceThreshold` (default 90%)\n\n**On-system** means the value exactly matches a resolved token value. Off-system means it\'s a hardcoded value with no token backing it.\n\n---\n\n## Complexity Classes \u2014 Practical Guide\n\nThe `complexityClass` field determines which render engine runs. Scope auto-detects it, but agents should understand it:\n\n**`"simple"` components:**\n- Pure presentational, flexbox layout only\n- No CSS grid, no absolute/fixed/sticky positioning\n- No CSS animations, transitions, or transforms\n- No `className` values Scope can\'t statically trace (e.g. dynamic Tailwind classes)\n- Renders in ~8ms via Satori (SVG-based, no browser needed)\n\n**`"complex"` components:**\n- Anything using Tailwind (CSS injection required)\n- CSS grid, positioned elements, overflow, z-index\n- Components that read from context at render time\n- Any component Scope isn\'t sure about (conservative default)\n- Renders in ~200\u2013800ms via Playwright BrowserPool\n\nWhen in doubt: complex is always safe. Simple is an optimization.\n\n---\n\n## Required Contexts \u2014 Why Renders Fail\n\nIf `requiredContexts` is non-empty, the component calls `useContext` on one or more contexts. Without a provider, it will either render broken or throw entirely.\n\nTwo ways to fix:\n1. **Provider presets in config** (recommended): add provider names to `reactscope.config.json \u2192 components.wrappers.providers`\n2. **Scope file with wrapper**: wrap the component in a provider in the scenario itself\n\nBuilt-in mocks (always provided): `ThemeContext \u2192 { theme: \'light\' }`, `LocaleContext \u2192 { locale: \'en-US\' }`.\n\n---\n\n## `scope doctor` \u2014 Always Run This First\n\nBefore debugging any render issue, run:\n```bash\nscope doctor --json\n```\n\nIt checks:\n- `reactscope.config.json` is valid JSON\n- Token file exists and has a `version` field\n- Every path in `globalCSS` resolves on disk\n- Manifest is present and up to date (not stale relative to source)\n- Target-project dependencies are installed (`node_modules` exists)\n- Playwright Chromium is available for render/site/instrument commands\n\n**If `globalCSS` is empty or missing**: Tailwind styles won\'t apply to renders. Every component will look unstyled. This is the most common footgun. Fix: add your CSS entry file (the one with `@tailwind base; @tailwind components; @tailwind utilities;`) to `globalCSS` in config.\n\n---\n\n## Agent Decision Tree\n\n**"I want to know what props Component X accepts"**\n\u2192 `scope manifest get X --format json | jq \'.props\'`\n\n**"I want to know which components will break if I change a context"**\n\u2192 `scope manifest query --context MyContext --format json`\n\n**"I want to render a component to verify visual output"**\n\u2192 Create a `.scope.tsx` file with real props first, then `scope render component X --format json`\n\n**"I want to render all variants of a component"**\n\u2192 Define all variants in `.scope.tsx`, then `scope render all --format json --output-dir .reactscope/renders` (auto-matrix)\n\u2192 Or: `scope render matrix X --axes \'variant:primary,secondary,danger\' --format json`\n\n**"I want to audit token compliance"**\n\u2192 `scope render all` first (populates computedStyles), then `scope tokens compliance`\n\n**"Renders look unstyled / blank"**\n\u2192 Run `scope doctor` \u2014 likely missing `globalCSS`\n\u2192 If props are the issue: create/update the `.scope.tsx` file\n\n**"I want to understand blast radius of a token change"**\n\u2192 `scope tokens impact color.primary.500 --new-value \'#0077dd\'`\n\u2192 `scope tokens preview color.primary.500 --new-value \'#0077dd\'` for visual diff\n\n**"I need to set up Scope in a new project"**\n\u2192 `scope init --yes` (auto-detects Tailwind + CSS, generates manifest automatically)\n\u2192 `scope doctor` to validate\n\u2192 Create `.scope.tsx` files for key components\n\u2192 `scope render all`\n\n**"I want to profile a live SPA"**\n\u2192 Generate a manifest, then `scope instrument profile SearchPage --interaction \'[{"action":"click","target":"button"}]\'`\n\u2192 For auth/timing-heavy apps, follow `docs/profiling-production-spas.md`\n\n**"I want to run Scope in CI"**\n\u2192 `scope ci --json --output ci-result.json`\n\u2192 Exit code 0 = pass, non-zero = specific failure type\n\n---\n\n\n## Installation\n\nPublished package:\n\n```bash\nnpm install -g @agent-scope/cli # global\nnpm install --save-dev @agent-scope/cli # per-project\n```\n\nLocal-from-source quickstart for this repo:\n\n```bash\nbun install\nbun run build\nbunx playwright install chromium\ncd fixtures/tailwind-showcase\nbun install\n../../packages/cli/dist/cli.js init --yes\n../../packages/cli/dist/cli.js doctor --json\n../../packages/cli/dist/cli.js render all --format json --output-dir .reactscope/renders\n../../packages/cli/dist/cli.js site build --output .reactscope/site\n```\n\nBinary: `scope` (published install) or `packages/cli/dist/cli.js` (local build)\n\n---\n\n## Core Workflow\n\n```\nget-skill \u2192 init --yes \u2192 doctor --json \u2192 manifest query/get/list --format json \u2192 render all --format json \u2192 site build \u2192 instrument profile \u2192 ci\n```\n\n- **init**: Scaffold `reactscope.config.json` + token stub, auto-detect framework/globalCSS, **immediately runs `manifest generate`** so you see results right away.\n- **doctor**: Health-check command \u2014 validates config, token file, globalCSS presence, manifest staleness, target-project dependencies, and Playwright Chromium availability.\n- **generate**: Parse TypeScript AST and emit `.reactscope/manifest.json`. Run once per codebase change (or automatically via `scope init`).\n- **query / get / list**: Ask structural questions. No network required. Works from manifest alone. Supports filtering by `--collection` and `--internal`.\n- **render**: Produce PNGs of components via esbuild + Playwright (BrowserPool). Requires manifest for file paths. Auto-injects required prop defaults and globalCSS.\n- **token audit**: Validate design tokens via `@scope/tokens` CLI commands (`tokens list`, `tokens compliance`, `tokens impact`, `tokens preview`, `tokens export`).\n- **ci**: Run compliance checks and exit with code 0/1 for CI pipelines. `report pr-comment` posts results to GitHub PRs.\n\n---\n\n## Full CLI Reference\n\n### `scope init`\nScaffold config, detect framework, extract Tailwind tokens, detect globalCSS files, and **automatically run `scope manifest generate`**.\n\n```bash\nscope init\nscope init --force # overwrite existing config\n```\n\nAfter init completes, the manifest is already written \u2014 no manual `scope manifest generate` step needed.\n\n**Tailwind token extraction**: reads `tailwind.config.js`, extracts colors (with nested scale support), spacing, fontFamily, borderRadius. Stored in `reactscope.tokens.json`.\n\n**globalCSS detection**: checks 9 common patterns (`src/styles.css`, `src/index.css`, `app/globals.css`, etc.). Stored in `components.wrappers.globalCSS` in config.\n\n---\n\n### `scope doctor`\nValidate the Scope setup. Exits non-zero on errors, zero on warnings-only.\n\n```bash\nscope doctor --json\nscope doctor --json\n```\n\nChecks:\n- `config` \u2014 `reactscope.config.json` is valid JSON with required fields\n- `tokens` \u2014 token file is present and has a valid `version` field\n- `globalCSS` \u2014 globalCSS files listed in config exist on disk\n- `manifest` \u2014 manifest exists and is not stale (compares source file mtimes)\n- `dependencies` \u2014 `node_modules` exists in the target project root\n- `playwright` \u2014 Playwright Chromium is available before render/site/instrument\n\nIf `dependencies` fails, run `bun install` (or the package manager detected by your lockfile) in the target project root, then rerun `scope doctor --json`.\n\nIf `playwright` fails, run `bunx playwright install chromium`, then rerun `scope doctor --json` before retrying `scope render component`, `scope render all`, `scope site build`, or `scope instrument ...`.\n\n```\n$ scope doctor --json\nScope Doctor\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n [\u2713] config reactscope.config.json valid\n [\u2713] tokens Token file valid\n [\u2713] globalCSS 1 globalCSS file(s) present\n [!] manifest Manifest may be stale \u2014 5 source file(s) modified since last generate\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n 1 warning(s) \u2014 everything works but could be better\n```\n\n---\n\n### `scope capture <url>`\nCapture a live React component tree from a running app URL.\n\n```bash\nscope capture http://localhost:3000\nscope capture http://localhost:3000 --output report.json --pretty\nscope capture http://localhost:3000 --timeout 15000 --wait 2000\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `-o, --output <path>` | string | stdout | Write JSON to file |\n| `--pretty` | bool | false | Pretty-print JSON |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\nOutput (stdout): serialized PageReport JSON (or path when `--output` is set)\n\n---\n\n### `scope tree <url>`\nPrint the React component tree from a live URL.\n\n```bash\nscope tree http://localhost:3000\nscope tree http://localhost:3000 --depth 3 --show-props --show-hooks\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--depth <n>` | number | unlimited | Max depth to display |\n| `--show-props` | bool | false | Include prop names next to components |\n| `--show-hooks` | bool | false | Show hook counts per component |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\n---\n\n### `scope report <url>`\nCapture and print a human-readable summary of a React app.\n\n```bash\nscope report http://localhost:3000\nscope report http://localhost:3000 --json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--json` | bool | false | Emit structured JSON instead of text |\n| `--timeout <ms>` | number | 10000 | Max wait for React to mount |\n| `--wait <ms>` | number | 0 | Additional wait after page load |\n\n---\n\n### `scope report baseline`\nSave a baseline snapshot for future diff comparisons.\n\n```bash\nscope report baseline\nscope report baseline --output baselines/my-baseline.json\n```\n\n---\n\n### `scope report diff`\nDiff the current app state against a saved baseline.\n\n```bash\nscope report diff\nscope report diff --baseline baselines/my-baseline.json\nscope report diff --json\n```\n\n---\n\n### `scope report pr-comment`\nPost a Scope CI report as a GitHub PR comment. Used in CI pipelines via the reusable `scope-ci` workflow.\n\n```bash\nscope report pr-comment --report-path scope-ci-report.json\n```\n\nRequires `GITHUB_TOKEN`, `GITHUB_REPOSITORY`, and `GITHUB_PR_NUMBER` in environment.\n\n---\n\n### `scope manifest generate`\nScan source files and write `.reactscope/manifest.json`.\n\n```bash\nscope manifest generate\nscope manifest generate --root ./packages/ui\nscope manifest generate --include "src/**/*.tsx" --exclude "**/*.test.tsx"\nscope manifest generate --output custom/manifest.json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--root <path>` | string | cwd | Project root directory |\n| `--output <path>` | string | `.reactscope/manifest.json` | Output path |\n| `--include <globs>` | string | `src/**/*.tsx,src/**/*.ts` | Comma-separated include globs |\n| `--exclude <globs>` | string | `**/node_modules/**,...` | Comma-separated exclude globs |\n\n---\n\n### `scope manifest list`\nList all components in the manifest.\n\n```bash\nscope manifest list\nscope manifest list --filter "Button*"\nscope manifest list --format json\nscope manifest list --collection Forms # filter to named collection\nscope manifest list --internal # only internal components\nscope manifest list --no-internal # hide internal components\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--format <fmt>` | `json\\|table` | auto (TTY\u2192table, pipe\u2192json) | Output format |\n| `--filter <glob>` | string | \u2014 | Filter component names by glob |\n| `--collection <name>` | string | \u2014 | Filter to named collection |\n| `--internal` | bool | false | Show only internal components |\n| `--no-internal` | bool | false | Hide internal components |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\nTTY table output (includes COLLECTION and INTERNAL columns):\n```\nNAME FILE COMPLEXITY HOOKS CONTEXTS COLLECTION INTERNAL\n------------ --------------------------- ---------- ----- -------- ---------- --------\nButton src/components/Button.tsx simple 1 0 \u2014 no\nThemeToggle src/components/Toggle.tsx complex 3 1 Forms no\n```\n\n---\n\n### `scope manifest get <name>`\nGet full details of a single component.\n\n```bash\nscope manifest get Button\nscope manifest get Button --format json\n```\n\nJSON output includes `collection` and `internal` fields:\n```json\n{\n "name": "Button",\n "filePath": "src/components/Button.tsx",\n "collection": "Primitives",\n "internal": false,\n ...\n}\n```\n\n---\n\n### `scope manifest query`\nQuery components by attributes.\n\n```bash\nscope manifest query --context ThemeContext\nscope manifest query --hook useEffect\nscope manifest query --complexity complex\nscope manifest query --side-effects\nscope manifest query --has-fetch\nscope manifest query --has-prop <propName>\nscope manifest query --composed-by <ComponentName>\nscope manifest query --internal\nscope manifest query --collection Forms\nscope manifest query --context ThemeContext --format json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--context <name>` | string | \u2014 | Find components consuming a context by name |\n| `--hook <name>` | string | \u2014 | Find components using a specific hook |\n| `--complexity <class>` | `simple\\|complex` | \u2014 | Filter by complexity class |\n| `--side-effects` | bool | false | Any side effects detected |\n| `--has-fetch` | bool | false | Components with fetch calls specifically |\n| `--has-prop <name>` | string | \u2014 | Components that accept a specific prop |\n| `--composed-by <name>` | string | \u2014 | Components rendered inside a specific parent |\n| `--internal` | bool | false | Only internal components |\n| `--collection <name>` | string | \u2014 | Filter to named collection |\n| `--format <fmt>` | `json\\|table` | auto | Output format |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\n---\n\n### `scope render <component>`\nRender a single component to PNG (TTY) or JSON (pipe).\n\n**Auto prop defaults**: if `--props` is omitted, Scope injects sensible defaults so required props don\'t produce blank renders: strings/nodes \u2192 component name, unions \u2192 first value, booleans \u2192 `false`, numbers \u2192 `0`.\n\n**globalCSS auto-injection**: reads `components.wrappers.globalCSS` from config and compiles/injects CSS (supports Tailwind v3 via PostCSS) into the render harness. A warning is printed to stderr if no globalCSS is configured (common cause of unstyled renders).\n\n```bash\nscope render component Button\nscope render component Button --props \'{"variant":"primary","children":"Click me"}\'\nscope render component Button --viewport 375x812\nscope render component Button --output button.png\nscope render component Button --format json\n```\n\nFlags:\n| Flag | Type | Default | Description |\n|------|------|---------|-------------|\n| `--props <json>` | string | `{}` | Inline props as JSON string |\n| `--viewport <WxH>` | string | `375x812` | Viewport size |\n| `--theme <name>` | string | \u2014 | Theme name from token system |\n| `-o, --output <path>` | string | \u2014 | Write PNG to specific path |\n| `--format <fmt>` | `png\\|json` | auto (TTY\u2192file, pipe\u2192json) | Output format |\n| `--manifest <path>` | string | `.reactscope/manifest.json` | Manifest path |\n\n---\n\n### `scope render matrix <component>`\nRender across a Cartesian product of prop axes. Accepts both `key:v1,v2` and `{"key":["v1","v2"]}` JSON format for `--axes`.\n\n```bash\nscope render matrix Button --axes \'variant:primary,secondary,danger\'\nscope render matrix Button --axes \'{"variant":["primary","secondary"]}\'\nscope render matrix Button --axes \'variant:primary,secondary size:sm,md,lg\'\nscope render matrix Button --sprite button-matrix.png --format json\n```\n\n---\n\n### `scope render all`\nRender every component in the manifest.\n\n```bash\nscope render all\nscope render all --concurrency 4 --output-dir renders/\n```\n\nHandles imports of CSS files in components (maps to empty loader so styles are injected at page level). SVG and font imports are handled via dataurl loaders.\n\n---\n\n### `scope instrument tree`\nCapture the live React component tree with instrumentation metadata.\n\n```bash\nscope instrument tree http://localhost:3000\nscope instrument tree http://localhost:3000 --depth 5 --show-props\n```\n\n**Implementation note**: uses a fresh `chromium.launch()` + `newContext()` + `newPage()` per call (not BrowserPool), with `addInitScript` called before `setContent` to ensure the Scope runtime is injected at document-start before React loads.\n\n---\n\n### `scope instrument hooks`\nProfile hook execution in live components.\n\n```bash\nscope instrument hooks http://localhost:3000\nscope instrument hooks http://localhost:3000 --component Button\n```\n\n**Implementation note**: requires `addInitScript({ content: getBrowserEntryScript() })` before `setContent` so `__REACT_DEVTOOLS_GLOBAL_HOOK__` is present when React loads its renderer.\n\n---\n\n### `scope instrument profile`\nProfile render performance of live components.\n\n```bash\nscope instrument profile SearchPage --interaction \'[{"action":"click","target":"button"}]\'\n```\n\n---\n\n### `scope instrument renders`\nRe-render causality analysis \u2014 what triggered each render.\n\n```bash\nscope instrument renders http://localhost:3000\n```\n\n---\n\n### `scope tokens get <name>`\nGet details of a single design token.\n\n### `scope tokens list`\nList all tokens. Token file must have a `version` field (written by `scope init`).\n\n```bash\nscope tokens list\nscope tokens list --type color\nscope tokens list --format json\n```\n\n### `scope tokens search <query>`\nFull-text search across token names/values.\n\n### `scope tokens resolve <value>`\nResolve a CSS value or alias back to its token name.\n\n### `scope tokens validate`\nValidate token file schema.\n\n### `scope tokens compliance`\nCheck rendered components for design token compliance.\n\n```bash\nscope tokens compliance\nscope tokens compliance --threshold 95\n```\n\n### `scope tokens impact <token>`\nAnalyze impact of changing a token \u2014 which components use it.\n\n```bash\nscope tokens impact --token color.primary.500\n```\n\n### `scope tokens preview <token>`\nPreview a token value change visually before committing.\n\n### `scope tokens export`\nExport tokens in multiple formats.\n\n```bash\nscope tokens export --format flat-json\nscope tokens export --format css\nscope tokens export --format scss\nscope tokens export --format ts\nscope tokens export --format tailwind\nscope tokens export --format style-dictionary\n```\n\n**Format aliases** (auto-corrected with "Did you mean?" hint):\n- `json` \u2192 `flat-json`\n- `js` \u2192 `ts`\n- `sass` \u2192 `scss`\n- `tw` \u2192 `tailwind`\n\n---\n\n### `scope ci`\nRun all CI checks (compliance, accessibility, console errors) and exit 0/1.\n\n```bash\nscope ci\nscope ci --json\nscope ci --threshold 90 # compliance threshold (default: 90)\n```\n\n```\n$ scope ci --json\n\u2192 CI passed in 3.2s\n\u2192 Compliance 100.0% >= threshold 90.0% \u2705\n\u2192 Accessibility audit not yet implemented \u2014 skipped \u2705\n\u2192 No console errors detected \u2705\n\u2192 Exit code 0\n```\n\nThe `scope-ci` **reusable GitHub Actions workflow** is available at `.github/workflows/scope-ci.yml` and can be included in any repo\'s CI to run `scope ci` and post results as a PR comment via `scope report pr-comment`.\n\n---\n\n### `scope site build`\nGenerate a static HTML component gallery site from the manifest.\n\n```bash\nscope site build\nscope site build --output ./dist/site\n```\n\n**Collections support**: components are grouped under named collection sections in the sidebar and index grid. Internal components are hidden from the sidebar and card grid but appear in composition detail sections with an `internal` badge.\n\n**Collection display rules**:\n- Sidebar: one section divider per collection + an "Ungrouped" section; internal components excluded\n- Index page: named sections with heading + optional description; internal components excluded\n- Component detail page: Composes/Composed By lists ALL components including internal ones (with subtle badge)\n- Falls back to flat list when no collections configured (backwards-compatible)\n\n### `scope site serve`\nServe the generated site locally.\n\n```bash\nscope site serve\nscope site serve --port 4000\n```\n\n---\n\n## Collections & Internal Components\n\nComponents can be organized into named **collections** and flagged as **internal** (library implementation details not shown in the public gallery).\n\n### Defining collections\n\n**1. TSDoc tag** (highest precedence):\n```tsx\n/**\n * @collection Forms\n */\nexport function Input() { ... }\n```\n\n**2. `.scope.ts` co-located file**:\n```ts\n// Input.scope.ts\nexport const collection = "Forms"\n```\n\n**3. Config-level glob patterns**:\n```json\n// reactscope.config.json\n{\n "collections": [\n { "name": "Forms", "description": "Form inputs and controls", "patterns": ["src/forms/**"] },\n { "name": "Primitives", "patterns": ["src/primitives/**"] }\n ]\n}\n```\n\nResolution precedence: TSDoc `@collection` > `.scope.ts` export > config pattern.\n\n### Flagging internal components\n\n**TSDoc tag**:\n```tsx\n/**\n * @internal\n */\nexport function InternalHelperButton() { ... }\n```\n\n**Config glob patterns**:\n```json\n{\n "internalPatterns": ["src/internal/**", "src/**/*Internal*"]\n}\n```\n\n---\n\n## Manifest Output Schema\n\nFile: `.reactscope/manifest.json`\n\n```typescript\n{\n version: "0.1",\n generatedAt: string, // ISO 8601\n collections: CollectionConfig[], // echoes config.collections, [] when not set\n components: Record<string, ComponentDescriptor>,\n tree: Record<string, { children: string[], parents: string[] }>\n}\n```\n\n### `ComponentDescriptor` fields\n\n| Field | Type | Description |\n|-------|------|-------------|\n| `filePath` | `string` | Relative path from project root to source file |\n| `exportType` | `"named" \\| "default" \\| "none"` | How the component is exported |\n| `displayName` | `string` | `displayName` if set, else function/class name |\n| `collection` | `string?` | Resolved collection name (`undefined` = ungrouped) |\n| `internal` | `boolean` | `true` if flagged as internal (default: `false`) |\n| `props` | `Record<string, PropDescriptor>` | Extracted prop types keyed by prop name |\n| `composes` | `string[]` | Components this one renders in its JSX |\n| `composedBy` | `string[]` | Components that render this one in their JSX |\n| `complexityClass` | `"simple" \\| "complex"` | Render path: simple = Satori-safe, complex = requires BrowserPool |\n| `requiredContexts` | `string[]` | React context names consumed |\n| `detectedHooks` | `string[]` | All hooks called, sorted alphabetically |\n| `sideEffects` | `SideEffects` | Side effect categories detected |\n| `memoized` | `boolean` | Wrapped with `React.memo` |\n| `forwardedRef` | `boolean` | Wrapped with `React.forwardRef` |\n| `hocWrappers` | `string[]` | HOC wrapper names (excluding memo/forwardRef) |\n| `loc` | `{ start: number, end: number }` | Line numbers in source file |\n\n---\n\n## Common Agent Workflows\n\n### Structural queries\n\n```bash\n# Which components use ThemeContext?\nscope manifest query --context ThemeContext\n\n# What props does Button accept?\nscope manifest get Button --format json | jq \'.props\'\n\n# Which components are safe to render without a provider?\nscope manifest query --complexity simple # + check requiredContexts === []\n\n# Show all components with side effects\nscope manifest query --side-effects\n\n# Which components make fetch calls?\nscope manifest query --has-fetch\n\n# Which components use useEffect?\nscope manifest query --hook useEffect\n\n# Which components accept a disabled prop?\nscope manifest query --has-prop disabled\n\n# Which components are composed inside Modal?\nscope manifest query --composed-by Modal\n\n# All components in the Forms collection\nscope manifest list --collection Forms\n\n# All internal components (library implementation details)\nscope manifest list --internal\n\n# Public components only (hide internals)\nscope manifest list --no-internal\n```\n\n### Render workflows\n\n```bash\n# Render Button in all variants (auto-defaults props if not provided)\nscope render matrix Button --axes \'variant:primary,secondary,danger\'\n\n# Render with JSON axes format\nscope render matrix Button --axes \'{"variant":["primary","secondary"]}\'\n\n# Render with explicit props\nscope render component Button --props \'{"variant":"primary","disabled":true}\'\n\n# Render all components (handles CSS/SVG/font imports automatically)\nscope render all --concurrency 8\n\n# Get render as JSON\nscope render component Button --format json | jq \'.screenshot\' | base64 -d > button.png\n```\n\n### Token workflows\n\n```bash\n# List all tokens\nscope tokens list\n\n# Check compliance\nscope tokens compliance --threshold 95\n\n# See what a token change impacts\nscope tokens impact --token color.primary.500\n\n# Export for Tailwind\nscope tokens export --format tailwind\n```\n\n### CI workflow\n\n```bash\n# Full compliance check\nscope ci --json\n\n# In GitHub Actions \u2014 use the reusable workflow\n# .github/workflows/ci.yml:\n# uses: FlatFilers/Scope/.github/workflows/scope-ci.yml@main\n```\n\n---\n\n## Error Patterns\n\n| Error | Cause | Fix |\n|-------|-------|-----|\n| `"React root not found"` | App not running, wrong URL, or Vite HMR interfering | Use `scope capture --wait 2000` |\n| `"Component not in manifest"` | Manifest is stale | Run `scope manifest generate` first |\n| `"Manifest not found"` | Missing manifest | Run `scope init` or `scope manifest generate` |\n| `"requiredContexts missing"` | Component needs a provider | Add provider presets to `reactscope.config.json` |\n| Blank PNG / 16\xD76px renders | No globalCSS injected (common with Tailwind) | Set `components.wrappers.globalCSS` in config; run `scope doctor` to verify |\n| `PLAYWRIGHT_BROWSERS_MISSING` / `browserType.launch: Executable doesn\'t exist` | Playwright Chromium is not installed in this sandbox | Run `bunx playwright install chromium`, then `scope doctor --json`, then retry render/site/instrument |\n| `TARGET_PROJECT_DEPENDENCIES_MISSING` / `Could not resolve "react"` | Target project dependencies are missing | Run `bun install` (or the detected package manager) in the target project root, then `scope doctor --json` |\n| `"Invalid props JSON"` | Malformed JSON in `--props` | Use single outer quotes: `--props \'{"key":"val"}\'` |\n| `"SCOPE_CAPTURE_JSON not available"` | Scope runtime not injected before React loaded | Fixed in PR #83 \u2014 update CLI |\n| `"No React DevTools hook found"` | Hook instrumentation init order bug | Fixed in PR #83 \u2014 update CLI |\n| `"ERR_MODULE_NOT_FOUND"` after tokens commands | Old Node shebang in CLI binary | Fixed in PR #90 \u2014 CLI now uses `#!/usr/bin/env bun` |\n| `"version" field missing in tokens` | Token stub written by old `scope init` | Re-run `scope init --force` or add `"version": "1"` to token file |\n| `"unknown option --has-prop"` | Old CLI version | Fixed in PR #90 \u2014 update CLI |\n| Format alias error (`json`, `js`, `sass`, `tw`) | Wrong format name for `tokens export` | Use `flat-json`, `ts`, `scss`, `tailwind`; CLI shows "Did you mean?" hint |\n\n---\n\n## `reactscope.config.json`\n\n```json\n{\n "components": {\n "wrappers": {\n "globalCSS": ["src/styles.css"]\n }\n },\n "tokens": {\n "file": "reactscope.tokens.json"\n },\n "collections": [\n { "name": "Forms", "description": "Form inputs and controls", "patterns": ["src/forms/**"] },\n { "name": "Primitives", "patterns": ["src/primitives/**"] }\n ],\n "internalPatterns": ["src/internal/**"],\n "providers": {\n "theme": { "component": "ThemeProvider", "props": { "theme": "light" } },\n "router": { "component": "MemoryRouter", "props": { "initialEntries": ["/"] } }\n }\n}\n```\n\n**Built-in mock providers** (always available, no config needed):\n- `ThemeContext` \u2192 `{ theme: \'light\' }` (or `--theme <name>`)\n- `LocaleContext` \u2192 `{ locale: \'en-US\' }`\n\n---\n\n## What Scope Cannot Do\n\n- **Runtime state**: `useState` values after user interaction\n- **Network requests**: `fetch`, `XHR`, `WebSocket`\n- **User interactions**: click, type, hover, drag\n- **Auth/session-gated components**: components that redirect or throw without a session\n- **Server components (RSC)**: React Server Components\n- **Dynamic CSS**: CSS-in-JS styles computed at runtime from props Scope can\'t infer\n\n---\n\n## Version History\n\n| Version | Date | Summary |\n|---------|------|---------|\n| v1.0 | 2026-03-11 | Initial SKILL.md (PR #36) \u2014 manifest, render, capture, tree, report, tokens, ci commands |\n| v1.1 | 2026-03-11 | Updated through PR #82 \u2014 Phase 2 CLI commands complete |\n| v1.2 | 2026-03-13 | PRs #83\u2013#95: runtime injection fix, dogfooding fixes (12 bugs), `scope doctor`, `scope init` auto-manifest, globalCSS render warning, collections & internal components feature |\n';
1176
1416
 
1177
1417
  // src/get-skill-command.ts
1178
1418
  function createGetSkillCommand() {
1179
1419
  return new Command("get-skill").description(
1180
- 'Print the embedded Scope SKILL.md to stdout.\n\nAgents: pipe this command into your context loader to bootstrap Scope knowledge.\nThe skill covers: when to use each command, config requirements, output format,\nrender engine selection, and common failure modes.\n\nEMBEDDED AT BUILD TIME \u2014 works in any install context (global npm, npx, local).\n\nExamples:\n scope get-skill # raw markdown to stdout\n scope get-skill --json # { "skill": "..." } for structured ingestion\n scope get-skill | head -50 # preview the skill\n scope get-skill > /tmp/SKILL.md # save locally'
1420
+ 'Print the embedded Scope SKILL.md to stdout.\n\nAgents: load this first, then follow the canonical workflow in the skill:\n init --yes \u2192 doctor --json \u2192 manifest --format json \u2192 render all --format json \u2192 site build\nThe skill documents .scope.tsx discovery/exports, config paths, profiler usage,\nand failure recovery.\n\nEMBEDDED AT BUILD TIME \u2014 works in any install context (global npm, npx, local).\n\nExamples:\n scope get-skill # raw markdown to stdout\n scope get-skill --json # { "skill": "..." } for structured ingestion\n scope get-skill | head -50 # preview the skill\n scope get-skill > /tmp/SKILL.md # save locally'
1181
1421
  ).option("--json", "Wrap output in JSON { skill: string } instead of raw markdown").action((opts) => {
1182
1422
  if (opts.json) {
1183
1423
  process.stdout.write(`${JSON.stringify({ skill: SKILL_CONTENT }, null, 2)}
@@ -1210,7 +1450,7 @@ function detectFramework(rootDir, packageDeps) {
1210
1450
  if ("react-scripts" in packageDeps) return "cra";
1211
1451
  return "unknown";
1212
1452
  }
1213
- function detectPackageManager(rootDir) {
1453
+ function detectPackageManager2(rootDir) {
1214
1454
  if (existsSync(join(rootDir, "bun.lock"))) return "bun";
1215
1455
  if (existsSync(join(rootDir, "yarn.lock"))) return "yarn";
1216
1456
  if (existsSync(join(rootDir, "pnpm-lock.yaml"))) return "pnpm";
@@ -1286,6 +1526,31 @@ var TAILWIND_STEMS = ["tailwind.config"];
1286
1526
  var CSS_EXTS = [".css", ".scss", ".sass", ".less"];
1287
1527
  var THEME_SUFFIXES = [".theme.ts", ".theme.js", ".theme.tsx"];
1288
1528
  var CSS_CUSTOM_PROPS_RE = /:root\s*\{[^}]*--[a-zA-Z]/;
1529
+ var TAILWIND_V4_THEME_RE = /@theme\s*(?:inline\s*)?\{[^}]*--[a-zA-Z]/;
1530
+ var MAX_SCAN_DEPTH = 4;
1531
+ var SKIP_CSS_NAMES = ["compiled", ".min."];
1532
+ function collectCSSFiles(dir, depth) {
1533
+ if (depth > MAX_SCAN_DEPTH) return [];
1534
+ const results = [];
1535
+ try {
1536
+ const entries = readdirSync(dir, { withFileTypes: true });
1537
+ for (const entry of entries) {
1538
+ if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".next") {
1539
+ continue;
1540
+ }
1541
+ const full = join(dir, entry.name);
1542
+ if (entry.isFile() && CSS_EXTS.some((x) => entry.name.endsWith(x))) {
1543
+ if (!SKIP_CSS_NAMES.some((skip) => entry.name.includes(skip))) {
1544
+ results.push(full);
1545
+ }
1546
+ } else if (entry.isDirectory()) {
1547
+ results.push(...collectCSSFiles(full, depth + 1));
1548
+ }
1549
+ }
1550
+ } catch {
1551
+ }
1552
+ return results;
1553
+ }
1289
1554
  function detectTokenSources(rootDir) {
1290
1555
  const sources = [];
1291
1556
  for (const stem of TAILWIND_STEMS) {
@@ -1301,32 +1566,53 @@ function detectTokenSources(rootDir) {
1301
1566
  }
1302
1567
  }
1303
1568
  const srcDir = join(rootDir, "src");
1304
- const dirsToScan = existsSync(srcDir) ? [srcDir] : [];
1305
- for (const scanDir of dirsToScan) {
1306
- try {
1307
- const entries = readdirSync(scanDir, { withFileTypes: true });
1308
- for (const entry of entries) {
1309
- if (entry.isFile() && CSS_EXTS.some((x) => entry.name.endsWith(x))) {
1310
- const filePath = join(scanDir, entry.name);
1311
- const content = readSafe(filePath);
1312
- if (content !== null && CSS_CUSTOM_PROPS_RE.test(content)) {
1313
- sources.push({ kind: "css-custom-properties", path: filePath });
1314
- }
1315
- }
1569
+ if (existsSync(srcDir)) {
1570
+ const cssFiles = collectCSSFiles(srcDir, 0);
1571
+ const seen = /* @__PURE__ */ new Set();
1572
+ for (const filePath of cssFiles) {
1573
+ const content = readSafe(filePath);
1574
+ if (content === null) continue;
1575
+ if (TAILWIND_V4_THEME_RE.test(content) && !seen.has(filePath)) {
1576
+ sources.push({ kind: "tailwind-v4-theme", path: filePath });
1577
+ seen.add(filePath);
1578
+ }
1579
+ if (CSS_CUSTOM_PROPS_RE.test(content) && !seen.has(filePath)) {
1580
+ sources.push({ kind: "css-custom-properties", path: filePath });
1581
+ seen.add(filePath);
1582
+ }
1583
+ }
1584
+ }
1585
+ for (const tokenDir of ["tokens", "styles", "theme"]) {
1586
+ const dir = join(rootDir, tokenDir);
1587
+ if (!existsSync(dir)) continue;
1588
+ const cssFiles = collectCSSFiles(dir, 0);
1589
+ for (const filePath of cssFiles) {
1590
+ const content = readSafe(filePath);
1591
+ if (content === null) continue;
1592
+ if (TAILWIND_V4_THEME_RE.test(content)) {
1593
+ sources.push({ kind: "tailwind-v4-theme", path: filePath });
1594
+ } else if (CSS_CUSTOM_PROPS_RE.test(content)) {
1595
+ sources.push({ kind: "css-custom-properties", path: filePath });
1316
1596
  }
1317
- } catch {
1318
1597
  }
1319
1598
  }
1320
1599
  if (existsSync(srcDir)) {
1321
- try {
1322
- const entries = readdirSync(srcDir);
1323
- for (const entry of entries) {
1324
- if (THEME_SUFFIXES.some((s) => entry.endsWith(s))) {
1325
- sources.push({ kind: "theme-file", path: join(srcDir, entry) });
1600
+ const scanThemeFiles = (dir, depth) => {
1601
+ if (depth > MAX_SCAN_DEPTH) return;
1602
+ try {
1603
+ const entries = readdirSync(dir, { withFileTypes: true });
1604
+ for (const entry of entries) {
1605
+ if (entry.name === "node_modules" || entry.name === "dist") continue;
1606
+ if (entry.isFile() && THEME_SUFFIXES.some((s) => entry.name.endsWith(s))) {
1607
+ sources.push({ kind: "theme-file", path: join(dir, entry.name) });
1608
+ } else if (entry.isDirectory()) {
1609
+ scanThemeFiles(join(dir, entry.name), depth + 1);
1610
+ }
1326
1611
  }
1612
+ } catch {
1327
1613
  }
1328
- } catch {
1329
- }
1614
+ };
1615
+ scanThemeFiles(srcDir, 0);
1330
1616
  }
1331
1617
  return sources;
1332
1618
  }
@@ -1346,7 +1632,7 @@ function detectProject(rootDir) {
1346
1632
  }
1347
1633
  const framework = detectFramework(rootDir, packageDeps);
1348
1634
  const { typescript, tsconfigPath } = detectTypeScript(rootDir);
1349
- const packageManager = detectPackageManager(rootDir);
1635
+ const packageManager = detectPackageManager2(rootDir);
1350
1636
  const componentPatterns = detectComponentPatterns(rootDir, typescript);
1351
1637
  const tokenSources = detectTokenSources(rootDir);
1352
1638
  const globalCSSFiles = detectGlobalCSSFiles(rootDir);
@@ -1397,9 +1683,9 @@ function createRL() {
1397
1683
  });
1398
1684
  }
1399
1685
  async function ask(rl, question) {
1400
- return new Promise((resolve19) => {
1686
+ return new Promise((resolve21) => {
1401
1687
  rl.question(question, (answer) => {
1402
- resolve19(answer.trim());
1688
+ resolve21(answer.trim());
1403
1689
  });
1404
1690
  });
1405
1691
  }
@@ -1861,21 +2147,64 @@ function registerQuery(manifestCmd) {
1861
2147
  }
1862
2148
  );
1863
2149
  }
2150
+ function loadReactScopeConfig(rootDir) {
2151
+ const configPath = resolve(rootDir, "reactscope.config.json");
2152
+ if (!existsSync(configPath)) return null;
2153
+ try {
2154
+ const raw = readFileSync(configPath, "utf-8");
2155
+ const cfg = JSON.parse(raw);
2156
+ const result = {};
2157
+ const components = cfg.components;
2158
+ if (components !== void 0 && typeof components === "object" && components !== null) {
2159
+ if (Array.isArray(components.include)) {
2160
+ result.include = components.include;
2161
+ }
2162
+ if (Array.isArray(components.exclude)) {
2163
+ result.exclude = components.exclude;
2164
+ }
2165
+ }
2166
+ if (Array.isArray(cfg.internalPatterns)) {
2167
+ result.internalPatterns = cfg.internalPatterns;
2168
+ }
2169
+ if (Array.isArray(cfg.collections)) {
2170
+ result.collections = cfg.collections;
2171
+ }
2172
+ const icons = cfg.icons;
2173
+ if (icons !== void 0 && typeof icons === "object" && icons !== null) {
2174
+ if (Array.isArray(icons.patterns)) {
2175
+ result.iconPatterns = icons.patterns;
2176
+ }
2177
+ }
2178
+ return result;
2179
+ } catch {
2180
+ return null;
2181
+ }
2182
+ }
1864
2183
  function registerGenerate(manifestCmd) {
1865
2184
  manifestCmd.command("generate").description(
1866
- 'Scan source files and generate .reactscope/manifest.json.\n\nUses Babel static analysis \u2014 no runtime or bundler required.\nRe-run whenever components are added, removed, or significantly changed.\n\nWHAT IT CAPTURES per component:\n - File path and export name\n - All props with types and default values\n - Hook usage (useState, useEffect, useContext, custom hooks)\n - Side effects (fetch, timers, subscriptions)\n - Complexity class: simple | complex\n - Context dependencies and composed child components\n\nExamples:\n scope manifest generate\n scope manifest generate --root ./packages/ui\n scope manifest generate --include "src/components/**/*.tsx" --exclude "**/*.stories.tsx"\n scope manifest generate --output ./custom-manifest.json'
2185
+ 'Scan source files and generate .reactscope/manifest.json.\n\nUses Babel static analysis \u2014 no runtime or bundler required.\nRe-run whenever components are added, removed, or significantly changed.\n\nReads reactscope.config.json for:\n components.include glob patterns for source files\n components.exclude glob patterns to skip\n internalPatterns globs to flag components as internal\n collections named groups of components\n\nCLI flags (--include, --exclude) override config-file values.\n\nWHAT IT CAPTURES per component:\n - File path and export name\n - All props with types and default values\n - Hook usage (useState, useEffect, useContext, custom hooks)\n - Side effects (fetch, timers, subscriptions)\n - Complexity class: simple | complex\n - Context dependencies and composed child components\n\nExamples:\n scope manifest generate\n scope manifest generate --root ./packages/ui\n scope manifest generate --include "src/components/**/*.tsx" --exclude "**/*.stories.tsx"\n scope manifest generate --output ./custom-manifest.json'
1867
2186
  ).option("--root <path>", "Project root directory (default: cwd)").option("--output <path>", "Output path for manifest.json", MANIFEST_PATH).option("--include <globs>", "Comma-separated glob patterns to include").option("--exclude <globs>", "Comma-separated glob patterns to exclude").action(async (opts) => {
1868
2187
  try {
1869
2188
  const rootDir = resolve(process.cwd(), opts.root ?? ".");
1870
2189
  const outputPath = resolve(process.cwd(), opts.output);
1871
- const include = opts.include?.split(",").map((s) => s.trim());
1872
- const exclude = opts.exclude?.split(",").map((s) => s.trim());
2190
+ const configValues = loadReactScopeConfig(rootDir);
2191
+ const include = opts.include?.split(",").map((s) => s.trim()) ?? configValues?.include;
2192
+ const exclude = opts.exclude?.split(",").map((s) => s.trim()) ?? configValues?.exclude;
1873
2193
  process.stderr.write(`Scanning ${rootDir} for React components...
1874
2194
  `);
1875
2195
  const manifest = await generateManifest({
1876
2196
  rootDir,
1877
2197
  ...include !== void 0 && { include },
1878
- ...exclude !== void 0 && { exclude }
2198
+ ...exclude !== void 0 && { exclude },
2199
+ ...configValues?.internalPatterns !== void 0 && {
2200
+ internalPatterns: configValues.internalPatterns
2201
+ },
2202
+ ...configValues?.collections !== void 0 && {
2203
+ collections: configValues.collections
2204
+ },
2205
+ ...configValues?.iconPatterns !== void 0 && {
2206
+ iconPatterns: configValues.iconPatterns
2207
+ }
1879
2208
  });
1880
2209
  const componentCount = Object.keys(manifest.components).length;
1881
2210
  process.stderr.write(`Found ${componentCount} components.
@@ -2264,7 +2593,7 @@ Available: ${available}`
2264
2593
  process.stdout.write(`${JSON.stringify(result, null, 2)}
2265
2594
  `);
2266
2595
  } catch (err) {
2267
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
2596
+ process.stderr.write(`${formatScopeDiagnostic(err)}
2268
2597
  `);
2269
2598
  process.exit(1);
2270
2599
  }
@@ -2367,13 +2696,11 @@ function buildProfilingCollectScript() {
2367
2696
  // mount commit), it *may* have been wasted if it didn't actually need to re-render.
2368
2697
  // For the initial snapshot we approximate: wastedRenders = max(0, totalCommits - 1) * 0.3
2369
2698
  // This is a heuristic \u2014 real wasted render detection needs shouldComponentUpdate/React.memo tracing.
2370
- var wastedRenders = Math.max(0, Math.round((totalCommits - 1) * uniqueNames.length * 0.3));
2371
-
2372
2699
  return {
2373
2700
  commitCount: totalCommits,
2374
2701
  uniqueComponents: uniqueNames.length,
2375
2702
  componentNames: uniqueNames,
2376
- wastedRenders: wastedRenders,
2703
+ wastedRenders: null,
2377
2704
  layoutTime: window.__scopeLayoutTime || 0,
2378
2705
  paintTime: window.__scopePaintTime || 0,
2379
2706
  layoutShifts: window.__scopeLayoutShifts || { count: 0, score: 0 }
@@ -2421,7 +2748,7 @@ async function replayInteraction(page, steps) {
2421
2748
  }
2422
2749
  function analyzeProfileFlags(totalRenders, wastedRenders, timing, layoutShifts) {
2423
2750
  const flags = /* @__PURE__ */ new Set();
2424
- if (wastedRenders > 0 && wastedRenders / Math.max(1, totalRenders) > 0.3) {
2751
+ if (wastedRenders !== null && wastedRenders > 0 && wastedRenders / Math.max(1, totalRenders) > 0.3) {
2425
2752
  flags.add("WASTED_RENDER");
2426
2753
  }
2427
2754
  if (totalRenders > 10) {
@@ -2492,13 +2819,18 @@ async function runInteractionProfile(componentName, filePath, props, interaction
2492
2819
  };
2493
2820
  const totalRenders = profileData.commitCount ?? 0;
2494
2821
  const uniqueComponents = profileData.uniqueComponents ?? 0;
2495
- const wastedRenders = profileData.wastedRenders ?? 0;
2822
+ const wastedRenders = profileData.wastedRenders ?? null;
2496
2823
  const flags = analyzeProfileFlags(totalRenders, wastedRenders, timing, layoutShifts);
2497
2824
  return {
2498
2825
  component: componentName,
2499
2826
  totalRenders,
2500
2827
  uniqueComponents,
2501
2828
  wastedRenders,
2829
+ wastedRendersHeuristic: {
2830
+ measured: false,
2831
+ value: null,
2832
+ note: "profile.wastedRenders is retained for compatibility but set to null because Scope does not directly measure wasted renders yet."
2833
+ },
2502
2834
  timing,
2503
2835
  layoutShifts,
2504
2836
  flags,
@@ -2573,7 +2905,7 @@ Available: ${available}`
2573
2905
  process.stdout.write(`${JSON.stringify(result, null, 2)}
2574
2906
  `);
2575
2907
  } catch (err) {
2576
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
2908
+ process.stderr.write(`${formatScopeDiagnostic(err)}
2577
2909
  `);
2578
2910
  process.exit(1);
2579
2911
  }
@@ -2918,7 +3250,7 @@ Available: ${available}`
2918
3250
  `);
2919
3251
  }
2920
3252
  } catch (err) {
2921
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
3253
+ process.stderr.write(`${formatScopeDiagnostic(err)}
2922
3254
  `);
2923
3255
  process.exit(1);
2924
3256
  }
@@ -3410,7 +3742,7 @@ Examples:
3410
3742
  }
3411
3743
  } catch (err) {
3412
3744
  await shutdownPool2();
3413
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
3745
+ process.stderr.write(`${formatScopeDiagnostic(err)}
3414
3746
  `);
3415
3747
  process.exit(1);
3416
3748
  }
@@ -3488,6 +3820,54 @@ function writeReportToFile(report, outputPath, pretty) {
3488
3820
  const json = pretty ? JSON.stringify(report, null, 2) : JSON.stringify(report);
3489
3821
  writeFileSync(outputPath, json, "utf-8");
3490
3822
  }
3823
+ var RUN_SUMMARY_PATH = ".reactscope/run-summary.json";
3824
+ function buildNextActions(summary) {
3825
+ const actions = /* @__PURE__ */ new Set();
3826
+ for (const failure of summary.failures) {
3827
+ if (failure.stage === "render" || failure.stage === "matrix") {
3828
+ actions.add(
3829
+ `Inspect ${failure.outputPath ?? ".reactscope/renders"} and add/fix ${failure.component}.scope.tsx scenarios or wrappers.`
3830
+ );
3831
+ } else if (failure.stage === "playground") {
3832
+ actions.add(
3833
+ `Open the generated component page and inspect the playground bundling error for ${failure.component}.`
3834
+ );
3835
+ } else if (failure.stage === "compliance") {
3836
+ actions.add(
3837
+ "Run `scope render all` first, then inspect .reactscope/compliance-styles.json and reactscope.tokens.json."
3838
+ );
3839
+ } else if (failure.stage === "site") {
3840
+ actions.add(
3841
+ "Inspect .reactscope/site output and rerun `scope site build` after fixing render/playground failures."
3842
+ );
3843
+ }
3844
+ }
3845
+ if (summary.compliance && summary.compliance.auditedProperties === 0) {
3846
+ actions.add(
3847
+ "No CSS properties were audited. Verify renders produced computed styles and your token file contains matching token categories."
3848
+ );
3849
+ } else if (summary.compliance && summary.compliance.threshold !== void 0 && summary.compliance.score < summary.compliance.threshold) {
3850
+ actions.add(
3851
+ "Inspect .reactscope/compliance-report.json for off-system values and update tokens or component styles."
3852
+ );
3853
+ }
3854
+ if (actions.size === 0) {
3855
+ actions.add("No follow-up needed. Outputs are ready for inspection.");
3856
+ }
3857
+ return [...actions];
3858
+ }
3859
+ function writeRunSummary(summary, summaryPath = RUN_SUMMARY_PATH) {
3860
+ const outputPath = resolve(process.cwd(), summaryPath);
3861
+ mkdirSync(dirname(outputPath), { recursive: true });
3862
+ const payload = {
3863
+ ...summary,
3864
+ generatedAt: summary.generatedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
3865
+ nextActions: summary.nextActions ?? buildNextActions(summary)
3866
+ };
3867
+ writeFileSync(outputPath, `${JSON.stringify(payload, null, 2)}
3868
+ `, "utf-8");
3869
+ return outputPath;
3870
+ }
3491
3871
  var SCOPE_EXTENSIONS = [".scope.tsx", ".scope.ts", ".scope.jsx", ".scope.js"];
3492
3872
  function findScopeFile(componentFilePath) {
3493
3873
  const dir = dirname(componentFilePath);
@@ -3615,6 +3995,63 @@ function loadGlobalCssFilesFromConfig(cwd) {
3615
3995
  return [];
3616
3996
  }
3617
3997
  }
3998
+ var TAILWIND_CONFIG_FILES2 = [
3999
+ "tailwind.config.js",
4000
+ "tailwind.config.cjs",
4001
+ "tailwind.config.mjs",
4002
+ "tailwind.config.ts",
4003
+ "postcss.config.js",
4004
+ "postcss.config.cjs",
4005
+ "postcss.config.mjs",
4006
+ "postcss.config.ts"
4007
+ ];
4008
+ function shouldWarnForMissingGlobalCss(cwd) {
4009
+ if (TAILWIND_CONFIG_FILES2.some((file) => existsSync(resolve(cwd, file)))) {
4010
+ return true;
4011
+ }
4012
+ const packageJsonPath = resolve(cwd, "package.json");
4013
+ if (!existsSync(packageJsonPath)) return false;
4014
+ try {
4015
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
4016
+ return [pkg.dependencies, pkg.devDependencies].some(
4017
+ (deps) => deps && Object.keys(deps).some(
4018
+ (name) => name === "tailwindcss" || name.startsWith("@tailwindcss/")
4019
+ )
4020
+ );
4021
+ } catch {
4022
+ return false;
4023
+ }
4024
+ }
4025
+ function loadIconPatternsFromConfig(cwd) {
4026
+ const configPath = resolve(cwd, "reactscope.config.json");
4027
+ if (!existsSync(configPath)) return [];
4028
+ try {
4029
+ const raw = readFileSync(configPath, "utf-8");
4030
+ const cfg = JSON.parse(raw);
4031
+ return cfg.icons?.patterns ?? [];
4032
+ } catch {
4033
+ return [];
4034
+ }
4035
+ }
4036
+ function matchGlob2(pattern, value) {
4037
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
4038
+ const regexStr = escaped.replace(/\*\*/g, "\xA7GLOBSTAR\xA7").replace(/\*/g, "[^/]*").replace(/§GLOBSTAR§/g, ".*");
4039
+ return new RegExp(`^${regexStr}$`, "i").test(value);
4040
+ }
4041
+ function isIconComponent(filePath, displayName, patterns) {
4042
+ return patterns.length > 0 && patterns.some((p) => matchGlob2(p, filePath) || matchGlob2(p, displayName));
4043
+ }
4044
+ function formatAggregateRenderFailureJson(componentName, failures, scenarioCount, runSummaryPath) {
4045
+ return {
4046
+ command: `scope render component ${componentName}`,
4047
+ status: "failed",
4048
+ component: componentName,
4049
+ scenarioCount,
4050
+ failureCount: failures.length,
4051
+ failures,
4052
+ runSummaryPath
4053
+ };
4054
+ }
3618
4055
  var MANIFEST_PATH6 = ".reactscope/manifest.json";
3619
4056
  var DEFAULT_OUTPUT_DIR = ".reactscope/renders";
3620
4057
  var _pool3 = null;
@@ -3635,7 +4072,7 @@ async function shutdownPool3() {
3635
4072
  _pool3 = null;
3636
4073
  }
3637
4074
  }
3638
- function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, globalCssFiles = [], projectCwd = process.cwd(), wrapperScript) {
4075
+ function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, globalCssFiles = [], projectCwd = process.cwd(), wrapperScript, iconMode = false) {
3639
4076
  const satori = new SatoriRenderer({
3640
4077
  defaultViewport: { width: viewportWidth, height: viewportHeight }
3641
4078
  });
@@ -3645,13 +4082,15 @@ function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, g
3645
4082
  const startMs = performance.now();
3646
4083
  const pool = await getPool3(viewportWidth, viewportHeight);
3647
4084
  const projectCss = await loadGlobalCss(globalCssFiles, projectCwd);
4085
+ const PAD = 8;
3648
4086
  const htmlHarness = await buildComponentHarness(
3649
4087
  filePath,
3650
4088
  componentName,
3651
4089
  props,
3652
4090
  viewportWidth,
3653
4091
  projectCss ?? void 0,
3654
- wrapperScript
4092
+ wrapperScript,
4093
+ PAD
3655
4094
  );
3656
4095
  const slot = await pool.acquire();
3657
4096
  const { page } = slot;
@@ -3692,17 +4131,28 @@ function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, g
3692
4131
  `Component "${componentName}" rendered with zero bounding box \u2014 it may be invisible or not mounted`
3693
4132
  );
3694
4133
  }
3695
- const PAD = 8;
3696
4134
  const clipX = Math.max(0, boundingBox.x - PAD);
3697
4135
  const clipY = Math.max(0, boundingBox.y - PAD);
3698
4136
  const rawW = boundingBox.width + PAD * 2;
3699
4137
  const rawH = boundingBox.height + PAD * 2;
3700
4138
  const safeW = Math.min(rawW, viewportWidth - clipX);
3701
4139
  const safeH = Math.min(rawH, viewportHeight - clipY);
3702
- const screenshot = await page.screenshot({
3703
- clip: { x: clipX, y: clipY, width: safeW, height: safeH },
3704
- type: "png"
3705
- });
4140
+ let svgContent;
4141
+ let screenshot;
4142
+ if (iconMode) {
4143
+ svgContent = await page.evaluate((sel) => {
4144
+ const root = document.querySelector(sel);
4145
+ const el = root?.firstElementChild;
4146
+ if (!el) return void 0;
4147
+ return el.outerHTML;
4148
+ }, "[data-reactscope-root]") ?? void 0;
4149
+ screenshot = Buffer.alloc(0);
4150
+ } else {
4151
+ screenshot = await page.screenshot({
4152
+ clip: { x: clipX, y: clipY, width: safeW, height: safeH },
4153
+ type: "png"
4154
+ });
4155
+ }
3706
4156
  const STYLE_PROPS = [
3707
4157
  "display",
3708
4158
  "width",
@@ -3825,7 +4275,7 @@ function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, g
3825
4275
  name: a11yInfo.name,
3826
4276
  violations: imgViolations
3827
4277
  };
3828
- return {
4278
+ const renderResult = {
3829
4279
  screenshot,
3830
4280
  width: Math.round(safeW),
3831
4281
  height: Math.round(safeH),
@@ -3834,6 +4284,10 @@ function buildRenderer(filePath, componentName, viewportWidth, viewportHeight, g
3834
4284
  dom,
3835
4285
  accessibility
3836
4286
  };
4287
+ if (iconMode && svgContent) {
4288
+ renderResult.svgContent = svgContent;
4289
+ }
4290
+ return renderResult;
3837
4291
  } finally {
3838
4292
  pool.release(slot);
3839
4293
  }
@@ -3933,7 +4387,7 @@ Available: ${available}`
3933
4387
  const wrapperScript = scopeData?.hasWrapper === true ? await buildWrapperScript(scopeData.filePath) : void 0;
3934
4388
  const scenarios = buildScenarioMap(opts, scopeData);
3935
4389
  const globalCssFiles = loadGlobalCssFilesFromConfig(rootDir);
3936
- if (globalCssFiles.length === 0) {
4390
+ if (globalCssFiles.length === 0 && shouldWarnForMissingGlobalCss(rootDir)) {
3937
4391
  process.stderr.write(
3938
4392
  "warning: No globalCSS files configured. Tailwind/CSS styles will not be applied to renders.\n Add `components.wrappers.globalCSS` to reactscope.config.json\n"
3939
4393
  );
@@ -3952,7 +4406,8 @@ Available: ${available}`
3952
4406
  `
3953
4407
  );
3954
4408
  const fmt2 = resolveSingleFormat(opts.format);
3955
- let anyFailed = false;
4409
+ const failures = [];
4410
+ const outputPaths = [];
3956
4411
  for (const [scenarioName, props2] of Object.entries(scenarios)) {
3957
4412
  const isNamed = scenarioName !== "__default__";
3958
4413
  const label = isNamed ? `${componentName}:${scenarioName}` : componentName;
@@ -3975,7 +4430,14 @@ Available: ${available}`
3975
4430
  process.stderr.write(` Hints: ${hintList}
3976
4431
  `);
3977
4432
  }
3978
- anyFailed = true;
4433
+ failures.push({
4434
+ component: componentName,
4435
+ scenario: isNamed ? scenarioName : void 0,
4436
+ stage: "render",
4437
+ message: outcome.error.message,
4438
+ outputPath: `${DEFAULT_OUTPUT_DIR}/${isNamed ? `${componentName}-${scenarioName}.error.json` : `${componentName}.error.json`}`,
4439
+ hints: outcome.error.heuristicFlags
4440
+ });
3979
4441
  continue;
3980
4442
  }
3981
4443
  const result = outcome.result;
@@ -3983,6 +4445,7 @@ Available: ${available}`
3983
4445
  if (opts.output !== void 0 && !isNamed) {
3984
4446
  const outPath = resolve(process.cwd(), opts.output);
3985
4447
  writeFileSync(outPath, result.screenshot);
4448
+ outputPaths.push(outPath);
3986
4449
  process.stdout.write(
3987
4450
  `\u2713 ${label} \u2192 ${opts.output} (${result.width}\xD7${result.height}, ${result.renderTimeMs.toFixed(0)}ms)
3988
4451
  `
@@ -3997,17 +4460,36 @@ Available: ${available}`
3997
4460
  const outPath = resolve(dir, outFileName);
3998
4461
  writeFileSync(outPath, result.screenshot);
3999
4462
  const relPath = `${DEFAULT_OUTPUT_DIR}/${outFileName}`;
4463
+ outputPaths.push(relPath);
4000
4464
  process.stdout.write(
4001
4465
  `\u2713 ${label} \u2192 ${relPath} (${result.width}\xD7${result.height}, ${result.renderTimeMs.toFixed(0)}ms)
4002
4466
  `
4003
4467
  );
4004
4468
  }
4005
4469
  }
4470
+ const summaryPath = writeRunSummary({
4471
+ command: `scope render ${componentName}`,
4472
+ status: failures.length > 0 ? "failed" : "success",
4473
+ outputPaths,
4474
+ failures
4475
+ });
4476
+ process.stderr.write(`[scope/render] Run summary written to ${summaryPath}
4477
+ `);
4478
+ if (fmt2 === "json" && failures.length > 0) {
4479
+ const aggregateFailure = formatAggregateRenderFailureJson(
4480
+ componentName,
4481
+ failures,
4482
+ Object.keys(scenarios).length,
4483
+ summaryPath
4484
+ );
4485
+ process.stderr.write(`${JSON.stringify(aggregateFailure, null, 2)}
4486
+ `);
4487
+ }
4006
4488
  await shutdownPool3();
4007
- if (anyFailed) process.exit(1);
4489
+ if (failures.length > 0) process.exit(1);
4008
4490
  } catch (err) {
4009
4491
  await shutdownPool3();
4010
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
4492
+ process.stderr.write(`${formatScopeDiagnostic(err)}
4011
4493
  `);
4012
4494
  process.exit(1);
4013
4495
  }
@@ -4168,7 +4650,7 @@ Available: ${available}`
4168
4650
  }
4169
4651
  } catch (err) {
4170
4652
  await shutdownPool3();
4171
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
4653
+ process.stderr.write(`${formatScopeDiagnostic(err)}
4172
4654
  `);
4173
4655
  process.exit(1);
4174
4656
  }
@@ -4186,7 +4668,21 @@ function registerRenderAll(renderCmd) {
4186
4668
  const total = componentNames.length;
4187
4669
  if (total === 0) {
4188
4670
  process.stderr.write("No components found in manifest.\n");
4189
- return;
4671
+ const summaryPath2 = writeRunSummary({
4672
+ command: "scope render all",
4673
+ status: "failed",
4674
+ outputPaths: [],
4675
+ failures: [
4676
+ {
4677
+ component: "*",
4678
+ stage: "render",
4679
+ message: "No components found in manifest; refusing to report a false-green batch render."
4680
+ }
4681
+ ]
4682
+ });
4683
+ process.stderr.write(`[scope/render] Run summary written to ${summaryPath2}
4684
+ `);
4685
+ process.exit(1);
4190
4686
  }
4191
4687
  const concurrency = Math.max(1, parseInt(opts.concurrency, 10) || 4);
4192
4688
  const outputDir = resolve(process.cwd(), opts.outputDir);
@@ -4195,13 +4691,17 @@ function registerRenderAll(renderCmd) {
4195
4691
  process.stderr.write(`Rendering ${total} components (concurrency: ${concurrency})\u2026
4196
4692
  `);
4197
4693
  const results = [];
4694
+ const failures = [];
4695
+ const outputPaths = [];
4198
4696
  const complianceStylesMap = {};
4199
4697
  let completed = 0;
4698
+ const iconPatterns = loadIconPatternsFromConfig(process.cwd());
4200
4699
  const renderOne = async (name) => {
4201
4700
  const descriptor = manifest.components[name];
4202
4701
  if (descriptor === void 0) return;
4203
4702
  const filePath = resolve(rootDir, descriptor.filePath);
4204
4703
  const allCssFiles = loadGlobalCssFilesFromConfig(process.cwd());
4704
+ const isIcon = isIconComponent(descriptor.filePath, name, iconPatterns);
4205
4705
  const scopeData = await loadScopeFileForComponent(filePath);
4206
4706
  const scenarioEntries = scopeData !== null ? Object.entries(scopeData.scenarios) : [];
4207
4707
  const defaultEntry = scenarioEntries.find(([k]) => k === "default") ?? scenarioEntries[0];
@@ -4214,7 +4714,8 @@ function registerRenderAll(renderCmd) {
4214
4714
  812,
4215
4715
  allCssFiles,
4216
4716
  process.cwd(),
4217
- wrapperScript
4717
+ wrapperScript,
4718
+ isIcon
4218
4719
  );
4219
4720
  const outcome = await safeRender(
4220
4721
  () => renderer.renderCell(renderProps, descriptor.complexityClass),
@@ -4251,14 +4752,32 @@ function registerRenderAll(renderCmd) {
4251
4752
  2
4252
4753
  )
4253
4754
  );
4755
+ failures.push({
4756
+ component: name,
4757
+ stage: "render",
4758
+ message: outcome.error.message,
4759
+ outputPath: errPath,
4760
+ hints: outcome.error.heuristicFlags
4761
+ });
4762
+ outputPaths.push(errPath);
4254
4763
  return;
4255
4764
  }
4256
4765
  const result = outcome.result;
4257
4766
  results.push({ name, renderTimeMs: result.renderTimeMs, success: true });
4258
- const pngPath = resolve(outputDir, `${name}.png`);
4259
- writeFileSync(pngPath, result.screenshot);
4767
+ if (!isIcon) {
4768
+ const pngPath = resolve(outputDir, `${name}.png`);
4769
+ writeFileSync(pngPath, result.screenshot);
4770
+ outputPaths.push(pngPath);
4771
+ }
4260
4772
  const jsonPath = resolve(outputDir, `${name}.json`);
4261
- writeFileSync(jsonPath, JSON.stringify(formatRenderJson(name, {}, result), null, 2));
4773
+ const renderJson = formatRenderJson(name, {}, result);
4774
+ const extResult = result;
4775
+ if (isIcon && extResult.svgContent) {
4776
+ renderJson.svgContent = extResult.svgContent;
4777
+ delete renderJson.screenshot;
4778
+ }
4779
+ writeFileSync(jsonPath, JSON.stringify(renderJson, null, 2));
4780
+ outputPaths.push(jsonPath);
4262
4781
  const rawStyles = result.computedStyles["[data-reactscope-root] > *"] ?? {};
4263
4782
  const compStyles = {
4264
4783
  colors: {},
@@ -4324,15 +4843,21 @@ function registerRenderAll(renderCmd) {
4324
4843
  existingJson.axisLabels = [scenarioAxis.values];
4325
4844
  writeFileSync(jsonPath, JSON.stringify(existingJson, null, 2));
4326
4845
  } catch (matrixErr) {
4327
- process.stderr.write(
4328
- ` [warn] Matrix render for ${name} failed: ${matrixErr instanceof Error ? matrixErr.message : String(matrixErr)}
4329
- `
4330
- );
4846
+ const message = matrixErr instanceof Error ? matrixErr.message : String(matrixErr);
4847
+ process.stderr.write(` [warn] Matrix render for ${name} failed: ${message}
4848
+ `);
4849
+ failures.push({
4850
+ component: name,
4851
+ stage: "matrix",
4852
+ message,
4853
+ outputPath: jsonPath
4854
+ });
4331
4855
  }
4332
4856
  }
4333
4857
  if (isTTY()) {
4858
+ const suffix = isIcon ? " [icon/svg]" : "";
4334
4859
  process.stdout.write(
4335
- `\u2713 ${name} \u2192 ${opts.outputDir}/${name}.png (${result.width}\xD7${result.height}, ${result.renderTimeMs.toFixed(0)}ms)
4860
+ `\u2713 ${name} \u2192 ${opts.outputDir}/${name}${isIcon ? ".json" : ".png"} (${result.width}\xD7${result.height}, ${result.renderTimeMs.toFixed(0)}ms)${suffix}
4336
4861
  `
4337
4862
  );
4338
4863
  }
@@ -4359,15 +4884,25 @@ function registerRenderAll(renderCmd) {
4359
4884
  "compliance-styles.json"
4360
4885
  );
4361
4886
  writeFileSync(compStylesPath, JSON.stringify(complianceStylesMap, null, 2));
4887
+ outputPaths.push(compStylesPath);
4362
4888
  process.stderr.write(`[scope/render] \u2713 Wrote compliance-styles.json
4363
4889
  `);
4364
4890
  process.stderr.write("\n");
4365
4891
  const summary = formatSummaryText(results, outputDir);
4366
4892
  process.stderr.write(`${summary}
4367
4893
  `);
4894
+ const summaryPath = writeRunSummary({
4895
+ command: "scope render all",
4896
+ status: failures.length > 0 ? "failed" : "success",
4897
+ outputPaths,
4898
+ failures
4899
+ });
4900
+ process.stderr.write(`[scope/render] Run summary written to ${summaryPath}
4901
+ `);
4902
+ if (failures.length > 0) process.exit(1);
4368
4903
  } catch (err) {
4369
4904
  await shutdownPool3();
4370
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
4905
+ process.stderr.write(`${formatScopeDiagnostic(err)}
4371
4906
  `);
4372
4907
  process.exit(1);
4373
4908
  }
@@ -4427,8 +4962,17 @@ async function shutdownPool4() {
4427
4962
  }
4428
4963
  }
4429
4964
  async function renderComponent2(filePath, componentName, props, viewportWidth, viewportHeight) {
4965
+ const PAD = 24;
4430
4966
  const pool = await getPool4(viewportWidth, viewportHeight);
4431
- const htmlHarness = await buildComponentHarness(filePath, componentName, props, viewportWidth);
4967
+ const htmlHarness = await buildComponentHarness(
4968
+ filePath,
4969
+ componentName,
4970
+ props,
4971
+ viewportWidth,
4972
+ void 0,
4973
+ void 0,
4974
+ PAD
4975
+ );
4432
4976
  const slot = await pool.acquire();
4433
4977
  const { page } = slot;
4434
4978
  try {
@@ -4468,7 +5012,6 @@ async function renderComponent2(filePath, componentName, props, viewportWidth, v
4468
5012
  `Component "${componentName}" rendered with zero bounding box \u2014 it may be invisible or not mounted`
4469
5013
  );
4470
5014
  }
4471
- const PAD = 24;
4472
5015
  const MIN_W = 320;
4473
5016
  const MIN_H = 200;
4474
5017
  const clipX = Math.max(0, boundingBox.x - PAD);
@@ -4560,12 +5103,12 @@ async function runBaseline(options = {}) {
4560
5103
  mkdirSync(rendersDir, { recursive: true });
4561
5104
  let manifest;
4562
5105
  if (manifestPath !== void 0) {
4563
- const { readFileSync: readFileSync14 } = await import('fs');
5106
+ const { readFileSync: readFileSync18 } = await import('fs');
4564
5107
  const absPath = resolve(rootDir, manifestPath);
4565
5108
  if (!existsSync(absPath)) {
4566
5109
  throw new Error(`Manifest not found at ${absPath}.`);
4567
5110
  }
4568
- manifest = JSON.parse(readFileSync14(absPath, "utf-8"));
5111
+ manifest = JSON.parse(readFileSync18(absPath, "utf-8"));
4569
5112
  process.stderr.write(`Loaded manifest from ${manifestPath}
4570
5113
  `);
4571
5114
  } else {
@@ -4757,8 +5300,17 @@ async function shutdownPool5() {
4757
5300
  }
4758
5301
  }
4759
5302
  async function renderComponent3(filePath, componentName, props, viewportWidth, viewportHeight) {
5303
+ const PAD = 24;
4760
5304
  const pool = await getPool5(viewportWidth, viewportHeight);
4761
- const htmlHarness = await buildComponentHarness(filePath, componentName, props, viewportWidth);
5305
+ const htmlHarness = await buildComponentHarness(
5306
+ filePath,
5307
+ componentName,
5308
+ props,
5309
+ viewportWidth,
5310
+ void 0,
5311
+ void 0,
5312
+ PAD
5313
+ );
4762
5314
  const slot = await pool.acquire();
4763
5315
  const { page } = slot;
4764
5316
  try {
@@ -4798,7 +5350,6 @@ async function renderComponent3(filePath, componentName, props, viewportWidth, v
4798
5350
  `Component "${componentName}" rendered with zero bounding box \u2014 it may be invisible or not mounted`
4799
5351
  );
4800
5352
  }
4801
- const PAD = 24;
4802
5353
  const MIN_W = 320;
4803
5354
  const MIN_H = 200;
4804
5355
  const clipX = Math.max(0, boundingBox.x - PAD);
@@ -4895,6 +5446,7 @@ function classifyComponent(entry, regressionThreshold) {
4895
5446
  async function runDiff(options = {}) {
4896
5447
  const {
4897
5448
  baselineDir: baselineDirRaw = DEFAULT_BASELINE_DIR2,
5449
+ complianceTokens = [],
4898
5450
  componentsGlob,
4899
5451
  manifestPath,
4900
5452
  viewportWidth = 375,
@@ -5010,7 +5562,7 @@ async function runDiff(options = {}) {
5010
5562
  if (isTTY() && total > 0) {
5011
5563
  process.stderr.write("\n");
5012
5564
  }
5013
- const resolver = new TokenResolver([]);
5565
+ const resolver = new TokenResolver(complianceTokens);
5014
5566
  const engine = new ComplianceEngine(resolver);
5015
5567
  const currentBatchReport = engine.auditBatch(computedStylesMap);
5016
5568
  const entries = [];
@@ -5606,129 +6158,803 @@ function buildStructuredReport(report) {
5606
6158
  route: report.route?.pattern ?? null
5607
6159
  };
5608
6160
  }
5609
- var MIME_TYPES = {
5610
- ".html": "text/html; charset=utf-8",
5611
- ".css": "text/css; charset=utf-8",
5612
- ".js": "application/javascript; charset=utf-8",
5613
- ".json": "application/json; charset=utf-8",
5614
- ".png": "image/png",
5615
- ".jpg": "image/jpeg",
5616
- ".jpeg": "image/jpeg",
5617
- ".svg": "image/svg+xml",
5618
- ".ico": "image/x-icon"
5619
- };
5620
- function registerBuild(siteCmd) {
5621
- siteCmd.command("build").description(
5622
- 'Build the static HTML site from manifest + render outputs.\n\nINPUT DIRECTORY (.reactscope/ by default) must contain:\n manifest.json component registry\n renders/ screenshots and render.json files from `scope render all`\n\nOPTIONAL:\n --compliance <path> include token compliance scores on detail pages\n --base-path <path> set if deploying to a subdirectory (e.g. /ui-docs)\n\nExamples:\n scope site build\n scope site build --title "Design System" -o .reactscope/site\n scope site build --compliance .reactscope/compliance-styles.json\n scope site build --base-path /ui'
5623
- ).option("-i, --input <path>", "Path to .reactscope input directory", ".reactscope").option("-o, --output <path>", "Output directory for generated site", ".reactscope/site").option("--base-path <path>", "Base URL path prefix for subdirectory deployment", "/").option("--compliance <path>", "Path to compliance batch report JSON").option("--title <text>", "Site title", "Scope \u2014 Component Gallery").action(
5624
- async (opts) => {
5625
- try {
5626
- const inputDir = resolve(process.cwd(), opts.input);
5627
- const outputDir = resolve(process.cwd(), opts.output);
5628
- if (!existsSync(inputDir)) {
5629
- throw new Error(
5630
- `Input directory not found: ${inputDir}
5631
- Run \`scope manifest generate\` and \`scope render\` first.`
5632
- );
5633
- }
5634
- const manifestPath = join(inputDir, "manifest.json");
5635
- if (!existsSync(manifestPath)) {
5636
- throw new Error(
5637
- `Manifest not found at ${manifestPath}
5638
- Run \`scope manifest generate\` first.`
5639
- );
5640
- }
5641
- process.stderr.write(`Building site from ${inputDir}\u2026
5642
- `);
5643
- await buildSite({
5644
- inputDir,
5645
- outputDir,
5646
- basePath: opts.basePath,
5647
- ...opts.compliance !== void 0 && {
5648
- compliancePath: resolve(process.cwd(), opts.compliance)
5649
- },
5650
- title: opts.title
5651
- });
5652
- process.stderr.write(`Site written to ${outputDir}
5653
- `);
5654
- process.stdout.write(`${outputDir}
5655
- `);
5656
- } catch (err) {
5657
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
5658
- `);
5659
- process.exit(1);
5660
- }
6161
+ async function buildPlaygroundHarness(filePath, componentName, projectCss, wrapperScript) {
6162
+ const bundledScript = await bundlePlaygroundIIFE(filePath, componentName);
6163
+ return wrapPlaygroundHtml(bundledScript, projectCss);
6164
+ }
6165
+ async function bundlePlaygroundIIFE(filePath, componentName) {
6166
+ const wrapperCode = (
6167
+ /* ts */
6168
+ `
6169
+ import * as __scopeMod from ${JSON.stringify(filePath)};
6170
+ import { createRoot } from "react-dom/client";
6171
+ import { createElement, Component as ReactComponent } from "react";
6172
+
6173
+ (function scopePlaygroundHarness() {
6174
+ var Target =
6175
+ __scopeMod["default"] ||
6176
+ __scopeMod[${JSON.stringify(componentName)}] ||
6177
+ (Object.values(__scopeMod).find(
6178
+ function(v) { return typeof v === "function" && /^[A-Z]/.test(v.name || ""); }
6179
+ ));
6180
+
6181
+ if (!Target) {
6182
+ document.getElementById("scope-root").innerHTML =
6183
+ '<p style="color:#dc2626;font-family:system-ui;font-size:13px">No renderable component found.</p>';
6184
+ return;
6185
+ }
6186
+
6187
+ // Error boundary to catch async render errors (React unmounts the whole
6188
+ // root when an error is uncaught \u2014 this keeps the error visible instead).
6189
+ var errorStyle = "color:#dc2626;font-family:system-ui;font-size:13px;padding:12px";
6190
+ class ScopeBoundary extends ReactComponent {
6191
+ constructor(p) { super(p); this.state = { error: null }; }
6192
+ static getDerivedStateFromError(err) { return { error: err }; }
6193
+ render() {
6194
+ if (this.state.error) {
6195
+ return createElement("pre", { style: errorStyle },
6196
+ "Render error: " + (this.state.error.message || String(this.state.error)));
6197
+ }
6198
+ return this.props.children;
6199
+ }
6200
+ }
6201
+
6202
+ var rootEl = document.getElementById("scope-root");
6203
+ var root = createRoot(rootEl);
6204
+ var Wrapper = window.__SCOPE_WRAPPER__;
6205
+
6206
+ function render(props) {
6207
+ var inner = createElement(Target, props);
6208
+ if (Wrapper) inner = createElement(Wrapper, null, inner);
6209
+ root.render(createElement(ScopeBoundary, null, inner));
6210
+ }
6211
+
6212
+ // Render immediately with empty props
6213
+ render({});
6214
+
6215
+ // Listen for messages from the parent frame
6216
+ window.addEventListener("message", function(e) {
6217
+ if (!e.data) return;
6218
+ if (e.data.type === "scope-playground-props") {
6219
+ render(e.data.props || {});
6220
+ } else if (e.data.type === "scope-playground-theme") {
6221
+ document.documentElement.classList.toggle("dark", e.data.theme === "dark");
5661
6222
  }
6223
+ });
6224
+
6225
+ // Report content height changes to the parent frame
6226
+ var ro = new ResizeObserver(function() {
6227
+ var h = rootEl.scrollHeight;
6228
+ if (parent !== window) {
6229
+ parent.postMessage({ type: "scope-playground-height", height: h }, "*");
6230
+ }
6231
+ });
6232
+ ro.observe(rootEl);
6233
+ })();
6234
+ `
5662
6235
  );
6236
+ const result = await esbuild2.build({
6237
+ stdin: {
6238
+ contents: wrapperCode,
6239
+ resolveDir: dirname(filePath),
6240
+ loader: "tsx",
6241
+ sourcefile: "__scope_playground__.tsx"
6242
+ },
6243
+ bundle: true,
6244
+ format: "iife",
6245
+ write: false,
6246
+ platform: "browser",
6247
+ jsx: "automatic",
6248
+ jsxImportSource: "react",
6249
+ target: "es2020",
6250
+ external: [],
6251
+ define: {
6252
+ "process.env.NODE_ENV": '"production"',
6253
+ global: "globalThis"
6254
+ },
6255
+ logLevel: "silent",
6256
+ banner: {
6257
+ js: "/* @agent-scope/cli playground harness */"
6258
+ },
6259
+ loader: {
6260
+ ".css": "empty",
6261
+ ".svg": "dataurl",
6262
+ ".png": "dataurl",
6263
+ ".jpg": "dataurl",
6264
+ ".jpeg": "dataurl",
6265
+ ".gif": "dataurl",
6266
+ ".webp": "dataurl",
6267
+ ".ttf": "dataurl",
6268
+ ".woff": "dataurl",
6269
+ ".woff2": "dataurl"
6270
+ }
6271
+ });
6272
+ if (result.errors.length > 0) {
6273
+ const msg = result.errors.map((e) => `${e.text}${e.location ? ` (${e.location.file}:${e.location.line})` : ""}`).join("\n");
6274
+ throw new Error(`esbuild failed to bundle playground component:
6275
+ ${msg}`);
6276
+ }
6277
+ const outputFile = result.outputFiles?.[0];
6278
+ if (outputFile === void 0 || outputFile.text.length === 0) {
6279
+ throw new Error("esbuild produced no playground output");
6280
+ }
6281
+ return outputFile.text;
5663
6282
  }
5664
- function registerServe(siteCmd) {
5665
- siteCmd.command("serve").description(
5666
- "Start a local HTTP server for the built site directory.\n\nRun `scope site build` first.\nCtrl+C to stop.\n\nExamples:\n scope site serve\n scope site serve --port 8080\n scope site serve --dir ./my-site-output"
5667
- ).option("-p, --port <number>", "Port to listen on", "3000").option("-d, --dir <path>", "Directory to serve", ".reactscope/site").action((opts) => {
6283
+ function wrapPlaygroundHtml(bundledScript, projectCss, wrapperScript) {
6284
+ const projectStyleBlock = projectCss != null && projectCss.length > 0 ? `<style id="scope-project-css">
6285
+ ${projectCss.replace(/<\/style>/gi, "<\\/style>")}
6286
+ </style>` : "";
6287
+ const wrapperScriptBlock = "";
6288
+ return `<!DOCTYPE html>
6289
+ <html lang="en">
6290
+ <head>
6291
+ <meta charset="UTF-8" />
6292
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6293
+ <script>
6294
+ window.__SCOPE_WRAPPER__ = null;
6295
+ // Prevent React DevTools from interfering with the embedded playground.
6296
+ // The hook causes render instability in same-origin iframes.
6297
+ delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
6298
+ </script>
6299
+ <style>
6300
+ *, *::before, *::after { box-sizing: border-box; }
6301
+ html, body { margin: 0; padding: 0; font-family: system-ui, sans-serif; }
6302
+ #scope-root { padding: 16px; min-width: 1px; min-height: 1px; }
6303
+ </style>
6304
+ ${projectStyleBlock}
6305
+ <style>html, body { background: transparent !important; }</style>
6306
+ </head>
6307
+ <body>
6308
+ <div id="scope-root" data-reactscope-root></div>
6309
+ ${wrapperScriptBlock}
6310
+ <script>${bundledScript}</script>
6311
+ </body>
6312
+ </html>`;
6313
+ }
6314
+
6315
+ // src/site-commands.ts
6316
+ var MIME_TYPES = {
6317
+ ".html": "text/html; charset=utf-8",
6318
+ ".css": "text/css; charset=utf-8",
6319
+ ".js": "application/javascript; charset=utf-8",
6320
+ ".json": "application/json; charset=utf-8",
6321
+ ".png": "image/png",
6322
+ ".jpg": "image/jpeg",
6323
+ ".jpeg": "image/jpeg",
6324
+ ".svg": "image/svg+xml",
6325
+ ".ico": "image/x-icon"
6326
+ };
6327
+ function slugify(name) {
6328
+ return name.replace(/([A-Z])/g, (m) => `-${m.toLowerCase()}`).replace(/^-/, "").replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
6329
+ }
6330
+ function loadGlobalCssFilesFromConfig2(cwd) {
6331
+ const configPath = resolve(cwd, "reactscope.config.json");
6332
+ if (!existsSync(configPath)) return [];
6333
+ try {
6334
+ const raw = readFileSync(configPath, "utf-8");
6335
+ const cfg = JSON.parse(raw);
6336
+ return cfg.components?.wrappers?.globalCSS ?? [];
6337
+ } catch {
6338
+ return [];
6339
+ }
6340
+ }
6341
+ function loadIconPatternsFromConfig2(cwd) {
6342
+ const configPath = resolve(cwd, "reactscope.config.json");
6343
+ if (!existsSync(configPath)) return [];
6344
+ try {
6345
+ const raw = readFileSync(configPath, "utf-8");
6346
+ const cfg = JSON.parse(raw);
6347
+ return cfg.icons?.patterns ?? [];
6348
+ } catch {
6349
+ return [];
6350
+ }
6351
+ }
6352
+ var LIVERELOAD_SCRIPT = `<script>(function(){var s=new EventSource("/__livereload");s.onmessage=function(e){if(e.data==="reload")location.reload()};s.onerror=function(){setTimeout(function(){location.reload()},2000)}})()</script>`;
6353
+ function injectLiveReloadScript(html) {
6354
+ const idx = html.lastIndexOf("</body>");
6355
+ if (idx >= 0) return html.slice(0, idx) + LIVERELOAD_SCRIPT + html.slice(idx);
6356
+ return html + LIVERELOAD_SCRIPT;
6357
+ }
6358
+ function loadWatchConfig(rootDir) {
6359
+ const configPath = resolve(rootDir, "reactscope.config.json");
6360
+ if (!existsSync(configPath)) return null;
6361
+ try {
6362
+ const raw = readFileSync(configPath, "utf-8");
6363
+ const cfg = JSON.parse(raw);
6364
+ const result = {};
6365
+ const components = cfg.components;
6366
+ if (components && typeof components === "object") {
6367
+ if (Array.isArray(components.include)) result.include = components.include;
6368
+ if (Array.isArray(components.exclude)) result.exclude = components.exclude;
6369
+ }
6370
+ if (Array.isArray(cfg.internalPatterns))
6371
+ result.internalPatterns = cfg.internalPatterns;
6372
+ if (Array.isArray(cfg.collections)) result.collections = cfg.collections;
6373
+ const icons = cfg.icons;
6374
+ if (icons && typeof icons === "object" && Array.isArray(icons.patterns)) {
6375
+ result.iconPatterns = icons.patterns;
6376
+ }
6377
+ return result;
6378
+ } catch {
6379
+ return null;
6380
+ }
6381
+ }
6382
+ function watchGlob(pattern, filePath) {
6383
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
6384
+ const regexStr = escaped.replace(/\*\*/g, "\xA7GLOBSTAR\xA7").replace(/\*/g, "[^/]*").replace(/\u00a7GLOBSTAR\u00a7/g, ".*");
6385
+ return new RegExp(`^${regexStr}$`, "i").test(filePath);
6386
+ }
6387
+ function matchesWatchPatterns(filePath, include, exclude) {
6388
+ for (const pattern of exclude) {
6389
+ if (watchGlob(pattern, filePath)) return false;
6390
+ }
6391
+ for (const pattern of include) {
6392
+ if (watchGlob(pattern, filePath)) return true;
6393
+ }
6394
+ return false;
6395
+ }
6396
+ function findAffectedComponents(manifest, changedFiles, previousManifest) {
6397
+ const affected = /* @__PURE__ */ new Set();
6398
+ const normalised = changedFiles.map((f) => f.replace(/\\/g, "/"));
6399
+ for (const [name, descriptor] of Object.entries(manifest.components)) {
6400
+ const componentFile = descriptor.filePath.replace(/\\/g, "/");
6401
+ for (const changed of normalised) {
6402
+ if (componentFile === changed) {
6403
+ affected.add(name);
6404
+ break;
6405
+ }
6406
+ const scopeBase = changed.replace(/\.scope\.(ts|tsx|js|jsx)$/, "");
6407
+ const compBase = componentFile.replace(/\.(tsx|ts|jsx|js)$/, "");
6408
+ if (scopeBase !== changed && compBase === scopeBase) {
6409
+ affected.add(name);
6410
+ break;
6411
+ }
6412
+ }
6413
+ }
6414
+ if (previousManifest) {
6415
+ const oldNames = new Set(Object.keys(previousManifest.components));
6416
+ for (const name of Object.keys(manifest.components)) {
6417
+ if (!oldNames.has(name)) affected.add(name);
6418
+ }
6419
+ }
6420
+ return [...affected];
6421
+ }
6422
+ async function renderComponentsForWatch(manifest, componentNames, rootDir, inputDir) {
6423
+ if (componentNames.length === 0) return;
6424
+ const rendersDir = join(inputDir, "renders");
6425
+ await mkdir(rendersDir, { recursive: true });
6426
+ const cssFiles = loadGlobalCssFilesFromConfig2(rootDir);
6427
+ const iconPatterns = loadIconPatternsFromConfig2(rootDir);
6428
+ const complianceStylesPath = join(inputDir, "compliance-styles.json");
6429
+ let complianceStyles = {};
6430
+ if (existsSync(complianceStylesPath)) {
6431
+ try {
6432
+ complianceStyles = JSON.parse(readFileSync(complianceStylesPath, "utf-8"));
6433
+ } catch {
6434
+ }
6435
+ }
6436
+ for (const name of componentNames) {
6437
+ const descriptor = manifest.components[name];
6438
+ if (!descriptor) continue;
6439
+ const filePath = resolve(rootDir, descriptor.filePath);
6440
+ const isIcon = isIconComponent(descriptor.filePath, name, iconPatterns);
6441
+ let scopeData = null;
6442
+ try {
6443
+ scopeData = await loadScopeFileForComponent(filePath);
6444
+ } catch {
6445
+ }
6446
+ const scenarioEntries = scopeData ? Object.entries(scopeData.scenarios) : [];
6447
+ const defaultEntry = scenarioEntries.find(([k]) => k === "default") ?? scenarioEntries[0];
6448
+ const renderProps = defaultEntry?.[1] ?? {};
6449
+ let wrapperScript;
5668
6450
  try {
5669
- const port = Number.parseInt(opts.port, 10);
5670
- if (Number.isNaN(port) || port < 1 || port > 65535) {
5671
- throw new Error(`Invalid port: ${opts.port}`);
6451
+ wrapperScript = scopeData?.hasWrapper ? await buildWrapperScript(scopeData.filePath) : void 0;
6452
+ } catch {
6453
+ }
6454
+ const renderer = buildRenderer(
6455
+ filePath,
6456
+ name,
6457
+ 375,
6458
+ 812,
6459
+ cssFiles,
6460
+ rootDir,
6461
+ wrapperScript,
6462
+ isIcon
6463
+ );
6464
+ const outcome = await safeRender(
6465
+ () => renderer.renderCell(renderProps, descriptor.complexityClass),
6466
+ {
6467
+ props: renderProps,
6468
+ sourceLocation: { file: descriptor.filePath, line: descriptor.loc.start, column: 0 }
5672
6469
  }
5673
- const serveDir = resolve(process.cwd(), opts.dir);
5674
- if (!existsSync(serveDir)) {
5675
- throw new Error(
5676
- `Serve directory not found: ${serveDir}
5677
- Run \`scope site build\` first.`
5678
- );
6470
+ );
6471
+ if (outcome.crashed) {
6472
+ process.stderr.write(` \u2717 ${name}: ${outcome.error.message}
6473
+ `);
6474
+ continue;
6475
+ }
6476
+ const result = outcome.result;
6477
+ if (!isIcon) {
6478
+ writeFileSync(join(rendersDir, `${name}.png`), result.screenshot);
6479
+ }
6480
+ const renderJson = formatRenderJson(name, renderProps, result);
6481
+ const extResult = result;
6482
+ if (isIcon && extResult.svgContent) {
6483
+ renderJson.svgContent = extResult.svgContent;
6484
+ delete renderJson.screenshot;
6485
+ }
6486
+ writeFileSync(join(rendersDir, `${name}.json`), JSON.stringify(renderJson, null, 2));
6487
+ const rawStyles = result.computedStyles["[data-reactscope-root] > *"] ?? {};
6488
+ const compStyles = {
6489
+ colors: {},
6490
+ spacing: {},
6491
+ typography: {},
6492
+ borders: {},
6493
+ shadows: {}
6494
+ };
6495
+ for (const [prop, val] of Object.entries(rawStyles)) {
6496
+ if (!val || val === "none" || val === "") continue;
6497
+ const lower = prop.toLowerCase();
6498
+ if (lower.includes("color") || lower.includes("background")) {
6499
+ compStyles.colors[prop] = val;
6500
+ } else if (lower.includes("padding") || lower.includes("margin") || lower.includes("gap") || lower.includes("width") || lower.includes("height")) {
6501
+ compStyles.spacing[prop] = val;
6502
+ } else if (lower.includes("font") || lower.includes("lineheight") || lower.includes("letterspacing") || lower.includes("texttransform")) {
6503
+ compStyles.typography[prop] = val;
6504
+ } else if (lower.includes("border") || lower.includes("radius") || lower.includes("outline")) {
6505
+ compStyles.borders[prop] = val;
6506
+ } else if (lower.includes("shadow")) {
6507
+ compStyles.shadows[prop] = val;
5679
6508
  }
5680
- const server = createServer((req, res) => {
5681
- const rawUrl = req.url ?? "/";
5682
- const urlPath = decodeURIComponent(rawUrl.split("?")[0] ?? "/");
5683
- const filePath = join(serveDir, urlPath.endsWith("/") ? `${urlPath}index.html` : urlPath);
5684
- if (!filePath.startsWith(serveDir)) {
5685
- res.writeHead(403, { "Content-Type": "text/plain" });
5686
- res.end("Forbidden");
5687
- return;
6509
+ }
6510
+ complianceStyles[name] = compStyles;
6511
+ process.stderr.write(` \u2713 ${name} (${result.renderTimeMs.toFixed(0)}ms)
6512
+ `);
6513
+ }
6514
+ await shutdownPool3();
6515
+ writeFileSync(complianceStylesPath, JSON.stringify(complianceStyles, null, 2), "utf-8");
6516
+ }
6517
+ async function watchRebuildSite(inputDir, outputDir, title, basePath) {
6518
+ const rootDir = process.cwd();
6519
+ await generatePlaygrounds(inputDir, outputDir);
6520
+ const iconPatterns = loadIconPatternsFromConfig2(rootDir);
6521
+ let tokenFilePath;
6522
+ const autoPath = resolve(rootDir, "reactscope.tokens.json");
6523
+ if (existsSync(autoPath)) tokenFilePath = autoPath;
6524
+ let compliancePath;
6525
+ const crPath = join(inputDir, "compliance-report.json");
6526
+ if (existsSync(crPath)) compliancePath = crPath;
6527
+ await buildSite({
6528
+ inputDir,
6529
+ outputDir,
6530
+ basePath,
6531
+ ...compliancePath && { compliancePath },
6532
+ ...tokenFilePath && { tokenFilePath },
6533
+ title,
6534
+ iconPatterns
6535
+ });
6536
+ }
6537
+ function findStaleComponents(manifest, previousManifest, rendersDir) {
6538
+ const stale = [];
6539
+ for (const [name, descriptor] of Object.entries(manifest.components)) {
6540
+ const jsonPath = join(rendersDir, `${name}.json`);
6541
+ if (!existsSync(jsonPath)) {
6542
+ stale.push(name);
6543
+ continue;
6544
+ }
6545
+ if (!previousManifest) continue;
6546
+ const prev = previousManifest.components[name];
6547
+ if (!prev) {
6548
+ stale.push(name);
6549
+ continue;
6550
+ }
6551
+ if (JSON.stringify(prev) !== JSON.stringify(descriptor)) {
6552
+ stale.push(name);
6553
+ }
6554
+ }
6555
+ return stale;
6556
+ }
6557
+ async function runFullBuild(rootDir, inputDir, outputDir, title, basePath) {
6558
+ process.stderr.write("[watch] Starting\u2026\n");
6559
+ const config = loadWatchConfig(rootDir);
6560
+ const manifestPath = join(inputDir, "manifest.json");
6561
+ let previousManifest = null;
6562
+ if (existsSync(manifestPath)) {
6563
+ try {
6564
+ previousManifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
6565
+ } catch {
6566
+ }
6567
+ }
6568
+ process.stderr.write("[watch] Generating manifest\u2026\n");
6569
+ const manifest = await generateManifest({
6570
+ rootDir,
6571
+ ...config?.include && { include: config.include },
6572
+ ...config?.exclude && { exclude: config.exclude },
6573
+ ...config?.internalPatterns && { internalPatterns: config.internalPatterns },
6574
+ ...config?.collections && { collections: config.collections },
6575
+ ...config?.iconPatterns && { iconPatterns: config.iconPatterns }
6576
+ });
6577
+ await mkdir(inputDir, { recursive: true });
6578
+ writeFileSync(join(inputDir, "manifest.json"), JSON.stringify(manifest, null, 2), "utf-8");
6579
+ const count = Object.keys(manifest.components).length;
6580
+ process.stderr.write(`[watch] Found ${count} components
6581
+ `);
6582
+ const rendersDir = join(inputDir, "renders");
6583
+ const stale = findStaleComponents(manifest, previousManifest, rendersDir);
6584
+ if (stale.length > 0) {
6585
+ process.stderr.write(
6586
+ `[watch] Rendering ${stale.length} component(s) (${count - stale.length} already up-to-date)
6587
+ `
6588
+ );
6589
+ await renderComponentsForWatch(manifest, stale, rootDir, inputDir);
6590
+ } else {
6591
+ process.stderr.write("[watch] All renders up-to-date, skipping render step\n");
6592
+ }
6593
+ process.stderr.write("[watch] Building site\u2026\n");
6594
+ await watchRebuildSite(inputDir, outputDir, title, basePath);
6595
+ process.stderr.write("[watch] Ready\n");
6596
+ return manifest;
6597
+ }
6598
+ function startFileWatcher(opts) {
6599
+ const { rootDir, inputDir, outputDir, title, basePath, notifyReload } = opts;
6600
+ let previousManifest = opts.previousManifest;
6601
+ const config = loadWatchConfig(rootDir);
6602
+ const includePatterns = config?.include ?? ["src/**/*.tsx", "src/**/*.ts"];
6603
+ const excludePatterns = config?.exclude ?? [
6604
+ "**/node_modules/**",
6605
+ "**/*.test.*",
6606
+ "**/*.spec.*",
6607
+ "**/dist/**",
6608
+ "**/*.d.ts"
6609
+ ];
6610
+ let debounceTimer = null;
6611
+ const pendingFiles = /* @__PURE__ */ new Set();
6612
+ let isRunning = false;
6613
+ const IGNORE_PREFIXES = ["node_modules/", ".reactscope/", "dist/", ".git/", ".next/", ".turbo/"];
6614
+ const handleChange = async () => {
6615
+ if (isRunning) return;
6616
+ isRunning = true;
6617
+ const changedFiles = [...pendingFiles];
6618
+ pendingFiles.clear();
6619
+ try {
6620
+ process.stderr.write(`
6621
+ [watch] ${changedFiles.length} file(s) changed
6622
+ `);
6623
+ process.stderr.write("[watch] Regenerating manifest\u2026\n");
6624
+ const newManifest = await generateManifest({
6625
+ rootDir,
6626
+ ...config?.include && { include: config.include },
6627
+ ...config?.exclude && { exclude: config.exclude },
6628
+ ...config?.internalPatterns && { internalPatterns: config.internalPatterns },
6629
+ ...config?.collections && { collections: config.collections },
6630
+ ...config?.iconPatterns && { iconPatterns: config.iconPatterns }
6631
+ });
6632
+ writeFileSync(join(inputDir, "manifest.json"), JSON.stringify(newManifest, null, 2), "utf-8");
6633
+ const affected = findAffectedComponents(newManifest, changedFiles, previousManifest);
6634
+ if (affected.length > 0) {
6635
+ process.stderr.write(`[watch] Re-rendering: ${affected.join(", ")}
6636
+ `);
6637
+ await renderComponentsForWatch(newManifest, affected, rootDir, inputDir);
6638
+ } else {
6639
+ process.stderr.write("[watch] No components directly affected\n");
6640
+ }
6641
+ process.stderr.write("[watch] Rebuilding site\u2026\n");
6642
+ await watchRebuildSite(inputDir, outputDir, title, basePath);
6643
+ previousManifest = newManifest;
6644
+ process.stderr.write("[watch] Done\n");
6645
+ notifyReload();
6646
+ } catch (err) {
6647
+ process.stderr.write(`[watch] Error: ${err instanceof Error ? err.message : String(err)}
6648
+ `);
6649
+ } finally {
6650
+ isRunning = false;
6651
+ if (pendingFiles.size > 0) {
6652
+ handleChange();
6653
+ }
6654
+ }
6655
+ };
6656
+ const onFileChange = (_eventType, filename) => {
6657
+ if (!filename) return;
6658
+ const normalised = filename.replace(/\\/g, "/");
6659
+ for (const prefix of IGNORE_PREFIXES) {
6660
+ if (normalised.startsWith(prefix)) return;
6661
+ }
6662
+ if (!matchesWatchPatterns(normalised, includePatterns, excludePatterns)) return;
6663
+ pendingFiles.add(normalised);
6664
+ if (debounceTimer) clearTimeout(debounceTimer);
6665
+ debounceTimer = setTimeout(() => {
6666
+ debounceTimer = null;
6667
+ handleChange();
6668
+ }, 500);
6669
+ };
6670
+ try {
6671
+ watch(rootDir, { recursive: true }, onFileChange);
6672
+ process.stderr.write(`[watch] Watching for changes (${includePatterns.join(", ")})
6673
+ `);
6674
+ } catch (err) {
6675
+ process.stderr.write(
6676
+ `[watch] Warning: Could not start watcher: ${err instanceof Error ? err.message : String(err)}
6677
+ `
6678
+ );
6679
+ }
6680
+ }
6681
+ async function generatePlaygrounds(inputDir, outputDir) {
6682
+ const manifestPath = join(inputDir, "manifest.json");
6683
+ const raw = readFileSync(manifestPath, "utf-8");
6684
+ const manifest = JSON.parse(raw);
6685
+ const rootDir = process.cwd();
6686
+ const componentNames = Object.keys(manifest.components);
6687
+ if (componentNames.length === 0) return [];
6688
+ const playgroundDir = join(outputDir, "playground");
6689
+ await mkdir(playgroundDir, { recursive: true });
6690
+ const cssFiles = loadGlobalCssFilesFromConfig2(rootDir);
6691
+ const projectCss = await loadGlobalCss(cssFiles, rootDir) ?? void 0;
6692
+ let succeeded = 0;
6693
+ const failures = [];
6694
+ const allDefaults = {};
6695
+ for (const name of componentNames) {
6696
+ const descriptor = manifest.components[name];
6697
+ if (!descriptor) continue;
6698
+ const filePath = resolve(rootDir, descriptor.filePath);
6699
+ const slug = slugify(name);
6700
+ try {
6701
+ const scopeData = await loadScopeFileForComponent(filePath);
6702
+ if (scopeData) {
6703
+ const defaultScenario = scopeData.scenarios.default ?? Object.values(scopeData.scenarios)[0];
6704
+ if (defaultScenario) allDefaults[name] = defaultScenario;
6705
+ }
6706
+ } catch {
6707
+ }
6708
+ try {
6709
+ const html = await buildPlaygroundHarness(filePath, name, projectCss);
6710
+ await writeFile(join(playgroundDir, `${slug}.html`), html, "utf-8");
6711
+ succeeded++;
6712
+ } catch (err) {
6713
+ process.stderr.write(
6714
+ `[scope/site] \u26A0 playground skip: ${name} \u2014 ${err instanceof Error ? err.message : String(err)}
6715
+ `
6716
+ );
6717
+ failures.push({
6718
+ component: name,
6719
+ stage: "playground",
6720
+ message: err instanceof Error ? err.message : String(err),
6721
+ outputPath: join(playgroundDir, `${slug}.html`)
6722
+ });
6723
+ }
6724
+ }
6725
+ await writeFile(
6726
+ join(inputDir, "playground-defaults.json"),
6727
+ JSON.stringify(allDefaults, null, 2),
6728
+ "utf-8"
6729
+ );
6730
+ process.stderr.write(
6731
+ `[scope/site] Playgrounds: ${succeeded} built${failures.length > 0 ? `, ${failures.length} failed` : ""}
6732
+ `
6733
+ );
6734
+ return failures;
6735
+ }
6736
+ function registerBuild(siteCmd) {
6737
+ siteCmd.command("build").description(
6738
+ 'Build the static HTML site from manifest + render outputs.\n\nINPUT DIRECTORY (.reactscope/ by default) must contain:\n manifest.json component registry\n renders/ screenshots and render.json files from `scope render all`\n\nOPTIONAL:\n --compliance <path> include token compliance scores on detail pages\n --base-path <path> set if deploying to a subdirectory (e.g. /ui-docs)\n\nExamples:\n scope site build\n scope site build --title "Design System" -o .reactscope/site\n scope site build --compliance .reactscope/compliance-report.json\n scope site build --tokens reactscope.tokens.json\n scope site build --base-path /ui'
6739
+ ).option("-i, --input <path>", "Path to .reactscope input directory", ".reactscope").option("-o, --output <path>", "Output directory for generated site", ".reactscope/site").option("--base-path <path>", "Base URL path prefix for subdirectory deployment", "/").option("--compliance <path>", "Path to compliance batch report JSON").option("--tokens <path>", "Path to reactscope.tokens.json (enables token browser page)").option("--title <text>", "Site title", "Scope \u2014 Component Gallery").action(
6740
+ async (opts) => {
6741
+ try {
6742
+ const inputDir = resolve(process.cwd(), opts.input);
6743
+ const outputDir = resolve(process.cwd(), opts.output);
6744
+ if (!existsSync(inputDir)) {
6745
+ throw new Error(
6746
+ `Input directory not found: ${inputDir}
6747
+ Run \`scope manifest generate\` and \`scope render\` first.`
6748
+ );
5688
6749
  }
5689
- if (existsSync(filePath) && statSync(filePath).isFile()) {
5690
- const ext = extname(filePath).toLowerCase();
5691
- const contentType = MIME_TYPES[ext] ?? "application/octet-stream";
5692
- res.writeHead(200, { "Content-Type": contentType });
5693
- createReadStream(filePath).pipe(res);
5694
- return;
6750
+ const manifestPath = join(inputDir, "manifest.json");
6751
+ if (!existsSync(manifestPath)) {
6752
+ throw new Error(
6753
+ `Manifest not found at ${manifestPath}
6754
+ Run \`scope manifest generate\` first.`
6755
+ );
5695
6756
  }
5696
- const htmlPath = `${filePath}.html`;
5697
- if (existsSync(htmlPath) && statSync(htmlPath).isFile()) {
5698
- res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
5699
- createReadStream(htmlPath).pipe(res);
5700
- return;
6757
+ process.stderr.write(`Building site from ${inputDir}\u2026
6758
+ `);
6759
+ process.stderr.write("Bundling playgrounds\u2026\n");
6760
+ const failures = await generatePlaygrounds(inputDir, outputDir);
6761
+ const iconPatterns = loadIconPatternsFromConfig2(process.cwd());
6762
+ let tokenFilePath = opts.tokens ? resolve(process.cwd(), opts.tokens) : void 0;
6763
+ if (tokenFilePath === void 0) {
6764
+ const autoPath = resolve(process.cwd(), "reactscope.tokens.json");
6765
+ if (existsSync(autoPath)) {
6766
+ tokenFilePath = autoPath;
6767
+ }
5701
6768
  }
5702
- res.writeHead(404, { "Content-Type": "text/plain" });
5703
- res.end(`Not found: ${urlPath}`);
5704
- });
5705
- server.listen(port, () => {
5706
- process.stderr.write(`Scope site running at http://localhost:${port}
6769
+ await buildSite({
6770
+ inputDir,
6771
+ outputDir,
6772
+ basePath: opts.basePath,
6773
+ ...opts.compliance !== void 0 && {
6774
+ compliancePath: resolve(process.cwd(), opts.compliance)
6775
+ },
6776
+ ...tokenFilePath !== void 0 && { tokenFilePath },
6777
+ title: opts.title,
6778
+ iconPatterns
6779
+ });
6780
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
6781
+ const componentCount = Object.keys(manifest.components).length;
6782
+ const generatedPlaygroundCount = componentCount === 0 ? 0 : statSync(join(outputDir, "playground")).isDirectory() ? componentCount - failures.length : 0;
6783
+ const siteFailures = [...failures];
6784
+ if (componentCount === 0) {
6785
+ siteFailures.push({
6786
+ component: "*",
6787
+ stage: "site",
6788
+ message: "Manifest contains zero components; generated site is structurally degraded.",
6789
+ outputPath: manifestPath
6790
+ });
6791
+ } else if (generatedPlaygroundCount === 0) {
6792
+ siteFailures.push({
6793
+ component: "*",
6794
+ stage: "site",
6795
+ message: "No playground pages were generated successfully; site build is degraded and should not be treated as green.",
6796
+ outputPath: join(outputDir, "playground")
6797
+ });
6798
+ }
6799
+ const summaryPath = writeRunSummary({
6800
+ command: "scope site build",
6801
+ status: siteFailures.length > 0 ? "failed" : "success",
6802
+ outputPaths: [outputDir, join(outputDir, "index.html")],
6803
+ failures: siteFailures
6804
+ });
6805
+ process.stderr.write(`Site written to ${outputDir}
5707
6806
  `);
5708
- process.stderr.write(`Serving ${serveDir}
6807
+ process.stderr.write(`[scope/site] Run summary written to ${summaryPath}
5709
6808
  `);
5710
- process.stderr.write("Press Ctrl+C to stop.\n");
5711
- });
5712
- server.on("error", (err) => {
5713
- if (err.code === "EADDRINUSE") {
5714
- process.stderr.write(`Error: Port ${port} is already in use.
6809
+ process.stdout.write(`${outputDir}
5715
6810
  `);
5716
- } else {
5717
- process.stderr.write(`Server error: ${err.message}
6811
+ if (siteFailures.length > 0) process.exit(1);
6812
+ } catch (err) {
6813
+ process.stderr.write(`${formatScopeDiagnostic(err)}
5718
6814
  `);
5719
- }
5720
6815
  process.exit(1);
5721
- });
5722
- } catch (err) {
5723
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
6816
+ }
6817
+ }
6818
+ );
6819
+ }
6820
+ function registerServe(siteCmd) {
6821
+ siteCmd.command("serve").description(
6822
+ "Start a local HTTP server for the built site directory.\n\nRun `scope site build` first, or use --watch to auto-rebuild on changes.\nCtrl+C to stop.\n\nExamples:\n scope site serve\n scope site serve --port 8080\n scope site serve --dir ./my-site-output\n scope site serve --watch"
6823
+ ).option("-p, --port <number>", "Port to listen on", "3000").option("-d, --dir <path>", "Directory to serve", ".reactscope/site").option("-w, --watch", "Watch source files and rebuild on changes").option(
6824
+ "-i, --input <path>",
6825
+ "Input directory for .reactscope data (watch mode)",
6826
+ ".reactscope"
6827
+ ).option("--title <text>", "Site title (watch mode)", "Scope \u2014 Component Gallery").option("--base-path <path>", "Base URL path prefix (watch mode)", "/").action(
6828
+ async (opts) => {
6829
+ try {
6830
+ let notifyReload2 = function() {
6831
+ for (const client of sseClients) {
6832
+ client.write("data: reload\n\n");
6833
+ }
6834
+ };
6835
+ var notifyReload = notifyReload2;
6836
+ const port = Number.parseInt(opts.port, 10);
6837
+ if (Number.isNaN(port) || port < 1 || port > 65535) {
6838
+ throw new Error(`Invalid port: ${opts.port}`);
6839
+ }
6840
+ const serveDir = resolve(process.cwd(), opts.dir);
6841
+ const watchMode = opts.watch === true;
6842
+ const sseClients = /* @__PURE__ */ new Set();
6843
+ if (watchMode) {
6844
+ await mkdir(serveDir, { recursive: true });
6845
+ }
6846
+ if (!watchMode && !existsSync(serveDir)) {
6847
+ throw new Error(
6848
+ `Serve directory not found: ${serveDir}
6849
+ Run \`scope site build\` first.`
6850
+ );
6851
+ }
6852
+ const server = createServer((req, res) => {
6853
+ const rawUrl = req.url ?? "/";
6854
+ const urlPath = decodeURIComponent(rawUrl.split("?")[0] ?? "/");
6855
+ if (watchMode && urlPath === "/__livereload") {
6856
+ res.writeHead(200, {
6857
+ "Content-Type": "text/event-stream",
6858
+ "Cache-Control": "no-cache",
6859
+ Connection: "keep-alive",
6860
+ "Access-Control-Allow-Origin": "*"
6861
+ });
6862
+ res.write("data: connected\n\n");
6863
+ sseClients.add(res);
6864
+ req.on("close", () => sseClients.delete(res));
6865
+ return;
6866
+ }
6867
+ const filePath = join(
6868
+ serveDir,
6869
+ urlPath.endsWith("/") ? `${urlPath}index.html` : urlPath
6870
+ );
6871
+ if (!filePath.startsWith(serveDir)) {
6872
+ res.writeHead(403, { "Content-Type": "text/plain" });
6873
+ res.end("Forbidden");
6874
+ return;
6875
+ }
6876
+ if (existsSync(filePath) && statSync(filePath).isFile()) {
6877
+ const ext = extname(filePath).toLowerCase();
6878
+ const contentType = MIME_TYPES[ext] ?? "application/octet-stream";
6879
+ if (watchMode && ext === ".html") {
6880
+ const html = injectLiveReloadScript(readFileSync(filePath, "utf-8"));
6881
+ res.writeHead(200, { "Content-Type": contentType });
6882
+ res.end(html);
6883
+ return;
6884
+ }
6885
+ res.writeHead(200, { "Content-Type": contentType });
6886
+ createReadStream(filePath).pipe(res);
6887
+ return;
6888
+ }
6889
+ const htmlPath = `${filePath}.html`;
6890
+ if (existsSync(htmlPath) && statSync(htmlPath).isFile()) {
6891
+ if (watchMode) {
6892
+ const html = injectLiveReloadScript(readFileSync(htmlPath, "utf-8"));
6893
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
6894
+ res.end(html);
6895
+ return;
6896
+ }
6897
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
6898
+ createReadStream(htmlPath).pipe(res);
6899
+ return;
6900
+ }
6901
+ res.writeHead(404, { "Content-Type": "text/plain" });
6902
+ res.end(`Not found: ${urlPath}`);
6903
+ });
6904
+ server.listen(port, () => {
6905
+ process.stderr.write(`Scope site running at http://localhost:${port}
5724
6906
  `);
5725
- process.exit(1);
6907
+ process.stderr.write(`Serving ${serveDir}
6908
+ `);
6909
+ if (watchMode) {
6910
+ process.stderr.write(
6911
+ "Watch mode enabled \u2014 source changes trigger rebuild + browser reload\n"
6912
+ );
6913
+ }
6914
+ process.stderr.write("Press Ctrl+C to stop.\n");
6915
+ });
6916
+ server.on("error", (err) => {
6917
+ if (err.code === "EADDRINUSE") {
6918
+ process.stderr.write(`Error: Port ${port} is already in use.
6919
+ `);
6920
+ } else {
6921
+ process.stderr.write(`Server error: ${err.message}
6922
+ `);
6923
+ }
6924
+ process.exit(1);
6925
+ });
6926
+ if (watchMode) {
6927
+ const rootDir = process.cwd();
6928
+ const inputDir = resolve(rootDir, opts.input);
6929
+ const initialManifest = await runFullBuild(
6930
+ rootDir,
6931
+ inputDir,
6932
+ serveDir,
6933
+ opts.title,
6934
+ opts.basePath
6935
+ );
6936
+ notifyReload2();
6937
+ startFileWatcher({
6938
+ rootDir,
6939
+ inputDir,
6940
+ outputDir: serveDir,
6941
+ title: opts.title,
6942
+ basePath: opts.basePath,
6943
+ previousManifest: initialManifest,
6944
+ notifyReload: notifyReload2
6945
+ });
6946
+ }
6947
+ } catch (err) {
6948
+ process.stderr.write(`${formatScopeDiagnostic(err)}
6949
+ `);
6950
+ process.exit(1);
6951
+ }
5726
6952
  }
5727
- });
6953
+ );
5728
6954
  }
5729
6955
  function createSiteCommand() {
5730
6956
  const siteCmd = new Command("site").description(
5731
- 'Build and serve the static HTML component gallery site.\n\nPREREQUISITES:\n scope manifest generate (manifest.json)\n scope render all (renders/ + compliance-styles.json)\n\nSITE CONTENTS:\n /index.html component gallery with screenshots + metadata\n /<component>/index.html detail page: props, renders, matrix, X-Ray, compliance\n\nExamples:\n scope site build && scope site serve\n scope site build --title "Acme UI" --compliance .reactscope/compliance-styles.json\n scope site serve --port 8080'
6957
+ 'Build and serve the static HTML component gallery site.\n\nPREREQUISITES:\n scope manifest generate (manifest.json)\n scope render all (renders/ + compliance-styles.json)\n\nSITE CONTENTS:\n /index.html component gallery with screenshots + metadata\n /<component>/index.html detail page: props, renders, matrix, X-Ray, compliance\n\nExamples:\n scope site build && scope site serve\n scope site build --title "Acme UI" --compliance .reactscope/compliance-report.json\n scope site serve --port 8080'
5732
6958
  );
5733
6959
  registerBuild(siteCmd);
5734
6960
  registerServe(siteCmd);
@@ -5772,11 +6998,11 @@ function categoryForProperty(property) {
5772
6998
  }
5773
6999
  function buildCategorySummary(batch) {
5774
7000
  const cats = {
5775
- color: { total: 0, onSystem: 0, offSystem: 0, compliance: 1 },
5776
- spacing: { total: 0, onSystem: 0, offSystem: 0, compliance: 1 },
5777
- typography: { total: 0, onSystem: 0, offSystem: 0, compliance: 1 },
5778
- border: { total: 0, onSystem: 0, offSystem: 0, compliance: 1 },
5779
- shadow: { total: 0, onSystem: 0, offSystem: 0, compliance: 1 }
7001
+ color: { total: 0, onSystem: 0, offSystem: 0, compliance: 0 },
7002
+ spacing: { total: 0, onSystem: 0, offSystem: 0, compliance: 0 },
7003
+ typography: { total: 0, onSystem: 0, offSystem: 0, compliance: 0 },
7004
+ border: { total: 0, onSystem: 0, offSystem: 0, compliance: 0 },
7005
+ shadow: { total: 0, onSystem: 0, offSystem: 0, compliance: 0 }
5780
7006
  };
5781
7007
  for (const report of Object.values(batch.components)) {
5782
7008
  for (const [property, result] of Object.entries(report.properties)) {
@@ -5792,7 +7018,7 @@ function buildCategorySummary(batch) {
5792
7018
  }
5793
7019
  }
5794
7020
  for (const summary of Object.values(cats)) {
5795
- summary.compliance = summary.total === 0 ? 1 : summary.onSystem / summary.total;
7021
+ summary.compliance = summary.total === 0 ? 0 : summary.onSystem / summary.total;
5796
7022
  }
5797
7023
  return cats;
5798
7024
  }
@@ -5833,6 +7059,11 @@ function formatComplianceReport(batch, threshold) {
5833
7059
  const lines = [];
5834
7060
  const thresholdLabel = threshold !== void 0 ? pct >= threshold ? " \u2713 (pass)" : ` \u2717 (below threshold ${threshold}%)` : "";
5835
7061
  lines.push(`Overall compliance score: ${pct}%${thresholdLabel}`);
7062
+ if (batch.totalProperties === 0) {
7063
+ lines.push(
7064
+ "No CSS properties were audited; run `scope render all` and inspect .reactscope/compliance-styles.json before treating compliance as green."
7065
+ );
7066
+ }
5836
7067
  lines.push("");
5837
7068
  const cats = buildCategorySummary(batch);
5838
7069
  const catEntries = Object.entries(cats).filter(([, s]) => s.total > 0);
@@ -5867,42 +7098,84 @@ function formatComplianceReport(batch, threshold) {
5867
7098
  }
5868
7099
  function registerCompliance(tokensCmd) {
5869
7100
  tokensCmd.command("compliance").description(
5870
- "Compute a token compliance score across all rendered components.\n\nCompares computed CSS values from .reactscope/compliance-styles.json\nagainst the token file \u2014 reports what % of style values are on-token.\n\nPREREQUISITES:\n scope render all must have run first (produces compliance-styles.json)\n\nSCORING:\n compliant value exactly matches a token\n near-match value is within tolerance of a token (e.g. close color)\n off-token value not found in token file\n\nEXIT CODES:\n 0 compliance >= threshold (or no --threshold set)\n 1 compliance < threshold\n\nExamples:\n scope tokens compliance\n scope tokens compliance --threshold 90\n scope tokens compliance --format json | jq '.summary'\n scope tokens compliance --styles ./custom/compliance-styles.json"
5871
- ).option("--file <path>", "Path to token file (overrides config)").option("--styles <path>", `Path to compliance styles JSON (default: ${DEFAULT_STYLES_PATH})`).option("--threshold <n>", "Exit code 1 if compliance score is below this percentage (0-100)").option("--format <fmt>", "Output format: json or text (default: auto-detect)").action((opts) => {
5872
- try {
5873
- const tokenFilePath = resolveTokenFilePath(opts.file);
5874
- const { tokens } = loadTokens(tokenFilePath);
5875
- const resolver = new TokenResolver(tokens);
5876
- const engine = new ComplianceEngine(resolver);
5877
- const stylesPath = opts.styles ?? DEFAULT_STYLES_PATH;
5878
- const stylesFile = loadStylesFile(stylesPath);
5879
- const componentMap = /* @__PURE__ */ new Map();
5880
- for (const [name, styles] of Object.entries(stylesFile)) {
5881
- componentMap.set(name, styles);
5882
- }
5883
- if (componentMap.size === 0) {
5884
- process.stderr.write(`Warning: No components found in styles file at ${stylesPath}
7101
+ "Compute a token compliance score across all rendered components.\n\nCompares computed CSS values from .reactscope/compliance-styles.json\nagainst the token file \u2014 reports what % of style values are on-token.\n\nPREREQUISITES:\n scope render all must have run first (produces compliance-styles.json)\n\nSCORING:\n compliant value exactly matches a token\n near-match value is within tolerance of a token (e.g. close color)\n off-token value not found in token file\n\nEXIT CODES:\n 0 compliance >= threshold (or no --threshold set)\n 1 compliance < threshold\n\nExamples:\n scope tokens compliance\n scope tokens compliance --threshold 90\n scope tokens compliance --format json | jq '.summary'\n scope tokens compliance --out .reactscope/compliance-report.json\n scope tokens compliance --styles ./custom/compliance-styles.json"
7102
+ ).option("--file <path>", "Path to token file (overrides config)").option("--styles <path>", `Path to compliance styles JSON (default: ${DEFAULT_STYLES_PATH})`).option(
7103
+ "--out <path>",
7104
+ "Write JSON report to file (for use with scope site build --compliance)"
7105
+ ).option("--threshold <n>", "Exit code 1 if compliance score is below this percentage (0-100)").option("--format <fmt>", "Output format: json or text (default: auto-detect)").action(
7106
+ (opts) => {
7107
+ try {
7108
+ const tokenFilePath = resolveTokenFilePath(opts.file);
7109
+ const { tokens } = loadTokens(tokenFilePath);
7110
+ const resolver = new TokenResolver(tokens);
7111
+ const engine = new ComplianceEngine(resolver);
7112
+ const stylesPath = opts.styles ?? DEFAULT_STYLES_PATH;
7113
+ const stylesFile = loadStylesFile(stylesPath);
7114
+ const componentMap = /* @__PURE__ */ new Map();
7115
+ for (const [name, styles] of Object.entries(stylesFile)) {
7116
+ componentMap.set(name, styles);
7117
+ }
7118
+ if (componentMap.size === 0) {
7119
+ process.stderr.write(`Warning: No components found in styles file at ${stylesPath}
5885
7120
  `);
5886
- }
5887
- const batch = engine.auditBatch(componentMap);
5888
- const useJson = opts.format === "json" || opts.format !== "text" && !isTTY();
5889
- const threshold = opts.threshold !== void 0 ? Number.parseInt(opts.threshold, 10) : void 0;
5890
- if (useJson) {
5891
- process.stdout.write(`${JSON.stringify(batch, null, 2)}
7121
+ }
7122
+ const batch = engine.auditBatch(componentMap);
7123
+ const threshold = opts.threshold !== void 0 ? Number.parseInt(opts.threshold, 10) : void 0;
7124
+ const failures = [];
7125
+ if (batch.totalProperties === 0) {
7126
+ failures.push({
7127
+ component: "*",
7128
+ stage: "compliance",
7129
+ message: `No CSS properties were audited from ${stylesPath}; refusing to report silent success.`,
7130
+ outputPath: stylesPath
7131
+ });
7132
+ } else if (threshold !== void 0 && Math.round(batch.aggregateCompliance * 100) < threshold) {
7133
+ failures.push({
7134
+ component: "*",
7135
+ stage: "compliance",
7136
+ message: `Compliance ${Math.round(batch.aggregateCompliance * 100)}% is below threshold ${threshold}%.`,
7137
+ outputPath: opts.out ?? ".reactscope/compliance-report.json"
7138
+ });
7139
+ }
7140
+ if (opts.out !== void 0) {
7141
+ const outPath = resolve(process.cwd(), opts.out);
7142
+ writeFileSync(outPath, JSON.stringify(batch, null, 2), "utf-8");
7143
+ process.stderr.write(`Compliance report written to ${outPath}
5892
7144
  `);
5893
- } else {
5894
- process.stdout.write(`${formatComplianceReport(batch, threshold)}
7145
+ }
7146
+ const useJson = opts.format === "json" || opts.format !== "text" && !isTTY();
7147
+ if (useJson) {
7148
+ process.stdout.write(`${JSON.stringify(batch, null, 2)}
7149
+ `);
7150
+ } else {
7151
+ process.stdout.write(`${formatComplianceReport(batch, threshold)}
7152
+ `);
7153
+ }
7154
+ const summaryPath = writeRunSummary({
7155
+ command: "scope tokens compliance",
7156
+ status: failures.length > 0 ? "failed" : "success",
7157
+ outputPaths: [opts.out ?? ".reactscope/compliance-report.json", stylesPath],
7158
+ compliance: {
7159
+ auditedProperties: batch.totalProperties,
7160
+ onSystemProperties: batch.totalOnSystem,
7161
+ offSystemProperties: batch.totalOffSystem,
7162
+ score: Math.round(batch.aggregateCompliance * 100),
7163
+ threshold
7164
+ },
7165
+ failures
7166
+ });
7167
+ process.stderr.write(`[scope/tokens] Run summary written to ${summaryPath}
7168
+ `);
7169
+ if (failures.length > 0) {
7170
+ process.exit(1);
7171
+ }
7172
+ } catch (err) {
7173
+ process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
5895
7174
  `);
5896
- }
5897
- if (threshold !== void 0 && Math.round(batch.aggregateCompliance * 100) < threshold) {
5898
7175
  process.exit(1);
5899
7176
  }
5900
- } catch (err) {
5901
- process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
5902
- `);
5903
- process.exit(1);
5904
7177
  }
5905
- });
7178
+ );
5906
7179
  }
5907
7180
  var DEFAULT_TOKEN_FILE = "reactscope.tokens.json";
5908
7181
  var CONFIG_FILE = "reactscope.config.json";
@@ -6107,18 +7380,242 @@ ${formatImpactSummary(report)}
6107
7380
  }
6108
7381
  );
6109
7382
  }
7383
+ var DEFAULT_TOKEN_FILE2 = "reactscope.tokens.json";
7384
+ var CONFIG_FILE2 = "reactscope.config.json";
7385
+ function resolveOutputPath(fileFlag) {
7386
+ if (fileFlag !== void 0) {
7387
+ return resolve(process.cwd(), fileFlag);
7388
+ }
7389
+ const configPath = resolve(process.cwd(), CONFIG_FILE2);
7390
+ if (existsSync(configPath)) {
7391
+ try {
7392
+ const raw = readFileSync(configPath, "utf-8");
7393
+ const config = JSON.parse(raw);
7394
+ if (typeof config === "object" && config !== null && "tokens" in config && typeof config.tokens === "object" && config.tokens !== null && typeof config.tokens?.file === "string") {
7395
+ const file = config.tokens.file;
7396
+ return resolve(process.cwd(), file);
7397
+ }
7398
+ } catch {
7399
+ }
7400
+ }
7401
+ return resolve(process.cwd(), DEFAULT_TOKEN_FILE2);
7402
+ }
7403
+ var CSS_VAR_RE = /--([\w-]+)\s*:\s*([^;]+)/g;
7404
+ var HEX_COLOR_RE = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
7405
+ var COLOR_FN_RE = /^(?:rgba?|hsla?|oklch|oklab|lch|lab|color|hwb)\(/;
7406
+ var DIMENSION_RE = /^-?\d+(?:\.\d+)?(?:px|rem|em|%|vw|vh|ch|ex|cap|lh|dvh|svh|lvh)$/;
7407
+ var DURATION_RE = /^-?\d+(?:\.\d+)?(?:ms|s)$/;
7408
+ var FONT_FAMILY_RE = /^["']|,\s*(?:sans-serif|serif|monospace|cursive|fantasy|system-ui)/;
7409
+ var NUMBER_RE = /^-?\d+(?:\.\d+)?$/;
7410
+ var CUBIC_BEZIER_RE = /^cubic-bezier\(/;
7411
+ var SHADOW_RE = /^\d.*(?:px|rem|em)\s+(?:#|rgba?|hsla?|oklch|oklab)/i;
7412
+ function inferTokenType(value) {
7413
+ const v = value.trim();
7414
+ if (HEX_COLOR_RE.test(v) || COLOR_FN_RE.test(v)) return "color";
7415
+ if (DURATION_RE.test(v)) return "duration";
7416
+ if (DIMENSION_RE.test(v)) return "dimension";
7417
+ if (FONT_FAMILY_RE.test(v)) return "fontFamily";
7418
+ if (CUBIC_BEZIER_RE.test(v)) return "cubicBezier";
7419
+ if (SHADOW_RE.test(v)) return "shadow";
7420
+ if (NUMBER_RE.test(v)) return "number";
7421
+ return "color";
7422
+ }
7423
+ function setNestedToken(root, segments, value, type) {
7424
+ let node = root;
7425
+ for (let i = 0; i < segments.length - 1; i++) {
7426
+ const seg = segments[i];
7427
+ if (seg === void 0) continue;
7428
+ if (!(seg in node) || typeof node[seg] !== "object" || node[seg] === null) {
7429
+ node[seg] = {};
7430
+ }
7431
+ node = node[seg];
7432
+ }
7433
+ const leaf = segments[segments.length - 1];
7434
+ if (leaf === void 0) return;
7435
+ node[leaf] = { value, type };
7436
+ }
7437
+ function extractBlockBody(css, openBrace) {
7438
+ let depth = 0;
7439
+ let end = -1;
7440
+ for (let i = openBrace; i < css.length; i++) {
7441
+ if (css[i] === "{") depth++;
7442
+ else if (css[i] === "}") {
7443
+ depth--;
7444
+ if (depth === 0) {
7445
+ end = i;
7446
+ break;
7447
+ }
7448
+ }
7449
+ }
7450
+ if (end === -1) return "";
7451
+ return css.slice(openBrace + 1, end);
7452
+ }
7453
+ function parseScopedBlocks(css) {
7454
+ const blocks = [];
7455
+ const blockRe = /(?::root|@theme(?:\s+inline)?|\.dark\.high-contrast|\.dark)\s*\{/g;
7456
+ let match = blockRe.exec(css);
7457
+ while (match !== null) {
7458
+ const selector = match[0];
7459
+ const braceIdx = css.indexOf("{", match.index);
7460
+ if (braceIdx === -1) {
7461
+ match = blockRe.exec(css);
7462
+ continue;
7463
+ }
7464
+ const body = extractBlockBody(css, braceIdx);
7465
+ let scope;
7466
+ if (selector.includes(".dark.high-contrast")) scope = "dark-high-contrast";
7467
+ else if (selector.includes(".dark")) scope = "dark";
7468
+ else if (selector.includes("@theme")) scope = "theme";
7469
+ else scope = "root";
7470
+ blocks.push({ scope, body });
7471
+ match = blockRe.exec(css);
7472
+ }
7473
+ return blocks;
7474
+ }
7475
+ function extractVarsFromBody(body) {
7476
+ const results = [];
7477
+ for (const m of body.matchAll(CSS_VAR_RE)) {
7478
+ const name = m[1];
7479
+ const value = m[2]?.trim();
7480
+ if (name === void 0 || value === void 0 || value.length === 0) continue;
7481
+ if (value.startsWith("var(") || value.startsWith("calc(")) continue;
7482
+ results.push({ name, value });
7483
+ }
7484
+ return results;
7485
+ }
7486
+ function extractCSSCustomProperties(tokenSources) {
7487
+ const cssSources = tokenSources.filter(
7488
+ (s) => s.kind === "css-custom-properties" || s.kind === "tailwind-v4-theme"
7489
+ );
7490
+ if (cssSources.length === 0) return null;
7491
+ const tokens = {};
7492
+ const themes = {};
7493
+ let found = false;
7494
+ for (const source of cssSources) {
7495
+ try {
7496
+ if (source.path.includes("compiled") || source.path.includes(".min.")) continue;
7497
+ const raw = readFileSync(source.path, "utf-8");
7498
+ const blocks = parseScopedBlocks(raw);
7499
+ for (const block of blocks) {
7500
+ const vars = extractVarsFromBody(block.body);
7501
+ for (const { name, value } of vars) {
7502
+ const segments = name.split("-").filter(Boolean);
7503
+ if (segments.length === 0) continue;
7504
+ if (block.scope === "root" || block.scope === "theme") {
7505
+ const type = inferTokenType(value);
7506
+ setNestedToken(tokens, segments, value, type);
7507
+ found = true;
7508
+ } else {
7509
+ const themeName = block.scope;
7510
+ if (!themes[themeName]) themes[themeName] = {};
7511
+ const path = segments.join(".");
7512
+ themes[themeName][path] = value;
7513
+ found = true;
7514
+ }
7515
+ }
7516
+ }
7517
+ } catch {
7518
+ }
7519
+ }
7520
+ return found ? { tokens, themes } : null;
7521
+ }
7522
+ function registerTokensInit(tokensCmd) {
7523
+ tokensCmd.command("init").description(
7524
+ "Detect design-token sources in the project and generate a token file.\n\nDETECTED SOURCES:\n - Tailwind config (colors, spacing, fontFamily, borderRadius)\n - CSS custom properties (:root { --color-primary: #0070f3; })\n\nWill not overwrite an existing token file unless --force is passed.\n\nOUTPUT PATH RESOLUTION (in priority order):\n 1. --file <path> explicit override\n 2. tokens.file in reactscope.config.json\n 3. reactscope.tokens.json default (project root)\n\nExamples:\n scope tokens init\n scope tokens init --force\n scope tokens init --file tokens/brand.json\n scope tokens init --force --file custom-tokens.json"
7525
+ ).option("--file <path>", "Output path for the token file (overrides config)").option("--force", "Overwrite existing token file", false).action((opts) => {
7526
+ try {
7527
+ const outPath = resolveOutputPath(opts.file);
7528
+ if (existsSync(outPath) && !opts.force) {
7529
+ process.stderr.write(
7530
+ `Token file already exists at ${outPath}.
7531
+ Run with --force to overwrite.
7532
+ `
7533
+ );
7534
+ process.exit(1);
7535
+ }
7536
+ const rootDir = process.cwd();
7537
+ const detected = detectProject(rootDir);
7538
+ const tailwindTokens = extractTailwindTokens(detected.tokenSources);
7539
+ const cssResult = extractCSSCustomProperties(detected.tokenSources);
7540
+ const mergedTokens = {};
7541
+ const mergedThemes = {};
7542
+ if (tailwindTokens !== null) {
7543
+ Object.assign(mergedTokens, tailwindTokens);
7544
+ }
7545
+ if (cssResult !== null) {
7546
+ for (const [key, value] of Object.entries(cssResult.tokens)) {
7547
+ if (!(key in mergedTokens)) {
7548
+ mergedTokens[key] = value;
7549
+ }
7550
+ }
7551
+ for (const [themeName, overrides] of Object.entries(cssResult.themes)) {
7552
+ if (!mergedThemes[themeName]) mergedThemes[themeName] = {};
7553
+ Object.assign(mergedThemes[themeName], overrides);
7554
+ }
7555
+ }
7556
+ const tokenFile = {
7557
+ $schema: "https://raw.githubusercontent.com/FlatFilers/Scope/main/packages/tokens/schema.json",
7558
+ version: "1.0.0",
7559
+ meta: {
7560
+ name: "Design Tokens",
7561
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString().split("T")[0]
7562
+ },
7563
+ tokens: mergedTokens
7564
+ };
7565
+ if (Object.keys(mergedThemes).length > 0) {
7566
+ tokenFile.themes = mergedThemes;
7567
+ }
7568
+ writeFileSync(outPath, `${JSON.stringify(tokenFile, null, 2)}
7569
+ `);
7570
+ const tokenGroupCount = Object.keys(mergedTokens).length;
7571
+ const themeNames = Object.keys(mergedThemes);
7572
+ if (detected.tokenSources.length > 0) {
7573
+ process.stdout.write("Detected token sources:\n");
7574
+ for (const source of detected.tokenSources) {
7575
+ process.stdout.write(` ${source.kind}: ${source.path}
7576
+ `);
7577
+ }
7578
+ process.stdout.write("\n");
7579
+ }
7580
+ if (tokenGroupCount > 0) {
7581
+ process.stdout.write(`Extracted ${tokenGroupCount} token group(s) \u2192 ${outPath}
7582
+ `);
7583
+ if (themeNames.length > 0) {
7584
+ for (const name of themeNames) {
7585
+ const count = Object.keys(mergedThemes[name] ?? {}).length;
7586
+ process.stdout.write(` theme "${name}": ${count} override(s)
7587
+ `);
7588
+ }
7589
+ }
7590
+ } else {
7591
+ process.stdout.write(
7592
+ `No token sources detected. Created empty token file \u2192 ${outPath}
7593
+ Add tokens manually or re-run after configuring a design system.
7594
+ `
7595
+ );
7596
+ }
7597
+ } catch (err) {
7598
+ process.stderr.write(`Error: ${err instanceof Error ? err.message : String(err)}
7599
+ `);
7600
+ process.exit(1);
7601
+ }
7602
+ });
7603
+ }
6110
7604
  var DEFAULT_STYLES_PATH3 = ".reactscope/compliance-styles.json";
6111
7605
  var DEFAULT_MANIFEST_PATH = ".reactscope/manifest.json";
6112
7606
  var DEFAULT_OUTPUT_DIR2 = ".reactscope/previews";
6113
7607
  async function renderComponentWithCssOverride(filePath, componentName, cssOverride, vpWidth, vpHeight, timeoutMs) {
7608
+ const PAD = 16;
6114
7609
  const htmlHarness = await buildComponentHarness(
6115
7610
  filePath,
6116
7611
  componentName,
6117
7612
  {},
6118
7613
  // no props
6119
7614
  vpWidth,
6120
- cssOverride
7615
+ cssOverride,
6121
7616
  // injected as <style>
7617
+ void 0,
7618
+ PAD
6122
7619
  );
6123
7620
  const pool = new BrowserPool({
6124
7621
  size: { browsers: 1, pagesPerBrowser: 1 },
@@ -6139,7 +7636,6 @@ async function renderComponentWithCssOverride(filePath, componentName, cssOverri
6139
7636
  );
6140
7637
  const rootLocator = page.locator("[data-reactscope-root]");
6141
7638
  const bb = await rootLocator.boundingBox();
6142
- const PAD = 16;
6143
7639
  const MIN_W = 320;
6144
7640
  const MIN_H = 120;
6145
7641
  const clipX = Math.max(0, (bb?.x ?? 0) - PAD);
@@ -6328,8 +7824,8 @@ function registerPreview(tokensCmd) {
6328
7824
  }
6329
7825
 
6330
7826
  // src/tokens/commands.ts
6331
- var DEFAULT_TOKEN_FILE2 = "reactscope.tokens.json";
6332
- var CONFIG_FILE2 = "reactscope.config.json";
7827
+ var DEFAULT_TOKEN_FILE3 = "reactscope.tokens.json";
7828
+ var CONFIG_FILE3 = "reactscope.config.json";
6333
7829
  function isTTY2() {
6334
7830
  return process.stdout.isTTY === true;
6335
7831
  }
@@ -6351,7 +7847,7 @@ function resolveTokenFilePath(fileFlag) {
6351
7847
  if (fileFlag !== void 0) {
6352
7848
  return resolve(process.cwd(), fileFlag);
6353
7849
  }
6354
- const configPath = resolve(process.cwd(), CONFIG_FILE2);
7850
+ const configPath = resolve(process.cwd(), CONFIG_FILE3);
6355
7851
  if (existsSync(configPath)) {
6356
7852
  try {
6357
7853
  const raw = readFileSync(configPath, "utf-8");
@@ -6363,7 +7859,7 @@ function resolveTokenFilePath(fileFlag) {
6363
7859
  } catch {
6364
7860
  }
6365
7861
  }
6366
- return resolve(process.cwd(), DEFAULT_TOKEN_FILE2);
7862
+ return resolve(process.cwd(), DEFAULT_TOKEN_FILE3);
6367
7863
  }
6368
7864
  function loadTokens(absPath) {
6369
7865
  if (!existsSync(absPath)) {
@@ -6692,6 +8188,7 @@ function createTokensCommand() {
6692
8188
  const tokensCmd = new Command("tokens").description(
6693
8189
  'Query, validate, and export design tokens from reactscope.tokens.json.\n\nTOKEN FILE RESOLUTION (in priority order):\n 1. --file <path> explicit override\n 2. tokens.file in reactscope.config.json\n 3. reactscope.tokens.json default (project root)\n\nTOKEN FILE FORMAT (reactscope.tokens.json):\n Nested JSON. Each leaf is a token with { value, type } or just a raw value.\n Paths use dot notation: color.primary.500, spacing.4, font.size.base\n References use {path.to.other.token} syntax.\n Themes: top-level "themes" key with named override maps.\n\nTOKEN TYPES: color | spacing | typography | shadow | radius | opacity | other\n\nExamples:\n scope tokens validate\n scope tokens list color\n scope tokens get color.primary.500\n scope tokens compliance\n scope tokens export --format css --out tokens.css'
6694
8190
  );
8191
+ registerTokensInit(tokensCmd);
6695
8192
  registerGet2(tokensCmd);
6696
8193
  registerList2(tokensCmd);
6697
8194
  registerSearch(tokensCmd);
@@ -6707,7 +8204,7 @@ function createTokensCommand() {
6707
8204
  // src/program.ts
6708
8205
  function createProgram(options = {}) {
6709
8206
  const program = new Command("scope").version(options.version ?? "0.1.0").description(
6710
- 'Scope \u2014 static analysis + visual rendering toolkit for React component libraries.\n\nScope answers questions about React codebases \u2014 structure, props, visual output,\ndesign token compliance \u2014 without running the full application.\n\nQUICKSTART (new project):\n scope init # detect config, scaffold reactscope.config.json\n scope doctor # verify setup before doing anything else\n scope manifest generate # scan source and build component manifest\n scope render all # screenshot every component\n scope site build # build HTML gallery\n scope site serve # open at http://localhost:3000\n\nQUICKSTART (existing project / CI):\n scope ci # manifest \u2192 render \u2192 compliance \u2192 regression in one step\n\nAGENT BOOTSTRAP:\n scope get-skill # print SKILL.md to stdout \u2014 pipe into agent context\n\nCONFIG FILE: reactscope.config.json (created by `scope init`)\n components.include glob patterns for component files (e.g. "src/**/*.tsx")\n components.wrappers providers and globalCSS to wrap every render\n render.viewport default viewport width\xD7height in px\n tokens.file path to reactscope.tokens.json (default)\n output.dir output root (default: .reactscope/)\n ci.complianceThreshold fail threshold for `scope ci` (default: 0.90)\n\nOUTPUT DIRECTORY: .reactscope/\n manifest.json component registry \u2014 updated by `scope manifest generate`\n renders/<Name>/ PNGs + render.json per component\n compliance-styles.json computed-style map for token matching\n site/ static HTML gallery (built by `scope site build`)\n\nRun `scope <command> --help` for detailed flags and examples.'
8207
+ 'Scope \u2014 static analysis + visual rendering toolkit for React component libraries.\n\nScope answers questions about React codebases \u2014 structure, props, visual output,\ndesign token compliance \u2014 without running the full application.\n\nAGENT QUICKSTART:\n scope get-skill > /tmp/scope-skill.md\n scope init --yes\n scope doctor --json\n scope manifest list --format json\n scope render all --format json --output-dir .reactscope/renders\n scope site build --output .reactscope/site\n scope instrument profile http://localhost:5173\n\nCI QUICKSTART:\n scope ci --json --output .reactscope/ci-result.json\n\nCONFIG FILE: reactscope.config.json (created by `scope init`)\n components.include glob patterns for component files (e.g. "src/**/*.tsx")\n components.wrappers providers and globalCSS to wrap every render\n render.viewport default viewport width\xD7height in px\n tokens.file path to reactscope.tokens.json (default)\n output.dir output root (default: .reactscope/)\n ci.complianceThreshold fail threshold for `scope ci` (default: 0.90)\n\nOUTPUT DIRECTORY: .reactscope/\n manifest.json component registry \u2014 updated by `scope manifest generate`\n renders/<Name>/ PNGs + render.json per component\n compliance-styles.json computed-style map for token matching\n site/ static HTML gallery (built by `scope site build`)\n\nRun `scope <command> --help` for detailed flags and examples.'
6711
8208
  );
6712
8209
  program.command("capture <url>").description(
6713
8210
  "Capture the live React component tree from a running app and emit it as JSON.\nRequires a running dev server at the given URL (e.g. http://localhost:5173).\n\nExamples:\n scope capture http://localhost:5173\n scope capture http://localhost:5173 -o report.json --pretty\n scope capture http://localhost:5173 --timeout 15000 --wait 500"