|
| 1 | +import { build as viteBuild } from 'vite'; |
| 2 | +import react from '@vitejs/plugin-react'; |
| 3 | +import { existsSync, mkdirSync, cpSync, rmSync } from 'fs'; |
| 4 | +import { join, resolve } from 'path'; |
| 5 | +import chalk from 'chalk'; |
| 6 | +import { execSync } from 'child_process'; |
| 7 | +import { scanPagesDirectory, createTempAppWithRouting, createTempApp, parseSchemaFile, type RouteInfo } from '../utils/app-generator.js'; |
| 8 | + |
| 9 | +interface BuildOptions { |
| 10 | + outDir?: string; |
| 11 | + clean?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export async function buildApp(schemaPath: string, options: BuildOptions) { |
| 15 | + const cwd = process.cwd(); |
| 16 | + const outDir = options.outDir || 'dist'; |
| 17 | + const outputPath = resolve(cwd, outDir); |
| 18 | + |
| 19 | + console.log(chalk.blue('🔨 Building application for production...')); |
| 20 | + console.log(); |
| 21 | + |
| 22 | + // Check if pages directory exists for file-system routing |
| 23 | + const pagesDir = join(cwd, 'pages'); |
| 24 | + const hasPagesDir = existsSync(pagesDir); |
| 25 | + |
| 26 | + let routes: RouteInfo[] = []; |
| 27 | + let schema: unknown = null; |
| 28 | + let useFileSystemRouting = false; |
| 29 | + |
| 30 | + if (hasPagesDir) { |
| 31 | + // File-system based routing |
| 32 | + console.log(chalk.blue('📁 Using file-system routing')); |
| 33 | + routes = scanPagesDirectory(pagesDir); |
| 34 | + useFileSystemRouting = true; |
| 35 | + |
| 36 | + if (routes.length === 0) { |
| 37 | + throw new Error('No schema files found in pages/ directory'); |
| 38 | + } |
| 39 | + |
| 40 | + console.log(chalk.green(`✓ Found ${routes.length} route(s)`)); |
| 41 | + } else { |
| 42 | + // Single schema file mode |
| 43 | + const fullSchemaPath = resolve(cwd, schemaPath); |
| 44 | + |
| 45 | + // Check if schema file exists |
| 46 | + if (!existsSync(fullSchemaPath)) { |
| 47 | + throw new Error(`Schema file not found: ${schemaPath}\nRun 'objectui init' to create a sample schema.`); |
| 48 | + } |
| 49 | + |
| 50 | + console.log(chalk.blue('📋 Loading schema:'), chalk.cyan(schemaPath)); |
| 51 | + |
| 52 | + // Read and validate schema |
| 53 | + try { |
| 54 | + schema = parseSchemaFile(fullSchemaPath); |
| 55 | + } catch (error) { |
| 56 | + throw new Error(`Invalid schema file: ${error instanceof Error ? error.message : error}`); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Create temporary app directory |
| 61 | + const tmpDir = join(cwd, '.objectui-tmp'); |
| 62 | + mkdirSync(tmpDir, { recursive: true }); |
| 63 | + |
| 64 | + // Create temporary app files |
| 65 | + if (useFileSystemRouting) { |
| 66 | + createTempAppWithRouting(tmpDir, routes); |
| 67 | + } else { |
| 68 | + createTempApp(tmpDir, schema); |
| 69 | + } |
| 70 | + |
| 71 | + // Install dependencies |
| 72 | + console.log(chalk.blue('📦 Installing dependencies...')); |
| 73 | + try { |
| 74 | + execSync('npm install --silent --prefer-offline', { |
| 75 | + cwd: tmpDir, |
| 76 | + stdio: 'pipe', |
| 77 | + }); |
| 78 | + console.log(chalk.green('✓ Dependencies installed')); |
| 79 | + } catch { |
| 80 | + throw new Error('Failed to install dependencies. Please check your internet connection and try again.'); |
| 81 | + } |
| 82 | + |
| 83 | + console.log(chalk.blue('⚙️ Building with Vite...')); |
| 84 | + console.log(); |
| 85 | + |
| 86 | + // Clean output directory if requested |
| 87 | + if (options.clean && existsSync(outputPath)) { |
| 88 | + console.log(chalk.dim(` Cleaning ${outDir}/ directory...`)); |
| 89 | + rmSync(outputPath, { recursive: true, force: true }); |
| 90 | + } |
| 91 | + |
| 92 | + // Build with Vite |
| 93 | + try { |
| 94 | + await viteBuild({ |
| 95 | + root: tmpDir, |
| 96 | + build: { |
| 97 | + outDir: join(tmpDir, 'dist'), |
| 98 | + emptyOutDir: true, |
| 99 | + reportCompressedSize: true, |
| 100 | + }, |
| 101 | + plugins: [react()], |
| 102 | + logLevel: 'info', |
| 103 | + }); |
| 104 | + |
| 105 | + // Copy built files to output directory |
| 106 | + mkdirSync(outputPath, { recursive: true }); |
| 107 | + cpSync(join(tmpDir, 'dist'), outputPath, { recursive: true }); |
| 108 | + |
| 109 | + console.log(); |
| 110 | + console.log(chalk.green('✓ Build completed successfully!')); |
| 111 | + console.log(); |
| 112 | + console.log(chalk.bold(' Output: ') + chalk.cyan(outDir + '/')); |
| 113 | + console.log(); |
| 114 | + console.log(chalk.dim(' To serve the production build, run:')); |
| 115 | + console.log(chalk.cyan(` objectui start --dir ${outDir}`)); |
| 116 | + console.log(); |
| 117 | + } catch (error) { |
| 118 | + throw new Error(`Build failed: ${error instanceof Error ? error.message : error}`); |
| 119 | + } |
| 120 | +} |
0 commit comments