kawkab-frontend
Version:
Kawkab frontend is a frontend library for the Kawkab framework
55 lines (54 loc) ⢠2.57 kB
JavaScript
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);
}
});
}