UNPKG

freshroute-server

Version:

Local development server for FreshRoute extension with API mocking and extended functionality

164 lines (139 loc) • 5.93 kB
import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; import fs from 'fs/promises'; import chalk from 'chalk'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const mockServerRoot = join(__dirname, '..'); const projectRoot = join(mockServerRoot, '..'); async function validatePackage() { console.log(chalk.blue.bold('šŸ” Validating package before publish...\n')); let errors = []; let warnings = []; try { // Check if we're in the right environment const parentPackageExists = await fs.access(join(projectRoot, 'package.json')).then(() => true).catch(() => false); if (!parentPackageExists) { warnings.push('Parent project not found - extension will use pre-built version'); } else { console.log(chalk.green('āœ“ Parent project found')); // Check if parent project has dist folder const distExists = await fs.access(join(projectRoot, 'dist')).then(() => true).catch(() => false); if (!distExists) { warnings.push('Parent project dist folder not found - will build extension'); } else { console.log(chalk.green('āœ“ Parent project dist folder exists')); } } // Check package.json const packagePath = join(mockServerRoot, 'package.json'); const packageJson = JSON.parse(await fs.readFile(packagePath, 'utf8')); console.log(chalk.cyan('\nšŸ“¦ Package Validation:')); console.log(`Name: ${chalk.green(packageJson.name)}`); console.log(`Version: ${chalk.green(packageJson.version)}`); console.log(`Description: ${packageJson.description}`); // Validate required fields if (!packageJson.name) errors.push('Package name is required'); if (!packageJson.version) errors.push('Package version is required'); if (!packageJson.description) errors.push('Package description is required'); if (!packageJson.main) errors.push('Package main field is required'); if (!packageJson.bin) errors.push('Package bin field is required'); // Check required files exist console.log(chalk.cyan('\nšŸ“ Required Files:')); const requiredFiles = [ 'server.js', 'bin/freshroute-cli.js', 'scripts/build-extension.js', 'scripts/install-extension.js', 'default-rules.json', 'README.md' ]; for (const file of requiredFiles) { const filePath = join(mockServerRoot, file); const exists = await fs.access(filePath).then(() => true).catch(() => false); if (exists) { console.log(`${chalk.green('āœ“')} ${file}`); } else { errors.push(`Required file missing: ${file}`); console.log(`${chalk.red('āœ—')} ${file}`); } } // Check CLI executable permission const cliPath = join(mockServerRoot, 'bin/freshroute-cli.js'); try { const stats = await fs.stat(cliPath); const isExecutable = !!(stats.mode & parseInt('111', 8)); if (isExecutable) { console.log(chalk.green('āœ“ CLI script is executable')); } else { warnings.push('CLI script may not be executable on Unix systems'); } } catch (error) { errors.push('Cannot check CLI script permissions'); } // Check dependencies console.log(chalk.cyan('\nšŸ“š Dependencies:')); const requiredDeps = ['express', 'ws', 'commander', 'chalk', 'ora']; for (const dep of requiredDeps) { if (packageJson.dependencies[dep]) { console.log(`${chalk.green('āœ“')} ${dep}: ${packageJson.dependencies[dep]}`); } else { errors.push(`Required dependency missing: ${dep}`); } } // Check responses directory const responsesPath = join(mockServerRoot, 'responses'); const responsesExists = await fs.access(responsesPath).then(() => true).catch(() => false); if (responsesExists) { console.log(chalk.green('āœ“ Responses directory exists')); } else { warnings.push('Responses directory not found - will be created during build'); } // Validate package files configuration if (packageJson.files && packageJson.files.length > 0) { console.log(chalk.green('āœ“ Package files configuration exists')); if (!packageJson.files.includes('extension/')) { errors.push('Package files must include "extension/" directory'); } } else { errors.push('Package files configuration is required'); } // Summary console.log(chalk.cyan('\nšŸ“Š Validation Summary:')); if (errors.length > 0) { console.log(chalk.red.bold(`āŒ ${errors.length} error(s) found:`)); errors.forEach(error => console.log(` • ${chalk.red(error)}`)); } if (warnings.length > 0) { console.log(chalk.yellow.bold(`āš ļø ${warnings.length} warning(s):`)); warnings.forEach(warning => console.log(` • ${chalk.yellow(warning)}`)); } if (errors.length === 0 && warnings.length === 0) { console.log(chalk.green.bold('āœ… Package validation passed!')); } else if (errors.length === 0) { console.log(chalk.yellow.bold('āš ļø Package validation passed with warnings')); } console.log(chalk.blue('\nšŸš€ Next steps:')); console.log('• Extension will be built automatically'); console.log('• Package will be ready for npm publish'); if (errors.length > 0) { process.exit(1); } } catch (error) { console.error(chalk.red.bold('āŒ Validation failed:'), error.message); process.exit(1); } } // If run directly if (import.meta.url === `file://${process.argv[1]}`) { validatePackage() .then(() => { console.log(chalk.green('\nšŸŽ‰ Package validation complete!')); process.exit(0); }) .catch((error) => { console.error(chalk.red('šŸ’„ Validation failed:'), error); process.exit(1); }); } export { validatePackage };