-
Notifications
You must be signed in to change notification settings - Fork 233
feat(desktop): add input context and inline comments #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sun-sunshine06
merged 1 commit into
main
from
feat/p0-input-design-system-inline-comment
Apr 18, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import type { Dirent } from 'node:fs'; | ||
| import { readFile, readdir } from 'node:fs/promises'; | ||
| import { basename, extname, join, relative } from 'node:path'; | ||
| import { | ||
| STORED_DESIGN_SYSTEM_SCHEMA_VERSION, | ||
| type StoredDesignSystem, | ||
| } from '@open-codesign/shared'; | ||
|
|
||
| const IGNORED_DIRS = new Set([ | ||
| '.git', | ||
| '.idea', | ||
| '.next', | ||
| '.turbo', | ||
| '.vscode', | ||
| 'build', | ||
| 'coverage', | ||
| 'dist', | ||
| 'node_modules', | ||
| 'out', | ||
| ]); | ||
|
|
||
| const CANDIDATE_EXTS = new Set([ | ||
| '.css', | ||
| '.scss', | ||
| '.sass', | ||
| '.less', | ||
| '.js', | ||
| '.jsx', | ||
| '.ts', | ||
| '.tsx', | ||
| '.json', | ||
| '.md', | ||
| ]); | ||
|
|
||
| const PRIORITY_PATTERNS = [ | ||
| /tailwind\.config/i, | ||
| /tokens?/i, | ||
| /theme/i, | ||
| /brand/i, | ||
| /palette/i, | ||
| /typography/i, | ||
| /global\.(css|scss|sass|less)$/i, | ||
| /variables?\.(css|scss|sass|less)$/i, | ||
| ]; | ||
|
|
||
| const MAX_FILES = 160; | ||
| const MAX_SELECTED_FILES = 12; | ||
| const MAX_FILE_CHARS = 32_000; | ||
|
|
||
| interface CandidateFile { | ||
| fullPath: string; | ||
| relativePath: string; | ||
| score: number; | ||
| } | ||
|
|
||
| function pushUnique(target: string[], value: string, max: number): void { | ||
| if (!value || target.includes(value) || target.length >= max) return; | ||
| target.push(value); | ||
| } | ||
|
|
||
| function cleanValue(value: string): string { | ||
| return value | ||
| .replace(/\s+/g, ' ') | ||
| .replace(/["'`,]/g, '') | ||
| .trim(); | ||
| } | ||
|
|
||
| function scoreCandidate(relativePath: string): number { | ||
| const fileName = basename(relativePath); | ||
| let score = 1; | ||
| for (const pattern of PRIORITY_PATTERNS) { | ||
| if (pattern.test(relativePath) || pattern.test(fileName)) score += 20; | ||
| } | ||
| if (/\.(css|scss|sass|less)$/i.test(fileName)) score += 8; | ||
| if (/tailwind/i.test(relativePath)) score += 8; | ||
| if (/src\//i.test(relativePath)) score += 4; | ||
| return score; | ||
| } | ||
|
|
||
| async function collectCandidateFiles( | ||
| rootPath: string, | ||
| dirPath: string, | ||
| files: CandidateFile[], | ||
| ): Promise<void> { | ||
| if (files.length >= MAX_FILES) return; | ||
|
|
||
| let entries: Dirent<string>[]; | ||
| try { | ||
| entries = await readdir(dirPath, { withFileTypes: true, encoding: 'utf8' }); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (files.length >= MAX_FILES) return; | ||
| const fullPath = join(dirPath, entry.name); | ||
| if (entry.isDirectory()) { | ||
| if (!IGNORED_DIRS.has(entry.name)) { | ||
| await collectCandidateFiles(rootPath, fullPath, files); | ||
| } | ||
| continue; | ||
| } | ||
| if (!entry.isFile()) continue; | ||
| const extension = extname(entry.name).toLowerCase(); | ||
| if (!CANDIDATE_EXTS.has(extension) && !/tailwind\.config/i.test(entry.name)) continue; | ||
| const relativePath = relative(rootPath, fullPath).replace(/\\/g, '/'); | ||
| files.push({ fullPath, relativePath, score: scoreCandidate(relativePath) }); | ||
| } | ||
| } | ||
|
|
||
| function collectCssVarValues( | ||
| raw: string, | ||
| colors: string[], | ||
| spacing: string[], | ||
| radius: string[], | ||
| shadows: string[], | ||
| ): void { | ||
| for (const match of raw.matchAll(/--([a-z0-9-]+)\s*:\s*([^;}{\n]+)/gi)) { | ||
| const name = match[1]?.toLowerCase() ?? ''; | ||
| const value = cleanValue(match[2] ?? ''); | ||
| if (!value) continue; | ||
| if (/color|accent|surface|text|brand|primary|secondary/.test(name)) | ||
| pushUnique(colors, value, 24); | ||
| if (/space|spacing|gap|padding|margin/.test(name)) pushUnique(spacing, value, 16); | ||
| if (/radius|rounded/.test(name)) pushUnique(radius, value, 16); | ||
| if (/shadow/.test(name)) pushUnique(shadows, value, 16); | ||
| } | ||
| } | ||
|
|
||
| function collectLooseValues( | ||
| raw: string, | ||
| colors: string[], | ||
| fonts: string[], | ||
| spacing: string[], | ||
| radius: string[], | ||
| shadows: string[], | ||
| ): void { | ||
| for (const match of raw.matchAll(/#[0-9a-f]{3,8}\b|rgba?\([^)]*\)|hsla?\([^)]*\)/gi)) { | ||
| pushUnique(colors, cleanValue(match[0] ?? ''), 24); | ||
| } | ||
|
|
||
| for (const match of raw.matchAll(/(?:font-family|fontFamily)[^:=]*[:=]\s*([^\n;]+)/gi)) { | ||
| const value = cleanValue(match[1] ?? ''); | ||
| for (const part of value.split(',')) pushUnique(fonts, cleanValue(part), 16); | ||
| } | ||
|
|
||
| for (const match of raw.matchAll( | ||
| /(?:spacing|space|gap|padding|margin)[^:=\n]*[:=]\s*([^\n,;]+)/gi, | ||
| )) { | ||
| pushUnique(spacing, cleanValue(match[1] ?? ''), 16); | ||
| } | ||
|
|
||
| for (const match of raw.matchAll(/(?:radius|rounded|borderRadius)[^:=\n]*[:=]\s*([^\n,;]+)/gi)) { | ||
| pushUnique(radius, cleanValue(match[1] ?? ''), 16); | ||
| } | ||
|
|
||
| for (const match of raw.matchAll(/(?:shadow|boxShadow)[^:=\n]*[:=]\s*([^\n,;]+)/gi)) { | ||
| pushUnique(shadows, cleanValue(match[1] ?? ''), 16); | ||
| } | ||
| } | ||
|
|
||
| function buildSummary( | ||
| snapshot: Omit<StoredDesignSystem, 'schemaVersion' | 'summary' | 'extractedAt'>, | ||
| ): string { | ||
| const repoLabel = basename(snapshot.rootPath); | ||
| const parts = [ | ||
| `Scanned ${snapshot.sourceFiles.length} likely design-system files under ${repoLabel}.`, | ||
| ]; | ||
| if (snapshot.colors.length > 0) | ||
| parts.push(`Color language: ${snapshot.colors.slice(0, 5).join(', ')}.`); | ||
| if (snapshot.fonts.length > 0) | ||
| parts.push(`Typography: ${snapshot.fonts.slice(0, 4).join(', ')}.`); | ||
| if (snapshot.spacing.length > 0) | ||
| parts.push(`Spacing cues: ${snapshot.spacing.slice(0, 4).join(', ')}.`); | ||
| if (snapshot.radius.length > 0) | ||
| parts.push(`Corner radius cues: ${snapshot.radius.slice(0, 4).join(', ')}.`); | ||
| if (snapshot.shadows.length > 0) | ||
| parts.push(`Shadow cues: ${snapshot.shadows.slice(0, 4).join(', ')}.`); | ||
| if (parts.length === 1) { | ||
| parts.push( | ||
| 'No strong structured tokens were extracted, so lean on the referenced styling files and keep the output conservative and cohesive.', | ||
| ); | ||
| } | ||
| return parts.join(' '); | ||
| } | ||
|
|
||
| export async function scanDesignSystem(rootPath: string): Promise<StoredDesignSystem> { | ||
| const candidates: CandidateFile[] = []; | ||
| await collectCandidateFiles(rootPath, rootPath, candidates); | ||
|
|
||
| const selected = candidates | ||
| .sort((a, b) => b.score - a.score || a.relativePath.localeCompare(b.relativePath)) | ||
| .slice(0, MAX_SELECTED_FILES); | ||
|
|
||
| const colors: string[] = []; | ||
| const fonts: string[] = []; | ||
| const spacing: string[] = []; | ||
| const radius: string[] = []; | ||
| const shadows: string[] = []; | ||
|
|
||
| for (const file of selected) { | ||
| let raw = ''; | ||
| try { | ||
| raw = await readFile(file.fullPath, 'utf8'); | ||
| } catch { | ||
| continue; | ||
| } | ||
| const snippet = raw.slice(0, MAX_FILE_CHARS); | ||
| collectCssVarValues(snippet, colors, spacing, radius, shadows); | ||
| collectLooseValues(snippet, colors, fonts, spacing, radius, shadows); | ||
| } | ||
|
|
||
| const baseSnapshot = { | ||
| rootPath, | ||
| sourceFiles: selected.map((file) => file.relativePath), | ||
| colors, | ||
| fonts, | ||
| spacing, | ||
| radius, | ||
| shadows, | ||
| }; | ||
|
|
||
| return { | ||
| schemaVersion: STORED_DESIGN_SYSTEM_SCHEMA_VERSION, | ||
| ...baseSnapshot, | ||
| summary: buildSummary(baseSnapshot), | ||
| extractedAt: new Date().toISOString(), | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { createRequire } from 'node:module'; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| export const electron = require('electron') as typeof import('electron'); | ||
|
|
||
| export const { app, BrowserWindow, dialog, ipcMain, safeStorage, shell } = electron; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scan still swallows unreadable directories/files and then returns success, and it still
readFile()s each selected candidate even though onlyMAX_FILE_CHARSare used. On a large repo that can both produce a misleading partial snapshot and stall the Electron main process.Suggested fix: