impeccable 3.6.0 → 3.6.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/README.md +3 -3
- package/README.npm.md +3 -3
- package/README.repo.md +39 -11
- package/cli/bin/commands/ignores.mjs +4 -0
- package/cli/bin/commands/skills.mjs +135 -57
- package/cli/engine/browser/injected/index.mjs +136 -5
- package/cli/engine/cli/main.mjs +10 -16
- package/cli/engine/design-system.mjs +199 -9
- package/cli/engine/detect-antipatterns-browser.js +350 -37
- package/cli/engine/engines/browser/detect-url.mjs +64 -2
- package/cli/engine/engines/regex/detect-text.mjs +155 -7
- package/cli/engine/engines/static-html/css-cascade.mjs +56 -7
- package/cli/engine/engines/static-html/detect-html.mjs +3 -5
- package/cli/engine/registry/antipatterns.mjs +18 -0
- package/cli/engine/rules/checks.mjs +175 -30
- package/cli/engine/shared/color.mjs +8 -0
- package/cli/engine/shared/constants.mjs +17 -2
- package/cli/lib/download-providers.js +1 -0
- package/package.json +2 -2
|
@@ -626,7 +626,7 @@ if (IS_BROWSER) {
|
|
|
626
626
|
if (currentStyle.filter && currentStyle.filter !== 'none') reasons.add('filter');
|
|
627
627
|
if (currentStyle.backdropFilter && currentStyle.backdropFilter !== 'none') reasons.add('backdrop filter');
|
|
628
628
|
|
|
629
|
-
const solidBg = parseRgb(currentStyle.backgroundColor);
|
|
629
|
+
const solidBg = parseRgb(currentStyle.backgroundColor) || parseAnyColor(currentStyle.backgroundColor);
|
|
630
630
|
if (solidBg && solidBg.a >= 0.95 && (!bgImage || bgImage === 'none')) break;
|
|
631
631
|
current = current.parentElement;
|
|
632
632
|
}
|
|
@@ -688,7 +688,7 @@ if (IS_BROWSER) {
|
|
|
688
688
|
// starve the url()-backed texts this mode exists to sample.
|
|
689
689
|
if (options.imageOnly && !reasons.includes('image background')) continue;
|
|
690
690
|
|
|
691
|
-
const textColor = parseRgb(style.color);
|
|
691
|
+
const textColor = parseRgb(style.color) || parseAnyColor(style.color);
|
|
692
692
|
const fontSize = parseFloat(style.fontSize) || 16;
|
|
693
693
|
const fontWeight = parseInt(style.fontWeight) || 400;
|
|
694
694
|
const isLargeText = fontSize >= WCAG_LARGE_TEXT_PX || (fontSize >= WCAG_LARGE_BOLD_TEXT_PX && fontWeight >= 700);
|
|
@@ -985,7 +985,7 @@ if (IS_BROWSER) {
|
|
|
985
985
|
return sample;
|
|
986
986
|
}
|
|
987
987
|
}
|
|
988
|
-
const bg = parseRgb(style.backgroundColor);
|
|
988
|
+
const bg = parseRgb(style.backgroundColor) || parseAnyColor(style.backgroundColor);
|
|
989
989
|
if (bg && bg.a > 0.05) return { status: 'sampled', color: bg, method: 'solid-background' };
|
|
990
990
|
return { status: 'unresolved', reason: 'no readable background' };
|
|
991
991
|
}
|
|
@@ -1115,7 +1115,7 @@ if (IS_BROWSER) {
|
|
|
1115
1115
|
}
|
|
1116
1116
|
|
|
1117
1117
|
const style = getComputedStyle(el);
|
|
1118
|
-
const textColor = parseRgb(style.color) || candidate.textColor;
|
|
1118
|
+
const textColor = parseRgb(style.color) || parseAnyColor(style.color) || candidate.textColor;
|
|
1119
1119
|
if (!textColor) return { ...candidate, status: 'unresolved', confidence: 'none', reason: 'unreadable text color' };
|
|
1120
1120
|
|
|
1121
1121
|
const rect = getDirectTextRect(el) || el.getBoundingClientRect();
|
|
@@ -1472,7 +1472,19 @@ if (IS_BROWSER) {
|
|
|
1472
1472
|
return findings;
|
|
1473
1473
|
}
|
|
1474
1474
|
|
|
1475
|
+
// A page matched by detector.ignoreFiles is waived wholesale: every scan
|
|
1476
|
+
// stage answers empty so the badge and toast read zero. Mirrors
|
|
1477
|
+
// shouldIgnoreDetectionFile in cli/lib/impeccable-config.mjs; the live
|
|
1478
|
+
// overlay resolves the globs per page (live-browser-ignores.js) and
|
|
1479
|
+
// forwards the verdict as config.skipScan.
|
|
1480
|
+
function skipScanActive() {
|
|
1481
|
+
return EXTENSION_MODE && window.__IMPECCABLE_CONFIG__?.skipScan === true;
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1475
1484
|
function collectBrowserFindings() {
|
|
1485
|
+
if (skipScanActive()) {
|
|
1486
|
+
return { groupMap: new Map(), allFindings: [], pageLevelFindings: [] };
|
|
1487
|
+
}
|
|
1476
1488
|
const groupMap = new Map();
|
|
1477
1489
|
const _disabled = EXTENSION_MODE ? (window.__IMPECCABLE_CONFIG__?.disabledRules || []) : [];
|
|
1478
1490
|
const _ruleOk = (id) => !_disabled.length || !_disabled.includes(id);
|
|
@@ -1675,6 +1687,119 @@ if (IS_BROWSER) {
|
|
|
1675
1687
|
addBrowserFindings(groupMap, document.body, mapped);
|
|
1676
1688
|
}
|
|
1677
1689
|
|
|
1690
|
+
// Value-level suppression (issue #639). `disabledRules` above handles
|
|
1691
|
+
// whole rules; this applies the config's remaining ignoreValues entries,
|
|
1692
|
+
// which the CLI filters through isIgnoredFindingValue in
|
|
1693
|
+
// cli/lib/impeccable-config.mjs, so a project waiver like
|
|
1694
|
+
// overused-font = "geist mono" reaches the overlay and extension too.
|
|
1695
|
+
const _normValue = (v) => String(v || '').trim().replace(/^["']|["']$/g, '')
|
|
1696
|
+
.replace(/\+/g, ' ').replace(/\s+/g, ' ').toLowerCase();
|
|
1697
|
+
const _disabledValues = EXTENSION_MODE
|
|
1698
|
+
? (Array.isArray(window.__IMPECCABLE_CONFIG__?.disabledValues) ? window.__IMPECCABLE_CONFIG__.disabledValues : [])
|
|
1699
|
+
.filter(e => e && typeof e === 'object' && e.rule && e.value)
|
|
1700
|
+
.map(e => ({ rule: String(e.rule).trim().toLowerCase(), value: _normValue(e.value) }))
|
|
1701
|
+
: [];
|
|
1702
|
+
if (_disabledValues.length > 0) {
|
|
1703
|
+
// The six rules whose findings carry a matchable value; keep in step
|
|
1704
|
+
// with extractFindingIgnoreValue in cli/lib/impeccable-config.mjs.
|
|
1705
|
+
// Everything else is suppressed by rule or by file scope, both already
|
|
1706
|
+
// resolved into disabledRules before the scan message was sent.
|
|
1707
|
+
const _directValueRules = new Set([
|
|
1708
|
+
'overused-font',
|
|
1709
|
+
'bounce-easing',
|
|
1710
|
+
'design-system-font',
|
|
1711
|
+
'design-system-color',
|
|
1712
|
+
'design-system-radius',
|
|
1713
|
+
'design-system-font-size',
|
|
1714
|
+
]);
|
|
1715
|
+
// The design-system checks set `ignoreValue` on their findings; the
|
|
1716
|
+
// detail fallbacks catch overused-font, whose value lives in its
|
|
1717
|
+
// sentence. One CLI matcher is not mirrored here: the motion extractor
|
|
1718
|
+
// (a value-scoped bounce-easing waiver only matches when the finding
|
|
1719
|
+
// carries ignoreValue directly). The CLI's [?&]family= URL fallback is
|
|
1720
|
+
// also omitted on purpose: browser findings for these rules always
|
|
1721
|
+
// carry ignoreValue or a "Primary font:" / "Google Fonts:" /
|
|
1722
|
+
// font-family sentence, so it is unreachable here.
|
|
1723
|
+
const _findingValue = (f) => {
|
|
1724
|
+
if (!f || !_directValueRules.has(f.type || f.id)) return '';
|
|
1725
|
+
const direct = f.ignoreValue || f.value;
|
|
1726
|
+
if (direct) return _normValue(direct);
|
|
1727
|
+
// The CLI routes bounce-easing through extractMotionIgnoreValue and
|
|
1728
|
+
// never the font regexes; without a direct ignoreValue there is no
|
|
1729
|
+
// value to match, so do not invent one from unrelated CSS text.
|
|
1730
|
+
if ((f.type || f.id) === 'bounce-easing') return '';
|
|
1731
|
+
for (const text of [f.detail, f.snippet]) {
|
|
1732
|
+
if (typeof text !== 'string' || !text) continue;
|
|
1733
|
+
const primary = text.match(/Primary font:\s*([^()\n;]+)/i);
|
|
1734
|
+
if (primary) return _normValue(primary[1]);
|
|
1735
|
+
const google = text.match(/Google Fonts:\s*([^()\n;]+)/i);
|
|
1736
|
+
if (google) return _normValue(google[1]);
|
|
1737
|
+
const family = text.match(/font-family\s*:\s*["']?([^'",;\n]+)/i);
|
|
1738
|
+
if (family) return _normValue(family[1]);
|
|
1739
|
+
}
|
|
1740
|
+
return '';
|
|
1741
|
+
};
|
|
1742
|
+
// design-system-color compares by color value, not by spelling: the
|
|
1743
|
+
// browser reports computed rgb(...) strings while waivers are usually
|
|
1744
|
+
// written as hex. Mirrors ignoreValueMatches -> colorIgnoreKey in
|
|
1745
|
+
// cli/lib/impeccable-config.mjs for the hex and rgb()/rgba() forms;
|
|
1746
|
+
// hsl stays CLI-only.
|
|
1747
|
+
const _colorKey = (value) => {
|
|
1748
|
+
const text = String(value || '').trim().toLowerCase();
|
|
1749
|
+
const hex = text.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/);
|
|
1750
|
+
if (hex) {
|
|
1751
|
+
const expanded = hex[1].length <= 4 ? [...hex[1]].map(d => d + d).join('') : hex[1];
|
|
1752
|
+
const [r, g, b, a = 255] = expanded.match(/../g).map(ch => parseInt(ch, 16));
|
|
1753
|
+
return `${r},${g},${b},${a}`;
|
|
1754
|
+
}
|
|
1755
|
+
const rgb = text.match(/^rgba?\((.*)\)$/);
|
|
1756
|
+
if (!rgb) return '';
|
|
1757
|
+
const body = rgb[1].trim().replace(/\s*\/\s*/g, ' / ');
|
|
1758
|
+
let parts;
|
|
1759
|
+
if (body.includes(',')) {
|
|
1760
|
+
parts = body.split(',').map(p => p.trim()).filter(Boolean);
|
|
1761
|
+
const last = parts[parts.length - 1];
|
|
1762
|
+
if (last && last.includes('/')) {
|
|
1763
|
+
parts = [...parts.slice(0, -1), ...last.split('/').map(p => p.trim()).filter(Boolean)];
|
|
1764
|
+
}
|
|
1765
|
+
} else {
|
|
1766
|
+
parts = body.split(/\s+/).filter(p => p && p !== '/');
|
|
1767
|
+
}
|
|
1768
|
+
if (parts.length < 3 || parts.length > 4) return '';
|
|
1769
|
+
const channel = (raw, isAlpha) => {
|
|
1770
|
+
const m = String(raw).trim().match(/^(-?\d*\.?\d+)(%)?$/);
|
|
1771
|
+
if (!m) return null;
|
|
1772
|
+
let v = parseFloat(m[1]);
|
|
1773
|
+
if (m[2]) v = isAlpha ? v / 100 : v * 2.55;
|
|
1774
|
+
const max = isAlpha ? 1 : 255;
|
|
1775
|
+
if (!Number.isFinite(v) || v < 0 || v > max) return null;
|
|
1776
|
+
return isAlpha ? v : Math.round(v);
|
|
1777
|
+
};
|
|
1778
|
+
const r = channel(parts[0], false);
|
|
1779
|
+
const g = channel(parts[1], false);
|
|
1780
|
+
const b = channel(parts[2], false);
|
|
1781
|
+
const a = parts[3] === undefined ? 1 : channel(parts[3], true);
|
|
1782
|
+
if ([r, g, b, a].some(v => v === null)) return '';
|
|
1783
|
+
return `${r},${g},${b},${Math.round(a * 255)}`;
|
|
1784
|
+
};
|
|
1785
|
+
const _valueIgnored = (f) => {
|
|
1786
|
+
const value = _findingValue(f);
|
|
1787
|
+
if (!value) return false;
|
|
1788
|
+
const rule = f.type || f.id;
|
|
1789
|
+
return _disabledValues.some(e => e.rule === rule && (e.value === value
|
|
1790
|
+
|| (rule === 'design-system-color'
|
|
1791
|
+
&& _colorKey(e.value) !== '' && _colorKey(e.value) === _colorKey(value))));
|
|
1792
|
+
};
|
|
1793
|
+
for (const [el, list] of [...groupMap.entries()]) {
|
|
1794
|
+
const kept = list.filter(f => !_valueIgnored(f));
|
|
1795
|
+
if (kept.length > 0) groupMap.set(el, kept);
|
|
1796
|
+
else groupMap.delete(el);
|
|
1797
|
+
}
|
|
1798
|
+
for (let i = pageLevelFindings.length - 1; i >= 0; i--) {
|
|
1799
|
+
if (_valueIgnored(pageLevelFindings[i])) pageLevelFindings.splice(i, 1);
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1678
1803
|
return {
|
|
1679
1804
|
groupMap,
|
|
1680
1805
|
allFindings: browserFindingsFromMap(groupMap),
|
|
@@ -1892,6 +2017,12 @@ if (IS_BROWSER) {
|
|
|
1892
2017
|
|
|
1893
2018
|
async function collectBrowserFindingsAsync(options = {}, runtime = {}) {
|
|
1894
2019
|
const collected = collectBrowserFindings();
|
|
2020
|
+
// The visual pass walks the DOM on its own; on a skipScan page it would
|
|
2021
|
+
// repopulate the emptied scan, so it is skipped with everything else.
|
|
2022
|
+
if (skipScanActive()) {
|
|
2023
|
+
lastVisualContrastAnalyses = [];
|
|
2024
|
+
return { ...collected, allFindings: [], visualContrastAnalyses: [] };
|
|
2025
|
+
}
|
|
1895
2026
|
await addVisualContrastFindings(collected.groupMap, options, runtime);
|
|
1896
2027
|
return {
|
|
1897
2028
|
...collected,
|
|
@@ -1945,7 +2076,7 @@ if (IS_BROWSER) {
|
|
|
1945
2076
|
const generation = scanGeneration;
|
|
1946
2077
|
const collected = collectBrowserFindings();
|
|
1947
2078
|
const allFindings = renderBrowserFindings(collected, options);
|
|
1948
|
-
if (shouldRunVisualContrast(options)) {
|
|
2079
|
+
if (!skipScanActive() && shouldRunVisualContrast(options)) {
|
|
1949
2080
|
addVisualContrastFindings(collected.groupMap, options, { decorate: true, generation })
|
|
1950
2081
|
.then(() => {
|
|
1951
2082
|
if (generation === scanGeneration) postSerializedFindings(collected.groupMap, options);
|
package/cli/engine/cli/main.mjs
CHANGED
|
@@ -105,6 +105,13 @@ function formatFindings(findings, jsonMode) {
|
|
|
105
105
|
// `optionsFor` maps a local path to scan options carrying that path's own
|
|
106
106
|
// project design system (or base options when null). Falls back to a plain
|
|
107
107
|
// object so direct/legacy callers still work.
|
|
108
|
+
async function detectLocalFile(filePath, options) {
|
|
109
|
+
if (HTML_EXTENSIONS.has(path.extname(filePath).toLowerCase())) {
|
|
110
|
+
return detectHtml(filePath, options);
|
|
111
|
+
}
|
|
112
|
+
return detectText(fs.readFileSync(filePath, 'utf-8'), filePath, options);
|
|
113
|
+
}
|
|
114
|
+
|
|
108
115
|
async function handleStdin(optionsFor = () => ({})) {
|
|
109
116
|
const resolve = typeof optionsFor === 'function' ? optionsFor : () => optionsFor;
|
|
110
117
|
const chunks = [];
|
|
@@ -114,9 +121,7 @@ async function handleStdin(optionsFor = () => ({})) {
|
|
|
114
121
|
const parsed = JSON.parse(input);
|
|
115
122
|
const fp = parsed?.tool_input?.file_path;
|
|
116
123
|
if (fp && fs.existsSync(fp)) {
|
|
117
|
-
|
|
118
|
-
return HTML_EXTENSIONS.has(path.extname(fp).toLowerCase())
|
|
119
|
-
? detectHtml(fp, options) : detectText(fs.readFileSync(fp, 'utf-8'), fp, options);
|
|
124
|
+
return detectLocalFile(fp, resolve(fp));
|
|
120
125
|
}
|
|
121
126
|
} catch { /* not JSON */ }
|
|
122
127
|
return detectText(input, '<stdin>', resolve(null));
|
|
@@ -374,16 +379,10 @@ async function detectCli() {
|
|
|
374
379
|
}
|
|
375
380
|
|
|
376
381
|
for (const file of files) {
|
|
377
|
-
const ext = path.extname(file).toLowerCase();
|
|
378
382
|
// Each file resolves its own project design system (cached by root),
|
|
379
383
|
// so a scan spanning sibling projects applies the right rules per file.
|
|
380
384
|
const fileOptions = scanOptionsFor(file);
|
|
381
|
-
|
|
382
|
-
if (HTML_EXTENSIONS.has(ext)) {
|
|
383
|
-
fileFindings = await detectHtml(file, fileOptions);
|
|
384
|
-
} else {
|
|
385
|
-
fileFindings = detectText(fs.readFileSync(file, 'utf-8'), file, fileOptions);
|
|
386
|
-
}
|
|
385
|
+
const fileFindings = await detectLocalFile(file, fileOptions);
|
|
387
386
|
// Annotate findings with import context
|
|
388
387
|
const importers = importedByMap.get(file);
|
|
389
388
|
if (importers && importers.size > 0) {
|
|
@@ -396,13 +395,8 @@ async function detectCli() {
|
|
|
396
395
|
}
|
|
397
396
|
} else if (stat.isFile()) {
|
|
398
397
|
if (shouldIgnoreDetectionFile(resolved, process.cwd(), detectionConfig)) continue;
|
|
399
|
-
const ext = path.extname(resolved).toLowerCase();
|
|
400
398
|
const fileOptions = scanOptionsFor(resolved);
|
|
401
|
-
|
|
402
|
-
allFindings.push(...await detectHtml(resolved, fileOptions));
|
|
403
|
-
} else {
|
|
404
|
-
allFindings.push(...detectText(fs.readFileSync(resolved, 'utf-8'), resolved, fileOptions));
|
|
405
|
-
}
|
|
399
|
+
allFindings.push(...await detectLocalFile(resolved, fileOptions));
|
|
406
400
|
}
|
|
407
401
|
}
|
|
408
402
|
} finally {
|
|
@@ -13,6 +13,11 @@ const FALLBACK_DIRS = ['.agents/context', 'docs'];
|
|
|
13
13
|
// CLI can't import (separate tree). `.git` and `package.json` are the common
|
|
14
14
|
// boundaries; `.impeccable` is our own project marker.
|
|
15
15
|
const PROJECT_ROOT_MARKERS = ['.git', 'package.json', '.impeccable'];
|
|
16
|
+
// Monorepo-root recognition, mirroring context.mjs's isMonorepoRoot: declared
|
|
17
|
+
// workspace globs (package.json `workspaces`, pnpm-workspace.yaml `packages:`)
|
|
18
|
+
// or a marker file beside apps/ or packages/ children.
|
|
19
|
+
const MONOREPO_MARKER_FILES = ['pnpm-workspace.yaml', 'turbo.json', 'nx.json', 'lerna.json'];
|
|
20
|
+
const MONOREPO_FALLBACK_PROJECT_DIRS = ['apps', 'packages'];
|
|
16
21
|
const COLOR_CHANNEL_TOLERANCE = 6;
|
|
17
22
|
// Shadow blacks at different alphas are different tokens (0.28 vs 0.55 is the
|
|
18
23
|
// difference between a documented shadow and drift), so shadow matching cannot
|
|
@@ -575,14 +580,179 @@ function designSystemStartDir(targetPath, cwd = process.cwd()) {
|
|
|
575
580
|
}
|
|
576
581
|
}
|
|
577
582
|
|
|
583
|
+
// Same two groups as context.mjs's readProjectPatternGroups: Impeccable
|
|
584
|
+
// projectRoots govern any path they match (positive or negated); package-manager
|
|
585
|
+
// globs only apply to paths the Impeccable group does not match.
|
|
586
|
+
function readWorkspacePatternGroups(dir) {
|
|
587
|
+
const impeccable = [];
|
|
588
|
+
for (const name of ['config.json', 'config.local.json']) {
|
|
589
|
+
const roots = safeReadJson(path.join(dir, '.impeccable', name))?.projectRoots;
|
|
590
|
+
if (Array.isArray(roots)) {
|
|
591
|
+
impeccable.push(...roots.filter(entry => typeof entry === 'string' && entry.trim()).map(entry => entry.trim()));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const pkg = [];
|
|
595
|
+
const workspaces = safeReadJson(path.join(dir, 'package.json'))?.workspaces;
|
|
596
|
+
if (Array.isArray(workspaces)) pkg.push(...workspaces);
|
|
597
|
+
else if (Array.isArray(workspaces?.packages)) pkg.push(...workspaces.packages);
|
|
598
|
+
const lernaPackages = safeReadJson(path.join(dir, 'lerna.json'))?.packages;
|
|
599
|
+
if (Array.isArray(lernaPackages)) pkg.push(...lernaPackages);
|
|
600
|
+
try {
|
|
601
|
+
let inPackages = false;
|
|
602
|
+
for (const line of fs.readFileSync(path.join(dir, 'pnpm-workspace.yaml'), 'utf-8').split(/\r?\n/)) {
|
|
603
|
+
const trimmed = stripInlineYamlComment(line).trim();
|
|
604
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
605
|
+
const flow = trimmed.match(/^packages:\s*\[(.*)\]\s*$/);
|
|
606
|
+
if (flow) {
|
|
607
|
+
pkg.push(...flow[1].split(',').map(entry => entry.trim().replace(/^['"]|['"]$/g, '')).filter(Boolean));
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
if (/^packages:\s*$/.test(trimmed)) { inPackages = true; continue; }
|
|
611
|
+
if (!inPackages) continue;
|
|
612
|
+
const item = trimmed.match(/^-\s*(.+)$/);
|
|
613
|
+
if (item) pkg.push(item[1].trim().replace(/^['"]|['"]$/g, ''));
|
|
614
|
+
else if (/^[A-Za-z0-9_-]+:\s*/.test(trimmed)) break;
|
|
615
|
+
}
|
|
616
|
+
} catch { /* no pnpm-workspace.yaml */ }
|
|
617
|
+
return [impeccable, pkg];
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function readWorkspacePatterns(dir) {
|
|
621
|
+
return readWorkspacePatternGroups(dir).flat();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function isMonorepoRoot(dir) {
|
|
625
|
+
if (readWorkspacePatterns(dir).some(pattern => !String(pattern).trim().startsWith('!'))) return true;
|
|
626
|
+
if (!MONOREPO_MARKER_FILES.some(file => fs.existsSync(path.join(dir, file)))) return false;
|
|
627
|
+
return MONOREPO_FALLBACK_PROJECT_DIRS.some(name => {
|
|
628
|
+
try {
|
|
629
|
+
return fs.readdirSync(path.join(dir, name), { withFileTypes: true }).some(entry => entry.isDirectory());
|
|
630
|
+
} catch {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function monorepoOwnsPath(root, boundaryDir) {
|
|
637
|
+
const rel = path.relative(root, boundaryDir);
|
|
638
|
+
if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return false;
|
|
639
|
+
const relSegments = rel.split(path.sep).filter(Boolean);
|
|
640
|
+
|
|
641
|
+
function normalizeWorkspacePattern(pattern) {
|
|
642
|
+
return String(pattern || '')
|
|
643
|
+
.trim()
|
|
644
|
+
.replace(/^['"]|['"]$/g, '')
|
|
645
|
+
.replace(/^\.\//, '')
|
|
646
|
+
.replace(/\/+$/, '');
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function escapeRegExp(s) {
|
|
650
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function segmentMatches(patternSegment, relSegment) {
|
|
654
|
+
if (patternSegment === '*') return true;
|
|
655
|
+
if (!patternSegment.includes('*')) return patternSegment === relSegment;
|
|
656
|
+
const re = new RegExp(`^${escapeRegExp(patternSegment).replace(/\\\*/g, '[^/]*')}$`);
|
|
657
|
+
return re.test(relSegment);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function matchGlobSegments(patternSegments, relSegments) {
|
|
661
|
+
function rec(pi, ri) {
|
|
662
|
+
if (pi === patternSegments.length) return ri === relSegments.length;
|
|
663
|
+
if (patternSegments[pi] === '**') {
|
|
664
|
+
if (pi === patternSegments.length - 1) return true;
|
|
665
|
+
for (let k = ri; k <= relSegments.length; k++) {
|
|
666
|
+
if (rec(pi + 1, k)) return true;
|
|
667
|
+
}
|
|
668
|
+
return false;
|
|
669
|
+
}
|
|
670
|
+
if (ri >= relSegments.length) return false;
|
|
671
|
+
if (!segmentMatches(patternSegments[pi], relSegments[ri])) return false;
|
|
672
|
+
return rec(pi + 1, ri + 1);
|
|
673
|
+
}
|
|
674
|
+
return rec(0, 0);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// Negations like !packages/excluded must also cover nested dirs under that path.
|
|
678
|
+
function matchesNegation(pattern) {
|
|
679
|
+
const patternSegments = normalizeWorkspacePattern(pattern).split('/').filter(Boolean);
|
|
680
|
+
if (!patternSegments.length) return false;
|
|
681
|
+
if (patternSegments.includes('**')) return matchGlobSegments(patternSegments, relSegments);
|
|
682
|
+
if (relSegments.length < patternSegments.length) return false;
|
|
683
|
+
for (let i = 0; i < patternSegments.length; i++) {
|
|
684
|
+
if (!segmentMatches(patternSegments[i], relSegments[i])) return false;
|
|
685
|
+
}
|
|
686
|
+
return true;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// Positive globs identify workspace packages at exact depth (`*` is a direct
|
|
690
|
+
// child). A nested package.json under that package is still owned: the
|
|
691
|
+
// ancestor directory of glob length must itself be a package.
|
|
692
|
+
function positiveOwns(pattern) {
|
|
693
|
+
const patternSegments = normalizeWorkspacePattern(pattern).split('/').filter(Boolean);
|
|
694
|
+
if (!patternSegments.length) return false;
|
|
695
|
+
if (patternSegments.includes('**')) return matchGlobSegments(patternSegments, relSegments);
|
|
696
|
+
if (relSegments.length < patternSegments.length) return false;
|
|
697
|
+
for (let i = 0; i < patternSegments.length; i++) {
|
|
698
|
+
if (!segmentMatches(patternSegments[i], relSegments[i])) return false;
|
|
699
|
+
}
|
|
700
|
+
if (relSegments.length === patternSegments.length) return true;
|
|
701
|
+
const ancestorDir = path.join(root, ...relSegments.slice(0, patternSegments.length));
|
|
702
|
+
return fs.existsSync(path.join(ancestorDir, 'package.json'));
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function groupOwns(rawPatterns) {
|
|
706
|
+
const patterns = rawPatterns.map(normalizeWorkspacePattern).filter(Boolean);
|
|
707
|
+
if (!patterns.length) return null;
|
|
708
|
+
const excluded = patterns.some((pattern) => (
|
|
709
|
+
pattern.startsWith('!') && matchesNegation(pattern.slice(1))
|
|
710
|
+
));
|
|
711
|
+
const included = patterns.filter((pattern) => !pattern.startsWith('!')).some(positiveOwns);
|
|
712
|
+
if (!excluded && !included) return null;
|
|
713
|
+
if (excluded) return false;
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const [impeccable, pkg] = readWorkspacePatternGroups(root);
|
|
718
|
+
const fromImpeccable = groupOwns(impeccable);
|
|
719
|
+
if (fromImpeccable !== null) return fromImpeccable;
|
|
720
|
+
const fromPkg = groupOwns(pkg);
|
|
721
|
+
if (fromPkg !== null) return fromPkg;
|
|
722
|
+
if ([...impeccable, ...pkg].some((pattern) => !normalizeWorkspacePattern(pattern).startsWith('!'))) {
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
725
|
+
return relSegments.length >= 2 && MONOREPO_FALLBACK_PROJECT_DIRS.includes(relSegments[0]);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// Both forms of the home directory. The walk compares path strings, and a
|
|
729
|
+
// symlinked home (e.g. /home -> /var/home) never string-matches the physical
|
|
730
|
+
// paths a cwd-resolved target produces, which would let the post-boundary walk
|
|
731
|
+
// sail through $HOME and inherit from it.
|
|
732
|
+
function homeDirForms() {
|
|
733
|
+
const homeDir = path.resolve(os.homedir());
|
|
734
|
+
const forms = new Set([homeDir]);
|
|
735
|
+
try {
|
|
736
|
+
forms.add(fs.realpathSync(homeDir));
|
|
737
|
+
} catch { /* keep the logical form only */ }
|
|
738
|
+
return forms;
|
|
739
|
+
}
|
|
740
|
+
|
|
578
741
|
// Walk up from `startDir` to the directory that governs the target's design
|
|
579
742
|
// system, mirroring skill/scripts/context.mjs's project-boundary semantics:
|
|
580
743
|
//
|
|
581
744
|
// - A directory carrying a DESIGN.md (directly or in a fallback dir) IS the
|
|
582
745
|
// design root — that's where the rules live.
|
|
583
746
|
// - A directory carrying a project marker (.git / package.json / .impeccable)
|
|
584
|
-
// but no DESIGN.md is a project BOUNDARY
|
|
585
|
-
//
|
|
747
|
+
// but no DESIGN.md is a project BOUNDARY. A nested package.json inherits
|
|
748
|
+
// the ancestor DESIGN.md only when that ancestor's workspace declarations
|
|
749
|
+
// include the path (negations win; a nested package under a matched
|
|
750
|
+
// workspace still inherits). Marker-only roots (turbo/nx/lerna/pnpm
|
|
751
|
+
// with no globs) still own apps/<name> and packages/<name>. A stray nested
|
|
752
|
+
// package that matches no glob does not inherit. This is detect's
|
|
753
|
+
// contamination contract, not skill-context's repoRoot fallback for
|
|
754
|
+
// excluded paths. A nested separate repository (.git with no workspace
|
|
755
|
+
// declaration) still inherits nothing (issue #570).
|
|
586
756
|
// - Reaching the home directory / filesystem root with neither means no
|
|
587
757
|
// design system at all — never process.cwd()'s.
|
|
588
758
|
//
|
|
@@ -590,15 +760,33 @@ function designSystemStartDir(targetPath, cwd = process.cwd()) {
|
|
|
590
760
|
// runs out. This is the fix for cross-project contamination.
|
|
591
761
|
export function findDesignRoot(startDir) {
|
|
592
762
|
let dir = path.resolve(startDir);
|
|
593
|
-
const
|
|
763
|
+
const homeDirs = homeDirForms();
|
|
764
|
+
let boundary = null;
|
|
594
765
|
while (true) {
|
|
595
|
-
if (resolveDesignMdPath(dir)) return { dir, hasDesign: true };
|
|
596
|
-
if (
|
|
597
|
-
|
|
766
|
+
if (!boundary && resolveDesignMdPath(dir)) return { dir, hasDesign: true };
|
|
767
|
+
if (boundary) {
|
|
768
|
+
// Past the boundary the walk only looks for the monorepo root that owns
|
|
769
|
+
// the workspace path (workspace globs including negations, or marker-only
|
|
770
|
+
// apps/packages fallback). Monorepo-root before .git, same order as
|
|
771
|
+
// context.mjs: a workspace root carrying its own .git is still recognized,
|
|
772
|
+
// while a .git that declares no workspaces is a separate repository and
|
|
773
|
+
// stops the walk with nothing inherited. The home directory is never an
|
|
774
|
+
// owning root, same as context.mjs's findMonorepoRoot, which stops at
|
|
775
|
+
// homeDir before its monorepo check.
|
|
776
|
+
if (!homeDirs.has(dir) && isMonorepoRoot(dir)) {
|
|
777
|
+
if (monorepoOwnsPath(dir, boundary.dir)) return { dir, hasDesign: !!resolveDesignMdPath(dir) };
|
|
778
|
+
return boundary;
|
|
779
|
+
}
|
|
780
|
+
if (fs.existsSync(path.join(dir, '.git'))) return boundary;
|
|
781
|
+
} else if (PROJECT_ROOT_MARKERS.some((marker) => fs.existsSync(path.join(dir, marker)))) {
|
|
782
|
+
boundary = { dir, hasDesign: false };
|
|
783
|
+
// A boundary that is itself a monorepo root, or a separate repository
|
|
784
|
+
// with its own .git, inherits nothing from above.
|
|
785
|
+
if (isMonorepoRoot(dir) || fs.existsSync(path.join(dir, '.git'))) return boundary;
|
|
598
786
|
}
|
|
599
|
-
if (dir
|
|
787
|
+
if (homeDirs.has(dir)) return boundary;
|
|
600
788
|
const parent = path.dirname(dir);
|
|
601
|
-
if (parent === dir) return
|
|
789
|
+
if (parent === dir) return boundary;
|
|
602
790
|
dir = parent;
|
|
603
791
|
}
|
|
604
792
|
}
|
|
@@ -807,7 +995,9 @@ function extractRadiusTokens(value) {
|
|
|
807
995
|
return String(value || '')
|
|
808
996
|
.replace(/\s*\/\s*/g, ' ')
|
|
809
997
|
.split(/\s+/)
|
|
810
|
-
|
|
998
|
+
// var() fallbacks leave the closing parenthesis on the final token. Strip
|
|
999
|
+
// it before length resolution so `8px)` is not treated as unitless 8rem.
|
|
1000
|
+
.map(token => token.trim().replace(/\)+$/, ''))
|
|
811
1001
|
.filter(Boolean);
|
|
812
1002
|
}
|
|
813
1003
|
|