#!/usr/bin/env python3 """ Lin - Community Manager Assistant for LinkedIn Universal startup script for development servers """ import os import sys import subprocess import platform from pathlib import Path def is_windows(): """Check if the current OS is Windows""" return platform.system().lower() == 'windows' def run_command(command, cwd=None, shell=False): """Run a command and stream output""" try: # For Windows, we need to use shell=True for certain commands use_shell = shell or is_windows() process = subprocess.Popen( command, cwd=cwd, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True ) # Stream output in real-time for line in process.stdout: print(line, end='') process.wait() return process.returncode == 0 except Exception as e: print(f"Error running command: {e}") return False def start_frontend(): """Start the React frontend development server""" print("Starting frontend development server...") if is_windows(): cmd = "npm run dev:frontend" else: cmd = ["npm", "run", "dev:frontend"] return run_command(cmd, shell=True) def start_backend(): """Start the Flask backend server""" print("Starting backend server...") # Set environment variables and run the app env = os.environ.copy() # Add the project root to PYTHONPATH to ensure imports work correctly env["PYTHONPATH"] = str(Path(__file__).parent) try: # Run the Flask app directly from the root directory process = subprocess.Popen( [sys.executable, "app.py"], cwd=Path(__file__).parent, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True ) # Stream output for line in process.stdout: print(line, end='') process.wait() return process.returncode == 0 except Exception as e: print(f"Error starting backend: {e}") return False def start_all(): """Start both frontend and backend using npm concurrently script""" print("Starting both frontend and backend servers...") # Use the frontend's concurrently script to run both servers if is_windows(): cmd = "cd frontend && npx concurrently \"npm run dev:frontend\" \"npm run dev:backend\"" else: cmd = "cd frontend && npx concurrently \"npm run dev:frontend\" \"npm run dev:backend\"" return run_command(cmd, shell=True) def show_help(): """Display usage information""" help_text = """ Lin Development Launcher Usage: python starty.py [command] Commands: backend Start only the backend server (default) frontend Start only the frontend development server dev:all Start both frontend and backend servers help Show this help message Examples: python starty.py # Start backend server python starty.py backend # Start backend server python starty.py frontend # Start frontend server python starty.py dev:all # Start both servers """ print(help_text) def main(): """Main entry point""" # Always ensure we're in the project root project_root = Path(__file__).parent os.chdir(project_root) if len(sys.argv) < 2: # Default behavior - start backend start_backend() return command = sys.argv[1].lower() if command in ['help', '--help', '-h']: show_help() elif command == 'frontend': start_frontend() elif command == 'backend': start_backend() elif command in ['dev:all', 'all', 'both']: # Instead of calling npm run dev:all (which creates a loop), # directly start both servers start_all() else: print(f"Unknown command: {command}") show_help() sys.exit(1) if __name__ == "__main__": main()