(path: string)
| 197 | * @returns The validated path if successful, or null if invalid. |
| 198 | */ |
| 199 | export async function tryPath(path: string): Promise<string | null> { |
| 200 | let timeoutId: NodeJS.Timeout | undefined; |
| 201 | |
| 202 | try { |
| 203 | const timeoutPromise = new Promise<never>((_, reject) => { |
| 204 | timeoutId = setTimeout(() => reject(new Error('Command timed out')), 2500); |
| 205 | }); |
| 206 | |
| 207 | const result = await Promise.race([execFileAsync(path, ['--version']), timeoutPromise]); |
| 208 | |
| 209 | if (timeoutId) { |
| 210 | clearTimeout(timeoutId); |
| 211 | } |
| 212 | |
| 213 | const versionOutput = (result as { stdout: string }).stdout.trim(); |
| 214 | if (versionOutput.startsWith('Python')) { |
| 215 | return path; |
| 216 | } |
| 217 | return null; |
| 218 | } catch { |
| 219 | if (timeoutId) { |
| 220 | clearTimeout(timeoutId); |
| 221 | } |
| 222 | return null; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Validates and caches the Python executable path. |
no outgoing calls
no test coverage detected
searching dependent graphs…