UNPKG

freshroute-server

Version:

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

162 lines (135 loc) • 5.22 kB
import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; import fs from 'fs/promises'; import os from 'os'; import path from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const mockServerRoot = join(__dirname, '..'); const extensionSourcePath = join(mockServerRoot, 'extension'); export async function installExtension() { console.log('šŸ“¦ Installing FreshRoute Chrome Extension...'); try { // Check if extension exists const extensionExists = await fs.access(extensionSourcePath).then(() => true).catch(() => false); if (!extensionExists) { throw new Error('Extension not found. Run "npm run build:extension" first.'); } // Create installation directory in user's home const userHome = os.homedir(); const installDir = join(userHome, '.freshroute'); const extensionInstallPath = join(installDir, 'extension'); // Ensure install directory exists await fs.mkdir(installDir, { recursive: true }); // Remove existing installation try { await fs.rm(extensionInstallPath, { recursive: true, force: true }); } catch (error) { // Directory might not exist, that's ok } // Copy extension to install directory console.log('šŸ“ Copying extension to:', extensionInstallPath); await copyDirectory(extensionSourcePath, extensionInstallPath); // Copy setup.html to install directory const setupSourcePath = join(mockServerRoot, 'setup.html'); const setupDestPath = join(installDir, 'setup.html'); try { await fs.copyFile(setupSourcePath, setupDestPath); console.log('šŸ“„ Copied setup.html to:', setupDestPath); } catch (error) { console.log('āš ļø Could not copy setup.html:', error.message); } // Create installation info const installInfo = { installedAt: new Date().toISOString(), installPath: extensionInstallPath, version: '1.0.0', source: 'freshroute-server npm package', instructions: [ 'Open Chrome and navigate to chrome://extensions/', 'Enable "Developer mode" (top right toggle)', 'Click "Load unpacked" button', `Select the folder: ${extensionInstallPath}`, 'The FreshRoute extension should now be active!' ] }; await fs.writeFile( join(installDir, 'install-info.json'), JSON.stringify(installInfo, null, 2) ); // Create a simple launcher script (optional) await createLauncherScript(installDir, extensionInstallPath); console.log('āœ… Extension installed to:', extensionInstallPath); console.log('šŸ“‹ Installation info saved to:', join(installDir, 'install-info.json')); return { installPath: extensionInstallPath, installInfo }; } catch (error) { console.error('āŒ Installation failed:', error.message); throw error; } } 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); } } } async function createLauncherScript(installDir, extensionPath) { const isWindows = process.platform === 'win32'; const scriptName = isWindows ? 'open-chrome-extensions.bat' : 'open-chrome-extensions.sh'; const scriptPath = join(installDir, scriptName); let scriptContent; if (isWindows) { scriptContent = `@echo off echo Opening Chrome extensions page... start chrome://extensions/ echo. echo To install FreshRoute extension: echo 1. Enable "Developer mode" (top right toggle) echo 2. Click "Load unpacked" echo 3. Select folder: ${extensionPath.replace(/\//g, '\\')} pause`; } else { scriptContent = `#!/bin/bash echo "Opening Chrome extensions page..." open "chrome://extensions/" 2>/dev/null || google-chrome "chrome://extensions/" 2>/dev/null || echo "Please open chrome://extensions/ manually" echo echo "To install FreshRoute extension:" echo "1. Enable 'Developer mode' (top right toggle)" echo "2. Click 'Load unpacked'" echo "3. Select folder: ${extensionPath}" read -p "Press enter to continue..."`; } await fs.writeFile(scriptPath, scriptContent); if (!isWindows) { // Make shell script executable await fs.chmod(scriptPath, '755'); } } // If run directly if (import.meta.url === `file://${process.argv[1]}`) { installExtension() .then((result) => { console.log('šŸŽ‰ Extension installation complete!'); console.log('šŸ“ Installed at:', result.installPath); console.log('\nšŸ“‹ Next steps:'); console.log('1. Open Chrome: chrome://extensions/'); console.log('2. Enable "Developer mode"'); console.log('3. Click "Load unpacked"'); console.log(`4. Select: ${result.installPath}`); process.exit(0); }) .catch((error) => { console.error('šŸ’„ Installation failed:', error); process.exit(1); }); }