-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-script.py
More file actions
77 lines (65 loc) · 2.33 KB
/
run-script.py
File metadata and controls
77 lines (65 loc) · 2.33 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
import subprocess
import sys
import os.path
import json
import shutil
with open("%s/%s" % (sys.path[0], 'script-aliases.json'), 'r') as f:
aliases = json.load(f)
def script_path(scr):
if os.path.isabs(scr):
return scr
return os.path.abspath("%s/%s" % (sys.path[0], scr))
def call_script(scr):
try:
if scr.endswith('.py'):
call_python_script(scr)
elif scr.endswith('.ts') or scr.endswith('.tsx'):
call_typescript_script(scr)
else:
call_bash_script(scr)
except subprocess.CalledProcessError as e:
# This is the specific exception raised by check_call() on failure
print("Error running script '%s': %s" % (scr, e))
# Optionally exit with the same return code
sys.exit(e.returncode)
except Exception as e:
# This catches any other error
print("Unexpected error while running script '%s': %s" % (scr, e))
sys.exit(1)
def call_bash_script(scr):
subprocess.check_call([script_path(scr)] + sys.argv[2:])
def call_python_script(scr):
subprocess.check_call([sys.executable, script_path(scr)] + sys.argv[2:])
def call_typescript_script(scr):
full_path = script_path(scr)
tsx = shutil.which("tsx")
if tsx:
subprocess.check_call([tsx, full_path] + sys.argv[2:])
return
pnpm = shutil.which("pnpm")
if pnpm:
subprocess.check_call([pnpm, "dlx", "tsx", full_path] + sys.argv[2:])
return
npx = shutil.which("npx")
if npx:
subprocess.check_call([npx, "--yes", "tsx", full_path] + sys.argv[2:])
return
raise SystemExit("TypeScript runner not found. Install `tsx` (recommended) or `pnpm`/`npx`.")
#script = aliases[sys.argv[1]]
if (len(sys.argv) > 1):
script = sys.argv[1]
if (script in aliases):
script = aliases[sys.argv[1]]
call_script(script)
elif (os.path.isfile("%s/%s" % (sys.path[0], script))):
call_script(script)
elif (os.path.isfile("%s/%s.py" % (sys.path[0], script))):
call_script(script + '.py')
elif (os.path.isfile("%s/%s.ts" % (sys.path[0], script))):
call_script(script + '.ts')
elif (os.path.isfile("%s/%s.tsx" % (sys.path[0], script))):
call_script(script + '.tsx')
else:
print("Unable to find script %s" % (script))
else:
print('No script specified')