UNPKG

kawkab-frontend

Version:

Kawkab frontend is a frontend library for the Kawkab framework

47 lines (46 loc) • 2.12 kB
import { execSync } from 'child_process'; import chalk from 'chalk'; import fs from 'fs'; import path from 'path'; export function previewCommand(program) { program .command('preview') .description('Locally preview the static production build.') .action(async () => { const projectRoot = process.cwd(); const spaBuildDir = path.join(projectRoot, 'web', 'client'); const htaccessPath = path.join(spaBuildDir, '.htaccess'); // --- Smart Check --- // We check for .htaccess as a definitive sign that 'build:static' was run. if (!fs.existsSync(htaccessPath)) { console.error(chalk.red.bold('āŒ Cannot start preview.')); console.error(chalk.yellow('The build in "./web/client" is not a static build or is incomplete.')); console.error(chalk.cyan('Please run "kawkab-frontend static" first to generate a static build.')); process.exit(1); } // --- Read the port from the configuration file --- let port = 3000; // Default port const configPath = path.join(projectRoot, 'app', 'configuration.ts'); try { if (fs.existsSync(configPath)) { // Use dynamic import to read the TypeScript file const configModule = await import(configPath); port = configModule.default?.server?.port || 3000; } } catch (e) { console.warn(chalk.yellow(`āš ļø Could not read port from configuration. Defaulting to ${port}.`)); } console.log(chalk.blue(`šŸš€ Starting a static server for "${chalk.cyan(spaBuildDir)}" on port ${port}...`)); try { // The -s flag tells 'serve' to operate in single-page app mode. // The -l flag sets the listen port. const command = `npx serve -s ${spaBuildDir} -l ${port}`; execSync(command, { stdio: 'inherit' }); } catch (error) { console.error(chalk.red('\nāŒ Failed to start the preview server.')); process.exit(1); } }); }