feat(fake-llm-server): add initial setup for fake LLM server with TypeScript and Express

- Created package.json for dependencies and scripts
- Added tsconfig.json for TypeScript configuration
- Implemented fake stdio MCP server with basic calculator and environment variable printing tools
- Added shell script to run the fake stdio MCP server
- Updated root tsconfig.json for project references and path mapping
This commit is contained in:
Kunthawat Greethong
2025-12-19 09:36:31 +07:00
parent 07bf4414cc
commit 756b405423
412 changed files with 69158 additions and 8 deletions

View File

@@ -0,0 +1,666 @@
import fsAsync from "node:fs/promises";
import path from "node:path";
import { gitIsIgnored } from "../ipc/utils/git_utils";
import log from "electron-log";
import { IS_TEST_BUILD } from "../ipc/utils/test_utils";
import { glob } from "glob";
import { AppChatContext } from "../lib/schemas";
import { readSettings } from "@/main/settings";
import { AsyncVirtualFileSystem } from "../../shared/VirtualFilesystem";
const logger = log.scope("utils/codebase");
// File extensions to include in the extraction
const ALLOWED_EXTENSIONS = [
".ts",
".tsx",
".js",
".jsx",
".mjs",
".cjs",
".mts",
".cts",
".css",
".html",
".md",
".astro",
".vue",
".svelte",
".scss",
".sass",
".less",
// Oftentimes used as config (e.g. package.json, vercel.json) or data files (e.g. translations)
".json",
// GitHub Actions
".yml",
".yaml",
// Needed for Capacitor projects
".xml",
".plist",
".entitlements",
".kt",
".java",
".gradle",
".swift",
// Edge cases
// https://github.com/dyad-sh/dyad/issues/880
".py",
// https://github.com/dyad-sh/dyad/issues/1221
".php",
];
// Directories to always exclude
// Normally these files are excluded by the gitignore, but sometimes
// people don't have their gitignore setup correctly so we want to
// be conservative and never include these directories.
//
// ex: https://github.com/dyad-sh/dyad/issues/727
const EXCLUDED_DIRS = [
"node_modules",
".git",
"dist",
"build",
".next",
".venv",
"venv",
];
// Files to always exclude
const EXCLUDED_FILES = ["pnpm-lock.yaml", "package-lock.json"];
// Files to always include, regardless of extension
const ALWAYS_INCLUDE_FILES = [".gitignore"];
// File patterns to always omit (contents will be replaced with a placeholder)
// We don't want to send environment variables to the LLM because they
// are sensitive and users should be configuring them via the UI.
const ALWAYS_OMITTED_FILES = [".env", ".env.local"];
// File patterns to omit (contents will be replaced with a placeholder)
//
// Why are we not using path.join here?
// Because we have already normalized the path to use /.
//
// Note: these files are only omitted when NOT using smart context.
//
// Why do we omit these files when not using smart context?
//
// Because these files are typically low-signal and adding them
// to the context can cause users to much more quickly hit their
// free rate limits.
const OMITTED_FILES = [
...ALWAYS_OMITTED_FILES,
"src/components/ui",
"eslint.config",
"tsconfig.json",
"tsconfig.app.json",
"tsconfig.node.json",
"tsconfig.base.json",
"components.json",
];
// Maximum file size to include (in bytes) - 1MB
const MAX_FILE_SIZE = 1000 * 1024;
// Maximum size for fileContentCache
const MAX_FILE_CACHE_SIZE = 500;
// File content cache with timestamps
type FileCache = {
content: string;
mtime: number;
};
// Cache for file contents
const fileContentCache = new Map<string, FileCache>();
// Cache for git ignored paths
const gitIgnoreCache = new Map<string, boolean>();
// Map to store .gitignore file paths and their modification times
const gitIgnoreMtimes = new Map<string, number>();
/**
* Check if a path should be ignored based on git ignore rules
*/
async function isGitIgnored(
filePath: string,
baseDir: string,
): Promise<boolean> {
try {
// Check if any relevant .gitignore has been modified
// Git checks .gitignore files in the path from the repo root to the file
let currentDir = baseDir;
const pathParts = path.relative(baseDir, filePath).split(path.sep);
let shouldClearCache = false;
// Check root .gitignore
const rootGitIgnorePath = path.join(baseDir, ".gitignore");
try {
const stats = await fsAsync.stat(rootGitIgnorePath);
const lastMtime = gitIgnoreMtimes.get(rootGitIgnorePath) || 0;
if (stats.mtimeMs > lastMtime) {
gitIgnoreMtimes.set(rootGitIgnorePath, stats.mtimeMs);
shouldClearCache = true;
}
} catch {
// Root .gitignore might not exist, which is fine
}
// Check .gitignore files in parent directories
for (let i = 0; i < pathParts.length - 1; i++) {
currentDir = path.join(currentDir, pathParts[i]);
const gitIgnorePath = path.join(currentDir, ".gitignore");
try {
const stats = await fsAsync.stat(gitIgnorePath);
const lastMtime = gitIgnoreMtimes.get(gitIgnorePath) || 0;
if (stats.mtimeMs > lastMtime) {
gitIgnoreMtimes.set(gitIgnorePath, stats.mtimeMs);
shouldClearCache = true;
}
} catch {
// This directory might not have a .gitignore, which is fine
}
}
// Clear cache if any .gitignore was modified
if (shouldClearCache) {
gitIgnoreCache.clear();
}
const cacheKey = `${baseDir}:${filePath}`;
if (gitIgnoreCache.has(cacheKey)) {
return gitIgnoreCache.get(cacheKey)!;
}
const relativePath = path.relative(baseDir, filePath);
const result = await gitIsIgnored({
path: baseDir,
filepath: relativePath,
});
gitIgnoreCache.set(cacheKey, result);
return result;
} catch (error) {
logger.error(`Error checking if path is git ignored: ${filePath}`, error);
return false;
}
}
/**
* Read file contents with caching based on last modified time
*/
export async function readFileWithCache(
filePath: string,
virtualFileSystem?: AsyncVirtualFileSystem,
): Promise<string | undefined> {
try {
// Check virtual filesystem first if provided
if (virtualFileSystem) {
const virtualContent = await virtualFileSystem.readFile(filePath);
if (virtualContent != null) {
return virtualContent;
}
}
// Get file stats to check the modification time
const stats = await fsAsync.stat(filePath);
const currentMtime = stats.mtimeMs;
// If file is in cache and hasn't been modified, use cached content
if (fileContentCache.has(filePath)) {
const cache = fileContentCache.get(filePath)!;
if (cache.mtime === currentMtime) {
return cache.content;
}
}
// Read file and update cache
const rawContent = await fsAsync.readFile(filePath, "utf-8");
const content = rawContent;
fileContentCache.set(filePath, {
content,
mtime: currentMtime,
});
// Manage cache size by clearing oldest entries when it gets too large
if (fileContentCache.size > MAX_FILE_CACHE_SIZE) {
// Get the oldest 25% of entries to remove
const entriesToDelete = Math.ceil(MAX_FILE_CACHE_SIZE * 0.25);
const keys = Array.from(fileContentCache.keys());
// Remove oldest entries (first in, first out)
for (let i = 0; i < entriesToDelete; i++) {
fileContentCache.delete(keys[i]);
}
}
return content;
} catch (error) {
logger.error(`Error reading file: ${filePath}`, error);
return undefined;
}
}
/**
* Recursively walk a directory and collect all relevant files
*/
async function collectFiles(dir: string, baseDir: string): Promise<string[]> {
const files: string[] = [];
// Check if directory exists
try {
await fsAsync.access(dir);
} catch {
// Directory doesn't exist or is not accessible
return files;
}
try {
// Read directory contents
const entries = await fsAsync.readdir(dir, { withFileTypes: true });
// Process entries concurrently
const promises = entries.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
// Skip excluded directories
if (entry.isDirectory() && EXCLUDED_DIRS.includes(entry.name)) {
return;
}
// Skip if the entry is git ignored
if (await isGitIgnored(fullPath, baseDir)) {
return;
}
if (entry.isDirectory()) {
// Recursively process subdirectories
const subDirFiles = await collectFiles(fullPath, baseDir);
files.push(...subDirFiles);
} else if (entry.isFile()) {
// Skip excluded files
if (EXCLUDED_FILES.includes(entry.name)) {
return;
}
// Skip files that are too large
try {
const stats = await fsAsync.stat(fullPath);
if (stats.size > MAX_FILE_SIZE) {
return;
}
} catch (error) {
logger.error(`Error checking file size: ${fullPath}`, error);
return;
}
// Include all files in the list
files.push(fullPath);
}
});
await Promise.all(promises);
} catch (error) {
logger.error(`Error reading directory ${dir}:`, error);
}
return files;
}
const OMITTED_FILE_CONTENT = "// File contents excluded from context";
/**
* Check if file contents should be read based on extension and inclusion rules
*/
function shouldReadFileContents({
filePath,
normalizedRelativePath,
}: {
filePath: string;
normalizedRelativePath: string;
}): boolean {
const ext = path.extname(filePath).toLowerCase();
const fileName = path.basename(filePath);
// OMITTED_FILES takes precedence - never read if omitted
if (
OMITTED_FILES.some((pattern) => normalizedRelativePath.includes(pattern))
) {
return false;
}
// Check if file should be included based on extension or filename
return (
ALLOWED_EXTENSIONS.includes(ext) || ALWAYS_INCLUDE_FILES.includes(fileName)
);
}
function shouldReadFileContentsForSmartContext({
filePath,
normalizedRelativePath,
}: {
filePath: string;
normalizedRelativePath: string;
}): boolean {
const ext = path.extname(filePath).toLowerCase();
const fileName = path.basename(filePath);
// ALWAYS__OMITTED_FILES takes precedence - never read if omitted
if (
ALWAYS_OMITTED_FILES.some((pattern) =>
normalizedRelativePath.includes(pattern),
)
) {
return false;
}
// Check if file should be included based on extension or filename
return (
ALLOWED_EXTENSIONS.includes(ext) || ALWAYS_INCLUDE_FILES.includes(fileName)
);
}
/**
* Format a file for inclusion in the codebase extract
*/
async function formatFile({
filePath,
normalizedRelativePath,
virtualFileSystem,
}: {
filePath: string;
normalizedRelativePath: string;
virtualFileSystem?: AsyncVirtualFileSystem;
}): Promise<string> {
try {
// Check if we should read file contents
if (!shouldReadFileContents({ filePath, normalizedRelativePath })) {
return `<dyad-file path="${normalizedRelativePath}">
${OMITTED_FILE_CONTENT}
</dyad-file>
`;
}
const content = await readFileWithCache(filePath, virtualFileSystem);
if (content == null) {
return `<dyad-file path="${normalizedRelativePath}">
// Error reading file
</dyad-file>
`;
}
return `<dyad-file path="${normalizedRelativePath}">
${content}
</dyad-file>
`;
} catch (error) {
logger.error(`Error reading file: ${filePath}`, error);
return `<dyad-file path="${normalizedRelativePath}">
// Error reading file: ${error}
</dyad-file>
`;
}
}
export interface BaseFile {
path: string;
focused?: boolean;
force?: boolean;
}
export interface CodebaseFile extends BaseFile {
content: string;
}
export interface CodebaseFileReference extends BaseFile {
fileId: string;
}
/**
* Extract and format codebase files as a string to be included in prompts
* @param appPath - Path to the codebase to extract
* @param virtualFileSystem - Optional virtual filesystem to apply modifications
* @returns Object containing formatted output and individual files
*/
export async function extractCodebase({
appPath,
chatContext,
virtualFileSystem,
}: {
appPath: string;
chatContext: AppChatContext;
virtualFileSystem?: AsyncVirtualFileSystem;
}): Promise<{
formattedOutput: string;
files: CodebaseFile[];
}> {
const settings = readSettings();
const isSmartContextEnabled =
settings?.enableDyadPro && settings?.enableProSmartFilesContextMode;
try {
await fsAsync.access(appPath);
} catch {
return {
formattedOutput: `# Error: Directory ${appPath} does not exist or is not accessible`,
files: [],
};
}
const startTime = Date.now();
// Collect all relevant files
let files = await collectFiles(appPath, appPath);
// Apply virtual filesystem modifications if provided
if (virtualFileSystem) {
// Filter out deleted files
const deletedFiles = new Set(
virtualFileSystem
.getDeletedFiles()
.map((relativePath) => path.resolve(appPath, relativePath)),
);
files = files.filter((file) => !deletedFiles.has(file));
// Add virtual files
const virtualFiles = virtualFileSystem.getVirtualFiles();
for (const virtualFile of virtualFiles) {
const absolutePath = path.resolve(appPath, virtualFile.path);
if (!files.includes(absolutePath)) {
files.push(absolutePath);
}
}
}
// Collect files from contextPaths and smartContextAutoIncludes
const { contextPaths, smartContextAutoIncludes, excludePaths } = chatContext;
const includedFiles = new Set<string>();
const autoIncludedFiles = new Set<string>();
const excludedFiles = new Set<string>();
// Add files from contextPaths
if (contextPaths && contextPaths.length > 0) {
for (const p of contextPaths) {
const pattern = createFullGlobPath({
appPath,
globPath: p.globPath,
});
const matches = await glob(pattern, {
nodir: true,
absolute: true,
ignore: "**/node_modules/**",
});
matches.forEach((file) => {
const normalizedFile = path.normalize(file);
includedFiles.add(normalizedFile);
});
}
}
// Add files from smartContextAutoIncludes
if (
isSmartContextEnabled &&
smartContextAutoIncludes &&
smartContextAutoIncludes.length > 0
) {
for (const p of smartContextAutoIncludes) {
const pattern = createFullGlobPath({
appPath,
globPath: p.globPath,
});
const matches = await glob(pattern, {
nodir: true,
absolute: true,
ignore: "**/node_modules/**",
});
matches.forEach((file) => {
const normalizedFile = path.normalize(file);
autoIncludedFiles.add(normalizedFile);
includedFiles.add(normalizedFile); // Also add to included files
});
}
}
// Add files from excludePaths
if (excludePaths && excludePaths.length > 0) {
for (const p of excludePaths) {
const pattern = createFullGlobPath({
appPath,
globPath: p.globPath,
});
const matches = await glob(pattern, {
nodir: true,
absolute: true,
ignore: "**/node_modules/**",
});
matches.forEach((file) => {
const normalizedFile = path.normalize(file);
excludedFiles.add(normalizedFile);
});
}
}
// Only filter files if contextPaths are provided
// If only smartContextAutoIncludes are provided, keep all files and just mark auto-includes as forced
if (contextPaths && contextPaths.length > 0) {
files = files.filter((file) => includedFiles.has(path.normalize(file)));
}
// Filter out excluded files (this takes precedence over include paths)
if (excludedFiles.size > 0) {
files = files.filter((file) => !excludedFiles.has(path.normalize(file)));
}
// Sort files by modification time (oldest first)
// This is important for cache-ability.
const sortedFiles = await sortFilesByModificationTime([...new Set(files)]);
// Format files and collect individual file contents
const filesArray: CodebaseFile[] = [];
const formatPromises = sortedFiles.map(async (file) => {
// Get raw content for the files array
const normalizedRelativePath = path
.relative(appPath, file)
// Why? Normalize Windows-style paths which causes lots of weird issues (e.g. Git commit)
.split(path.sep)
.join("/");
const formattedContent = await formatFile({
filePath: file,
normalizedRelativePath,
virtualFileSystem,
});
const isForced =
autoIncludedFiles.has(path.normalize(file)) &&
!excludedFiles.has(path.normalize(file));
// Determine file content based on whether we should read it
let fileContent: string;
if (
!shouldReadFileContentsForSmartContext({
filePath: file,
normalizedRelativePath,
})
) {
fileContent = OMITTED_FILE_CONTENT;
} else {
const readContent = await readFileWithCache(file, virtualFileSystem);
fileContent = readContent ?? "// Error reading file";
}
filesArray.push({
path: normalizedRelativePath,
content: fileContent,
force: isForced,
});
return formattedContent;
});
const formattedFiles = await Promise.all(formatPromises);
const formattedOutput = formattedFiles.join("");
const endTime = Date.now();
logger.log("extractCodebase: time taken", endTime - startTime);
if (IS_TEST_BUILD) {
// Why? For some reason, file ordering is not stable on Windows.
// This is a workaround to ensure stable ordering, although
// ideally we'd like to sort it by modification time which is
// important for cache-ability.
filesArray.sort((a, b) => a.path.localeCompare(b.path));
}
return {
formattedOutput,
files: filesArray,
};
}
/**
* Sort files by their modification timestamp (oldest first)
*/
async function sortFilesByModificationTime(files: string[]): Promise<string[]> {
// Get stats for all files
const fileStats = await Promise.all(
files.map(async (file) => {
try {
const stats = await fsAsync.stat(file);
return { file, mtime: stats.mtimeMs };
} catch (error) {
// If there's an error getting stats, use current time as fallback
// This can happen with virtual files, so it's not a big deal.
logger.warn(`Error getting file stats for ${file}:`, error);
return { file, mtime: Date.now() };
}
}),
);
if (IS_TEST_BUILD) {
// Why? For some reason, file ordering is not stable on Windows.
// This is a workaround to ensure stable ordering, although
// ideally we'd like to sort it by modification time which is
// important for cache-ability.
return fileStats
.sort((a, b) => a.file.localeCompare(b.file))
.map((item) => item.file);
}
// Sort by modification time (oldest first)
return fileStats.sort((a, b) => a.mtime - b.mtime).map((item) => item.file);
}
function createFullGlobPath({
appPath,
globPath,
}: {
appPath: string;
globPath: string;
}): string {
// By default the glob package treats "\" as an escape character.
// We want the path to use forward slash for all platforms.
return `${appPath.replace(/\\/g, "/")}/${globPath}`;
}

View File

@@ -0,0 +1,29 @@
// Determine language based on file extension
const getLanguage = (filePath: string) => {
const extension = filePath.split(".").pop()?.toLowerCase() || "";
const languageMap: Record<string, string> = {
js: "javascript",
jsx: "javascript",
ts: "typescript",
tsx: "typescript",
html: "html",
css: "css",
json: "json",
md: "markdown",
py: "python",
java: "java",
c: "c",
cpp: "cpp",
cs: "csharp",
go: "go",
rs: "rust",
rb: "ruby",
php: "php",
swift: "swift",
kt: "kotlin",
// Add more as needed
};
return languageMap[extension] || "plaintext";
};
export { getLanguage };

View File

@@ -0,0 +1,201 @@
import log from "electron-log";
import { writeSettings } from "../main/settings";
import os from "node:os";
const logger = log.scope("performance-monitor");
// Constants
const MONITOR_INTERVAL_MS = 30000; // 30 seconds
const BYTES_PER_MB = 1024 * 1024;
let monitorInterval: NodeJS.Timeout | null = null;
let lastCpuUsage: NodeJS.CpuUsage | null = null;
let lastTimestamp: number | null = null;
let lastSystemCpuInfo: os.CpuInfo[] | null = null;
let lastSystemTimestamp: number | null = null;
/**
* Get current memory usage in MB
*/
function getMemoryUsageMB(): number {
const memoryUsage = process.memoryUsage();
// Use RSS (Resident Set Size) for total memory used by the process
return Math.round(memoryUsage.rss / BYTES_PER_MB);
}
/**
* Get CPU usage percentage
* This measures CPU time used by this process relative to wall clock time
*/
function getCpuUsagePercent(): number | null {
const currentCpuUsage = process.cpuUsage();
const currentTimestamp = Date.now();
// On first call, just initialize and return null
if (lastCpuUsage === null || lastTimestamp === null) {
lastCpuUsage = currentCpuUsage;
lastTimestamp = currentTimestamp;
return null;
}
// Calculate elapsed wall clock time in microseconds
const elapsedTimeMs = currentTimestamp - lastTimestamp;
const elapsedTimeMicros = elapsedTimeMs * 1000;
// Calculate CPU time used (user + system) in microseconds
const cpuTimeMicros =
currentCpuUsage.user -
lastCpuUsage.user +
(currentCpuUsage.system - lastCpuUsage.system);
// CPU percentage = (CPU time / wall clock time) * 100
// This gives percentage across all cores (can exceed 100% on multi-core systems)
const cpuPercent = (cpuTimeMicros / elapsedTimeMicros) * 100;
// Update for next calculation
lastCpuUsage = currentCpuUsage;
lastTimestamp = currentTimestamp;
return Math.round(cpuPercent * 100) / 100; // Round to 2 decimal places
}
/**
* Get system memory usage
*/
function getSystemMemoryUsage(): {
totalMemoryMB: number;
usedMemoryMB: number;
freeMemoryMB: number;
usagePercent: number;
} {
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
const usedMemory = totalMemory - freeMemory;
return {
totalMemoryMB: Math.round(totalMemory / BYTES_PER_MB),
usedMemoryMB: Math.round(usedMemory / BYTES_PER_MB),
freeMemoryMB: Math.round(freeMemory / BYTES_PER_MB),
usagePercent: Math.round((usedMemory / totalMemory) * 100 * 100) / 100,
};
}
/**
* Get system CPU usage percentage
*/
function getSystemCpuUsagePercent(): number | null {
const cpus = os.cpus();
const currentTimestamp = Date.now();
// On first call, just initialize and return null
if (lastSystemCpuInfo === null || lastSystemTimestamp === null) {
lastSystemCpuInfo = cpus;
lastSystemTimestamp = currentTimestamp;
return null;
}
// Calculate total CPU time for all cores
let totalIdle = 0;
let totalTick = 0;
let lastTotalIdle = 0;
let lastTotalTick = 0;
// Current CPU times
for (const cpu of cpus) {
for (const type in cpu.times) {
totalTick += cpu.times[type as keyof typeof cpu.times];
}
totalIdle += cpu.times.idle;
}
// Last CPU times
for (const cpu of lastSystemCpuInfo) {
for (const type in cpu.times) {
lastTotalTick += cpu.times[type as keyof typeof cpu.times];
}
lastTotalIdle += cpu.times.idle;
}
// Calculate differences
const totalTickDiff = totalTick - lastTotalTick;
const idleDiff = totalIdle - lastTotalIdle;
// Calculate usage percentage
const usage = 100 - (100 * idleDiff) / totalTickDiff;
// Update for next calculation
lastSystemCpuInfo = cpus;
lastSystemTimestamp = currentTimestamp;
return Math.round(usage * 100) / 100;
}
/**
* Capture and save current performance metrics
*/
function capturePerformanceMetrics() {
try {
const memoryUsageMB = getMemoryUsageMB();
const cpuUsagePercent = getCpuUsagePercent();
const systemMemory = getSystemMemoryUsage();
const systemCpuPercent = getSystemCpuUsagePercent();
// Skip saving if CPU is null (first call for either metric)
if (cpuUsagePercent === null || systemCpuPercent === null) {
logger.debug(
`Performance: Memory=${memoryUsageMB}MB, CPU=initializing, System Memory=${systemMemory.usagePercent}%, System CPU=initializing`,
);
return;
}
logger.debug(
`Performance: Memory=${memoryUsageMB}MB, CPU=${cpuUsagePercent}%, System Memory=${systemMemory.usedMemoryMB}/${systemMemory.totalMemoryMB}MB (${systemMemory.usagePercent}%), System CPU=${systemCpuPercent}%`,
);
writeSettings({
lastKnownPerformance: {
timestamp: Date.now(),
memoryUsageMB,
cpuUsagePercent,
systemMemoryUsageMB: systemMemory.usedMemoryMB,
systemMemoryTotalMB: systemMemory.totalMemoryMB,
systemCpuPercent,
},
});
} catch (error) {
logger.error("Error capturing performance metrics:", error);
}
}
/**
* Start monitoring performance metrics
* Captures metrics every 30 seconds
*/
export function startPerformanceMonitoring() {
if (monitorInterval) {
logger.warn("Performance monitoring already started");
return;
}
logger.info("Starting performance monitoring");
// Capture initial metrics
capturePerformanceMetrics();
// Capture every 30 seconds
monitorInterval = setInterval(capturePerformanceMetrics, MONITOR_INTERVAL_MS);
}
/**
* Stop monitoring performance metrics
*/
export function stopPerformanceMonitoring() {
if (monitorInterval) {
logger.info("Stopping performance monitoring");
clearInterval(monitorInterval);
monitorInterval = null;
// Capture final metrics before stopping
capturePerformanceMetrics();
}
}

View File

@@ -0,0 +1,199 @@
// Style conversion and manipulation utilities
interface SpacingValues {
left?: string;
right?: string;
top?: string;
bottom?: string;
}
interface StyleObject {
margin?: { left?: string; right?: string; top?: string; bottom?: string };
padding?: { left?: string; right?: string; top?: string; bottom?: string };
dimensions?: { width?: string; height?: string };
border?: { width?: string; radius?: string; color?: string };
backgroundColor?: string;
text?: {
fontSize?: string;
fontWeight?: string;
color?: string;
fontFamily?: string;
};
}
/**
* Convert spacing values (margin/padding) to Tailwind classes
*/
function convertSpacingToTailwind(
values: SpacingValues,
prefix: "m" | "p",
): string[] {
const classes: string[] = [];
const { left, right, top, bottom } = values;
const hasHorizontal = left !== undefined && right !== undefined;
const hasVertical = top !== undefined && bottom !== undefined;
// All sides equal
if (
hasHorizontal &&
hasVertical &&
left === right &&
top === bottom &&
left === top
) {
classes.push(`${prefix}-[${left}]`);
} else {
const horizontalValue = hasHorizontal && left === right ? left : null;
const verticalValue = hasVertical && top === bottom ? top : null;
if (
horizontalValue !== null &&
verticalValue !== null &&
horizontalValue === verticalValue
) {
// px = py or mx = my, so use the shorthand for all sides
classes.push(`${prefix}-[${horizontalValue}]`);
} else {
// Horizontal
if (hasHorizontal && left === right) {
classes.push(`${prefix}x-[${left}]`);
} else {
if (left !== undefined) classes.push(`${prefix}l-[${left}]`);
if (right !== undefined) classes.push(`${prefix}r-[${right}]`);
}
// Vertical
if (hasVertical && top === bottom) {
classes.push(`${prefix}y-[${top}]`);
} else {
if (top !== undefined) classes.push(`${prefix}t-[${top}]`);
if (bottom !== undefined) classes.push(`${prefix}b-[${bottom}]`);
}
}
}
return classes;
}
/**
* Convert style object to Tailwind classes
*/
export function stylesToTailwind(styles: StyleObject): string[] {
const classes: string[] = [];
if (styles.margin) {
classes.push(...convertSpacingToTailwind(styles.margin, "m"));
}
if (styles.padding) {
classes.push(...convertSpacingToTailwind(styles.padding, "p"));
}
if (styles.border) {
if (styles.border.width !== undefined)
classes.push(`border-[${styles.border.width}]`);
if (styles.border.radius !== undefined)
classes.push(`rounded-[${styles.border.radius}]`);
if (styles.border.color !== undefined)
classes.push(`border-[${styles.border.color}]`);
}
if (styles.backgroundColor !== undefined) {
classes.push(`bg-[${styles.backgroundColor}]`);
}
if (styles.dimensions) {
if (styles.dimensions.width !== undefined)
classes.push(`w-[${styles.dimensions.width}]`);
if (styles.dimensions.height !== undefined)
classes.push(`h-[${styles.dimensions.height}]`);
}
if (styles.text) {
if (styles.text.fontSize !== undefined)
classes.push(`text-[${styles.text.fontSize}]`);
if (styles.text.fontWeight !== undefined)
classes.push(`font-[${styles.text.fontWeight}]`);
if (styles.text.color !== undefined)
classes.push(`[color:${styles.text.color}]`);
if (styles.text.fontFamily !== undefined) {
// Replace spaces with underscores for Tailwind arbitrary values
const fontFamilyValue = styles.text.fontFamily.replace(/\s/g, "_");
classes.push(`font-[${fontFamilyValue}]`);
}
}
return classes;
}
/**
* Convert RGB color to hex format
*/
export function rgbToHex(rgb: string): string {
if (!rgb || rgb.startsWith("#")) return rgb || "#000000";
const rgbMatch = rgb.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
if (rgbMatch) {
const r = parseInt(rgbMatch[1]).toString(16).padStart(2, "0");
const g = parseInt(rgbMatch[2]).toString(16).padStart(2, "0");
const b = parseInt(rgbMatch[3]).toString(16).padStart(2, "0");
return `#${r}${g}${b}`;
}
return rgb || "#000000";
}
/**
* Process value by adding px suffix if it's a plain number
*/
export function processNumericValue(value: string): string {
return /^\d+$/.test(value) ? `${value}px` : value;
}
/**
* Extract prefixes from Tailwind classes
*/
export function extractClassPrefixes(classes: string[]): string[] {
return Array.from(
new Set(
classes.map((cls) => {
// Handle arbitrary properties like [color:...]
const arbitraryMatch = cls.match(/^\[([a-z-]+):/);
if (arbitraryMatch) {
return `[${arbitraryMatch[1]}:`;
}
// Special handling for font-[...] classes
// We need to distinguish between font-weight and font-family
if (cls.startsWith("font-[")) {
const value = cls.match(/^font-\[([^\]]+)\]/);
if (value) {
// If it's numeric (like 400, 700), it's font-weight
// If it contains letters/underscores, it's font-family
const isNumeric = /^\d+$/.test(value[1]);
return isNumeric ? "font-weight-" : "font-family-";
}
}
// Special handling for text-size classes (text-xs, text-sm, text-3xl, etc.)
// to avoid removing text-center, text-left, text-color classes
if (cls.startsWith("text-")) {
// Check if it's a font-size class (ends with size suffix like xs, sm, lg, xl, 2xl, etc.)
const sizeMatch = cls.match(
/^text-(xs|sm|base|lg|xl|2xl|3xl|4xl|5xl|6xl|7xl|8xl|9xl)$/,
);
if (sizeMatch) {
return "text-size-"; // Use a specific prefix for font-size
}
// For arbitrary text sizes like text-[44px]
if (cls.match(/^text-\[[\d.]+[a-z]+\]$/)) {
return "text-size-";
}
}
// Handle regular Tailwind classes
const match = cls.match(/^([a-z]+[-])/);
return match ? match[1] : cls.split("-")[0] + "-";
}),
),
);
}

View File

@@ -0,0 +1,20 @@
/**
* Normalizes text for comparison by handling smart quotes and other special characters
*/
export function normalizeString(text: string): string {
return (
text
// Normalize smart quotes to regular quotes
.replace(/[\u2018\u2019]/g, "'") // Single quotes
.replace(/[\u201C\u201D]/g, '"') // Double quotes
// Normalize different types of dashes
.replace(/[\u2013\u2014]/g, "-") // En dash and em dash to hyphen
// Normalize ellipsis
.replace(/\u2026/g, "...") // Ellipsis to three dots
// Normalize non-breaking spaces
.replace(/\u00A0/g, " ") // Non-breaking space to regular space
// Normalize other common Unicode variants
.replace(/\u00AD/g, "") // Soft hyphen (remove)
.replace(/[\uFEFF]/g, "")
); // Zero-width no-break space (remove)
}