import { writeFile, mkdir } from 'node:fs/promises'; import { join } from 'node:path'; export async function writeProjectFiles(projectId, files, basePath) { const projectPath = join(basePath, projectId); await mkdir(projectPath, { recursive: true }); for (const [filepath, content] of Object.entries(files)) { const fullPath = join(projectPath, filepath); const dir = join(fullPath, '..'); await mkdir(dir, { recursive: true }); await writeFile(fullPath, content, 'utf-8'); } return projectPath; } export function detectLanguageFromFiles(files) { const filenames = Object.keys(files); if (filenames.some(f => f.endsWith('package.json'))) { const pkg = JSON.parse(files[filenames.find(f => f.endsWith('package.json'))]); if (pkg.dependencies?.streamlit || filenames.includes('streamlit_app.py')) { return { language: 'python', isStreamlit: true }; } return { language: 'nodejs', isStreamlit: false }; } if (filenames.some(f => f.endsWith('go.mod'))) return { language: 'go', isStreamlit: false }; if (filenames.some(f => f.endsWith('Cargo.toml'))) return { language: 'rust', isStreamlit: false }; if (filenames.some(f => f.endsWith('requirements.txt'))) return { language: 'python', isStreamlit: false }; if (filenames.some(f => f.endsWith('deno.json') || f.endsWith('deno.jsonc'))) return { language: 'deno', isStreamlit: false }; if (filenames.includes('index.html')) return { language: 'static', isStreamlit: false }; if (filenames.some(f => f.endsWith('.py'))) return { language: 'python', isStreamlit: false }; return { language: 'unknown', isStreamlit: false }; } export function scaffoldNodeJS(files) { const pkgPath = Object.keys(files).find(f => f.endsWith('package.json')); if (!pkgPath) { files['package.json'] = JSON.stringify({ name: 'deployed-project', version: '1.0.0', type: 'module', scripts: { dev: 'vite', build: 'vite build', preview: 'vite preview' }, dependencies: { react: '^18.2.0', 'react-dom': '^18.2.0' }, devDependencies: { vite: '^5.0.0', '@vitejs/plugin-react': '^4.2.0' } }, null, 2); if (!files['vite.config.js']) { files['vite.config.js'] = `import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], server: { host: '0.0.0.0' } })`; } if (!files['index.html']) { files['index.html'] = `