UNPKG

kawkab-frontend

Version:

Kawkab frontend is a frontend library for the Kawkab framework

55 lines (54 loc) • 2.57 kB
import { execSync } from 'child_process'; import fs from 'fs'; import path from 'path'; import chalk from 'chalk'; export function buildStaticCommand(program) { program .command('build:static') .description('Build the application as a static SPA and create server config files.') .action(() => { try { // --- Step 1: Generate routes --- console.log(chalk.cyan('šŸ”„ Generating routes...')); execSync('kawkab-frontend-generate-routes --no-watch', { stdio: 'inherit' }); console.log(chalk.green('āœ… Routes generated successfully.')); // --- Step 2: Run the main build process --- console.log(chalk.blue('šŸš€ Starting static build.')); execSync('npx cross-env BUILD_MODE=spa react-router build', { stdio: 'inherit' }); console.log(chalk.green('āœ… React Router build complete.')); } catch (buildError) { console.error(chalk.red('\nāŒ The build process failed. Halting post-build setup.'), buildError); process.exit(1); } try { // --- Step 3: Create server configuration files --- console.log(chalk.blue('\nšŸš€ Creating server configuration files...')); const projectRoot = process.cwd(); const targetDir = path.join(projectRoot, 'web', 'client'); // Verify build output directory exists if (!fs.existsSync(targetDir)) { console.error(chalk.red(`āŒ Build output directory not found at: ${targetDir}`)); console.error(chalk.yellow('This might happen if the build process failed silently.')); process.exit(1); } // --- Create .htaccess for Apache --- const htaccessContent = `<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule>`; const htaccessPath = path.join(targetDir, '.htaccess'); fs.writeFileSync(htaccessPath, htaccessContent.trim()); console.log(chalk.green(` āœ“ Created Apache config: ${chalk.cyan(htaccessPath)}`)); console.log(chalk.bold.green('\nšŸŽ‰ Static build and setup completed successfully!')); } catch (setupError) { console.error(chalk.red('\nāŒ An error occurred during the post-build setup:'), setupError); process.exit(1); } }); }