1212from typing import NoReturn , Optional
1313
1414_WRAPPER_ENV = "PROMPTFOO_PY_WRAPPER"
15+ _WINDOWS_SHELL_EXTENSIONS = (".bat" , ".cmd" )
1516
1617
1718def check_node_installed () -> bool :
@@ -73,6 +74,19 @@ def _find_external_promptfoo() -> Optional[str]:
7374 return promptfoo_path
7475
7576
77+ def _requires_shell (executable : str ) -> bool :
78+ if os .name != "nt" :
79+ return False
80+ _ , ext = os .path .splitext (executable )
81+ return ext .lower () in _WINDOWS_SHELL_EXTENSIONS
82+
83+
84+ def _run_command (cmd : list [str ], env : Optional [dict [str , str ]] = None ) -> subprocess .CompletedProcess :
85+ if _requires_shell (cmd [0 ]):
86+ return subprocess .run (subprocess .list2cmdline (cmd ), shell = True , env = env )
87+ return subprocess .run (cmd , env = env )
88+
89+
7690def main () -> NoReturn :
7791 """
7892 Main entry point for the promptfoo CLI wrapper.
@@ -90,15 +104,17 @@ def main() -> NoReturn:
90104 cmd = [promptfoo_path ] + sys .argv [1 :]
91105 env = os .environ .copy ()
92106 env [_WRAPPER_ENV ] = "1"
93- result = subprocess .run (cmd , env = env )
94- elif shutil .which ("npx" ):
95- cmd = ["npx" , "-y" , "promptfoo@latest" ] + sys .argv [1 :]
96- result = subprocess .run (cmd )
107+ result = _run_command (cmd , env = env )
97108 else :
98- print ("ERROR: Neither promptfoo nor npx is available." , file = sys .stderr )
99- print ("Please install promptfoo: npm install -g promptfoo" , file = sys .stderr )
100- print ("Or ensure Node.js is properly installed." , file = sys .stderr )
101- sys .exit (1 )
109+ npx_path = shutil .which ("npx" )
110+ if npx_path :
111+ cmd = [npx_path , "-y" , "promptfoo@latest" ] + sys .argv [1 :]
112+ result = _run_command (cmd )
113+ else :
114+ print ("ERROR: Neither promptfoo nor npx is available." , file = sys .stderr )
115+ print ("Please install promptfoo: npm install -g promptfoo" , file = sys .stderr )
116+ print ("Or ensure Node.js is properly installed." , file = sys .stderr )
117+ sys .exit (1 )
102118
103119 sys .exit (result .returncode )
104120
0 commit comments