chore: Refactor run_command function in build.py to handle exceptions

```
This commit is contained in:
Théophile Diot 2024-07-31 10:47:01 +01:00
parent a4d4c563b5
commit 43142134e8
No known key found for this signature in database
GPG key ID: FA995104A0BA376A

View file

@ -40,16 +40,20 @@ def set_setup():
def run_command(command: List[str]) -> int:
"""Utils to run a subprocess command. This is usefull to run npm commands to build vite project"""
print(f"Running command: {command}", flush=True)
process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=current_directory, text=True)
while process.poll() is None:
if process.stdout is not None:
for line in process.stdout:
print(line.strip(), flush=True)
try:
process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=current_directory, text=True)
while process.poll() is None:
if process.stdout is not None:
for line in process.stdout:
print(line.strip(), flush=True)
if process.returncode != 0:
print("Error while running command", flush=True)
print(process.stdout, flush=True)
print(process.stderr, flush=True)
if process.returncode != 0:
print("Error while running command", flush=True)
print(process.stdout, flush=True)
print(process.stderr, flush=True)
return 1
except BaseException as e:
print(f"Error while running command: {e}", flush=True)
return 1
print("Command executed successfully", flush=True)