freshroute-server
Version:
Local development server for FreshRoute extension with API mocking and extended functionality
164 lines (139 loc) ⢠5.93 kB
JavaScript
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 };