freshroute-server
Version:
Local development server for FreshRoute extension with API mocking and extended functionality
207 lines (171 loc) ⢠9.24 kB
JavaScript
import chalk from 'chalk';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import fs from 'fs/promises';
import os from 'os';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log(chalk.blue.bold('\nš FreshRoute Server installed successfully!\n'));
// Copy extension files and setup.html to ~/.freshroute/
async function copyExtensionFiles() {
try {
console.log(chalk.cyan('š¦ Setting up FreshRoute files...'));
const mockServerRoot = join(__dirname, '..');
const extensionSourcePath = join(mockServerRoot, 'extension');
const setupSourcePath = join(mockServerRoot, 'setup.html');
// Create ~/.freshroute directory
const userHome = os.homedir();
const installDir = join(userHome, '.freshroute');
const extensionInstallPath = join(installDir, 'extension');
const setupDestPath = join(installDir, 'setup.html');
// Ensure install directory exists
await fs.mkdir(installDir, { recursive: true });
// Copy extension files
const extensionExists = await fs.access(extensionSourcePath).then(() => true).catch(() => false);
if (extensionExists) {
// Remove existing extension
try {
await fs.rm(extensionInstallPath, { recursive: true, force: true });
} catch (error) {
// Directory might not exist, that's ok
}
// Copy extension
await copyDirectory(extensionSourcePath, extensionInstallPath);
console.log(chalk.green('ā Extension files copied to ~/.freshroute/extension/'));
}
// Copy setup.html
const setupExists = await fs.access(setupSourcePath).then(() => true).catch(() => false);
if (setupExists) {
await fs.copyFile(setupSourcePath, setupDestPath);
console.log(chalk.green('ā Setup page copied to ~/.freshroute/setup.html'));
}
// Create installation info
const installInfo = {
installedAt: new Date().toISOString(),
installPath: extensionInstallPath,
setupPath: setupDestPath,
version: '2.1.0',
source: 'freshroute-server npm package'
};
await fs.writeFile(
join(installDir, 'install-info.json'),
JSON.stringify(installInfo, null, 2)
);
console.log(chalk.green('ā
FreshRoute files setup complete!'));
} catch (error) {
console.log(chalk.yellow('ā ļø Could not copy extension files:', error.message));
console.log(chalk.gray(' You can run "freshroute install-extension" manually'));
}
}
// Helper function to copy directories recursively
async function copyDirectory(src, dest) {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
// Auto-create shortcuts after installation
async function createShortcutsAfterInstall() {
// First copy extension files
await copyExtensionFiles();
try {
console.log(chalk.cyan('š Creating desktop shortcuts...'));
// Run the create-shortcuts command
const createShortcuts = spawn('freshroute', ['create-shortcuts', '--auto'], {
stdio: 'pipe',
shell: true
});
let output = '';
createShortcuts.stdout.on('data', (data) => {
output += data.toString();
});
createShortcuts.stderr.on('data', (data) => {
output += data.toString();
});
createShortcuts.on('close', (code) => {
if (code === 0) {
console.log(chalk.green('ā
Desktop shortcuts created successfully!'));
// Show platform-specific shortcut locations
const platform = process.platform;
if (platform === 'darwin') {
console.log(chalk.blue('š Shortcuts created on Desktop and Applications folder'));
} else if (platform === 'win32') {
console.log(chalk.blue('š Shortcuts created on Desktop and Start Menu'));
} else {
console.log(chalk.blue('š Shortcuts created on Desktop'));
}
} else {
console.log(chalk.yellow('ā ļø Could not create shortcuts automatically'));
console.log(chalk.gray(' Run "freshroute create-shortcuts" manually if needed'));
}
showPostInstallInfo();
});
} catch (error) {
console.log(chalk.yellow('ā ļø Could not create shortcuts automatically'));
console.log(chalk.gray(' Run "freshroute create-shortcuts" manually if needed'));
showPostInstallInfo();
}
}
function showPostInstallInfo() {
console.log(chalk.cyan('\nš Quick Start:'));
console.log(' ' + chalk.green('freshroute setup') + ' # Complete setup (install extension)');
console.log(' ' + chalk.green('freshroute start --silent') + ' # Start server in background');
console.log(' ' + chalk.green('freshroute open-chrome') + ' # Open Chrome with extension & CORS disabled');
console.log(chalk.cyan('\nš ļø Available Commands:'));
console.log(' ' + chalk.yellow('freshroute start [options]') + ' # Start server (--silent for background)');
console.log(' ' + chalk.yellow('freshroute stop') + ' # Stop running server');
console.log(' ' + chalk.yellow('freshroute status') + ' # Check server status');
console.log(' ' + chalk.yellow('freshroute setup') + ' # Complete setup process');
console.log(' ' + chalk.yellow('freshroute install-extension') + ' # Install Chrome extension');
console.log(' ' + chalk.yellow('freshroute open-chrome') + ' # Open Chrome with dev settings');
console.log(' ' + chalk.yellow('freshroute create-shortcuts') + ' # Create desktop shortcuts');
console.log(chalk.cyan('\nš Server Endpoints:'));
console.log(' HTTP Server: ' + chalk.green('http://localhost:3001'));
console.log(' WebSocket: ' + chalk.green('ws://localhost:3002'));
console.log(' Health Check: ' + chalk.green('http://localhost:3001/health'));
console.log(' API Proxy: ' + chalk.green('http://localhost:3001/proxy/*'));
console.log(chalk.cyan('\nš§ Chrome Development Features:'));
console.log(' ⢠CORS disabled for API testing');
console.log(' ⢠Extension auto-loaded in development profile');
console.log(' ⢠Separate development profile (safe & isolated)');
console.log(' ⢠Background throttling disabled');
console.log(chalk.yellow('\nā ļø Important About Chrome Extension:'));
console.log(' ⢠Extension uses DEVELOPMENT profile only');
console.log(' ⢠Will NOT appear in your regular Chrome browser');
console.log(' ⢠Access via: ' + chalk.green('freshroute open-chrome'));
console.log(' ⢠For regular Chrome: ' + chalk.green('freshroute install-manual'));
console.log(chalk.cyan('\nš„ļø Desktop Shortcuts:'));
console.log(' ⢠FreshRoute shortcuts should now be on your desktop');
console.log(' ⢠Double-click to open Chrome with FreshRoute extension');
console.log(' ⢠Run ' + chalk.green('freshroute create-shortcuts') + ' to recreate if needed');
console.log(' ⢠Run ' + chalk.green('freshroute remove-shortcuts') + ' to remove shortcuts');
console.log(chalk.cyan('\nš Documentation:'));
console.log(' Run ' + chalk.green('freshroute --help') + ' for detailed command information');
console.log(' Extension location: ~/.freshroute/extension/');
console.log(chalk.gray('\nš” Tip: Use the desktop shortcuts or run "freshroute setup" to get started!\n'));
}
// Check if this is a global installation and create shortcuts
const isGlobalInstall = process.env.npm_config_global === 'true' ||
process.argv.includes('-g') ||
process.argv.includes('--global') ||
__dirname.includes('/lib/node_modules/') ||
__dirname.includes('\\node_modules\\');
if (isGlobalInstall) {
// Wait a moment for the CLI to be available, then create shortcuts
setTimeout(() => {
createShortcutsAfterInstall();
}, 1000);
} else {
console.log(chalk.yellow('\nš¦ Local installation detected'));
console.log(chalk.gray(' Install globally with: npm install -g freshroute-server'));
console.log(chalk.gray(' Then run: freshroute create-shortcuts'));
showPostInstallInfo();
}