legends810/testingnew
0
1import ignore from 'ignore';2 3// Common patterns to ignore, similar to .gitignore4export const IGNORE_PATTERNS = [5 'node_modules/**',6 '.git/**',7 'dist/**',8 'build/**',9 '.next/**',10 'coverage/**',11 '.cache/**',12 '.vscode/**',13 '.idea/**',14 '**/*.log',15 '**/.DS_Store',16 '**/npm-debug.log*',17 '**/yarn-debug.log*',18 '**/yarn-error.log*',19];20 21export const MAX_FILES = 1000;22export const ig = ignore().add(IGNORE_PATTERNS);23 24export const generateId = () => Math.random().toString(36).substring(2, 15);25 26export const isBinaryFile = async (file: File): Promise<boolean> => {27 const chunkSize = 1024;28 const buffer = new Uint8Array(await file.slice(0, chunkSize).arrayBuffer());29 30 for (let i = 0; i < buffer.length; i++) {31 const byte = buffer[i];32 33 if (byte === 0 || (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13)) {34 return true;35 }36 }37 38 return false;39};40 41export const shouldIncludeFile = (path: string): boolean => {42 return !ig.ignores(path);43};44 45const readPackageJson = async (files: File[]): Promise<{ scripts?: Record<string, string> } | null> => {46 const packageJsonFile = files.find((f) => f.webkitRelativePath.endsWith('package.json'));47 48 if (!packageJsonFile) {49 return null;50 }51 52 try {53 const content = await new Promise<string>((resolve, reject) => {54 const reader = new FileReader();55 reader.onload = () => resolve(reader.result as string);56 reader.onerror = reject;57 reader.readAsText(packageJsonFile);58 });59 60 return JSON.parse(content);61 } catch (error) {62 console.error('Error reading package.json:', error);63 return null;64 }65};66 67export const detectProjectType = async (68 files: File[],69): Promise<{ type: string; setupCommand: string; followupMessage: string }> => {70 const hasFile = (name: string) => files.some((f) => f.webkitRelativePath.endsWith(name));71 72 if (hasFile('package.json')) {73 const packageJson = await readPackageJson(files);74 const scripts = packageJson?.scripts || {};75 76 // Check for preferred commands in priority order77 const preferredCommands = ['dev', 'start', 'preview'];78 const availableCommand = preferredCommands.find((cmd) => scripts[cmd]);79 80 if (availableCommand) {81 return {82 type: 'Node.js',83 setupCommand: `npm install && npm run ${availableCommand}`,84 followupMessage: `Found "${availableCommand}" script in package.json. Running "npm run ${availableCommand}" after installation.`,85 };86 }87 88 return {89 type: 'Node.js',90 setupCommand: 'npm install',91 followupMessage:92 'Would you like me to inspect package.json to determine the available scripts for running this project?',93 };94 }95 96 if (hasFile('index.html')) {97 return {98 type: 'Static',99 setupCommand: 'npx --yes serve',100 followupMessage: '',101 };102 }103 104 return { type: '', setupCommand: '', followupMessage: '' };105};106 107export const filesToArtifacts = (files: { [path: string]: { content: string } }, id: string): string => {108 return `109<boltArtifact id="${id}" title="User Updated Files">110${Object.keys(files)111 .map(112 (filePath) => `113<boltAction type="file" filePath="${filePath}">114${files[filePath].content}115</boltAction>116`,117 )118 .join('\n')}119</boltArtifact>120 `;121};122 