File size: 4,215 Bytes
4b77a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/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()