Repository navigation
Expand file tree
/
Copy pathnodeapp.py
More file actions
276 lines (229 loc) · 10.4 KB
/
Copy pathnodeapp.py
File metadata and controls
276 lines (229 loc) · 10.4 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import json
import subprocess
import threading
import sys
import platform
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
class NodeManagerApp:
def __init__(self, root):
self.root = root
self.root.title("Node App Task Manager")
self.root.geometry("650x570")
self.root.minsize(550, 450)
self.current_dir = ""
self.package_manager = "npm"
self.scripts = {}
self.active_process = None # Reference to track the current running process
self.create_widgets()
def create_widgets(self):
# Top Frame - Directory Selector
dir_frame = ttk.LabelFrame(self.root, text="Repository Directory", padding=10)
dir_frame.pack(fill="x", padx=10, pady=5)
self.dir_entry = ttk.Entry(dir_frame)
self.dir_entry.pack(side="left", fill="x", expand=True, padx=(0, 5))
btn_browse = ttk.Button(dir_frame, text="Browse...", command=self.browse_directory)
btn_browse.pack(side="right")
# Info Frame - Manager & Shell
info_frame = ttk.Frame(self.root, padding=(10, 5))
info_frame.pack(fill="x", padx=10)
# Detected Package Manager Indicator
self.pm_label = ttk.Label(info_frame, text="Package Manager: Undetected", font=("Segoe UI", 9, "bold"))
self.pm_label.pack(side="left")
# Windows Shell Selector
if platform.system() == "Windows":
shell_frame = ttk.Frame(info_frame)
shell_frame.pack(side="right")
ttk.Label(shell_frame, text="Shell: ").pack(side="left")
self.shell_var = tk.StringVar(value="powershell")
shell_combo = ttk.Combobox(
shell_frame,
textvariable=self.shell_var,
values=["powershell", "cmd"],
state="readonly",
width=12
)
shell_combo.pack(side="right")
else:
self.shell_var = None
# Main Content Frame
main_frame = ttk.LabelFrame(self.root, text="Scripts & Actions", padding=10)
main_frame.pack(fill="both", expand=True, padx=10, pady=5)
# Action bar (Install, Run, Stop buttons)
actions_frame = ttk.Frame(main_frame)
actions_frame.pack(fill="x", pady=(0, 5))
self.btn_install = ttk.Button(
actions_frame,
text="Install Dependencies",
command=self.run_install,
state="disabled"
)
self.btn_install.pack(side="left", padx=(0, 5))
self.btn_run = ttk.Button(
actions_frame,
text="Run Selected Script",
command=self.run_script,
state="disabled"
)
self.btn_run.pack(side="left", padx=(0, 5))
self.btn_stop = ttk.Button(
actions_frame,
text="Stop Execution",
command=self.stop_command,
state="disabled"
)
self.btn_stop.pack(side="left")
# Listbox for package.json scripts
list_frame = ttk.Frame(main_frame)
list_frame.pack(fill="both", expand=True, pady=5)
self.script_listbox = tk.Listbox(list_frame, selectmode=tk.SINGLE, font=("Consolas", 10))
scrollbar = ttk.Scrollbar(list_frame, orient="vertical", command=self.script_listbox.yview)
self.script_listbox.config(yscrollcommand=scrollbar.set)
self.script_listbox.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# Output Terminal Frame
output_frame = ttk.LabelFrame(self.root, text="Console Output", padding=10)
output_frame.pack(fill="both", expand=True, padx=10, pady=(5, 10))
self.output_text = tk.Text(output_frame, height=8, wrap="word", bg="#1e1e1e", fg="#d4d4d4", font=("Consolas", 9))
output_scrollbar = ttk.Scrollbar(output_frame, orient="vertical", command=self.output_text.yview)
self.output_text.config(yscrollcommand=output_scrollbar.set)
self.output_text.pack(side="left", fill="both", expand=True)
output_scrollbar.pack(side="right", fill="y")
def browse_directory(self):
selected_dir = filedialog.askdirectory()
if selected_dir:
self.current_dir = selected_dir
self.dir_entry.delete(0, tk.END)
self.dir_entry.insert(0, selected_dir)
self.load_repository_info()
def detect_package_manager(self):
"""Detects package manager based on lockfiles, defaulting to npm."""
pnpm_lock = os.path.exists(os.path.join(self.current_dir, "pnpm-lock.yaml"))
yarn_lock = os.path.exists(os.path.join(self.current_dir, "yarn.lock"))
bun_lock = os.path.exists(os.path.join(self.current_dir, "bun.lockb")) or os.path.exists(os.path.join(self.current_dir, "bun.lock"))
if pnpm_lock:
return "pnpm"
elif yarn_lock:
return "yarn"
elif bun_lock:
return "bun"
return "npm"
def load_repository_info(self):
self.script_listbox.delete(0, tk.END)
self.output_text.delete("1.0", tk.END)
self.scripts = {}
pkg_path = os.path.join(self.current_dir, "package.json")
if not os.path.exists(pkg_path):
messagebox.showerror("Error", "No package.json found in the selected folder.")
self.pm_label.config(text="Package Manager: N/A")
self.btn_install.config(state="disabled")
self.btn_run.config(state="disabled")
self.btn_stop.config(state="disabled")
return
# 1. Detect Package Manager
self.package_manager = self.detect_package_manager()
self.pm_label.config(text=f"Package Manager: {self.package_manager}")
# 2. Parse package.json scripts
try:
with open(pkg_path, "r", encoding="utf-8") as f:
data = json.load(f)
self.scripts = data.get("scripts", {})
for script_name, cmd in self.scripts.items():
self.script_listbox.insert(tk.END, f"{script_name} --> {cmd}")
except Exception as e:
messagebox.showerror("Error", f"Failed to parse package.json:\n{str(e)}")
return
# Enable/Disable Install Button based on node_modules presence
node_modules_path = os.path.join(self.current_dir, "node_modules")
if not os.path.exists(node_modules_path):
self.btn_install.config(state="normal")
self.log_output("Notice: 'node_modules' directory is missing. Click 'Install Dependencies' to set up.\n")
else:
self.btn_install.config(state="normal")
if self.scripts:
self.btn_run.config(state="normal")
def run_install(self):
cmd = f"{self.package_manager} install"
self.execute_command(cmd)
def run_script(self):
selection = self.script_listbox.curselection()
if not selection:
messagebox.showwarning("Warning", "Please select a script to run.")
return
script_key = list(self.scripts.keys())[selection[0]]
# Build command depending on the package manager
if self.package_manager in ["npm", "pnpm"]:
cmd = f"{self.package_manager} run {script_key}"
else:
cmd = f"{self.package_manager} {script_key}"
self.execute_command(cmd)
def stop_command(self):
if self.active_process and self.active_process.poll() is None:
self.log_output("\n> Stopping process...\n")
try:
if platform.system() == "Windows":
# Kills process tree (/T) forcefully (/F) by PID on Windows
subprocess.run(
f"taskkill /F /T /PID {self.active_process.pid}",
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
else:
self.active_process.terminate()
except Exception as e:
self.log_output(f"\nFailed to terminate process: {str(e)}\n")
def set_running_state(self, is_running):
"""Toggles button states during process execution."""
if is_running:
self.btn_run.config(state="disabled")
self.btn_install.config(state="disabled")
self.btn_stop.config(state="normal")
else:
self.btn_run.config(state="normal" if self.scripts else "disabled")
self.btn_install.config(state="normal")
self.btn_stop.config(state="disabled")
def execute_command(self, full_cmd):
self.output_text.delete("1.0", tk.END)
self.log_output(f"> Executing: {full_cmd}\n\n")
# Format execution string based on OS and user-selected Windows shell
if platform.system() == "Windows":
selected_shell = self.shell_var.get() if self.shell_var else "powershell"
if selected_shell == "powershell":
args = f'powershell.exe -Command "{full_cmd}"'
else:
args = f'cmd.exe /c "{full_cmd}"'
else:
args = full_cmd
self.set_running_state(is_running=True)
def worker():
try:
self.active_process = subprocess.Popen(
args,
cwd=self.current_dir,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
for line in iter(self.active_process.stdout.readline, ''):
self.root.after(0, self.log_output, line)
self.active_process.stdout.close()
self.active_process.wait()
exit_code = self.active_process.returncode
self.root.after(0, self.log_output, f"\n[Process exited with code {exit_code}]\n")
except Exception as e:
self.root.after(0, self.log_output, f"\nExecution failed: {str(e)}\n")
finally:
self.active_process = None
self.root.after(0, self.set_running_state, False)
threading.Thread(target=worker, daemon=True).start()
def log_output(self, text):
self.output_text.insert(tk.END, text)
self.output_text.see(tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = NodeManagerApp(root)
root.mainloop()