| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-26 19:41:00 +02:00
										 |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | import * as childProcess from 'child_process'; | 
					
						
							|  |  |  | import * as readline from 'readline'; | 
					
						
							| 
									
										
										
										
											2022-04-07 19:18:22 -08:00
										 |  |  | import { isUnderTest } from './'; | 
					
						
							|  |  |  | import { removeFolders } from './fileUtils'; | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 07:03:42 -07:00
										 |  |  | export type Env = {[key: string]: string | number | boolean | undefined}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | export type LaunchProcessOptions = { | 
					
						
							| 
									
										
										
										
											2021-07-07 20:19:42 +02:00
										 |  |  |   command: string, | 
					
						
							|  |  |  |   args?: string[], | 
					
						
							| 
									
										
										
										
											2020-05-22 07:03:42 -07:00
										 |  |  |   env?: Env, | 
					
						
							| 
									
										
										
										
											2021-07-07 20:19:42 +02:00
										 |  |  |   shell?: boolean, | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   handleSIGINT?: boolean, | 
					
						
							|  |  |  |   handleSIGTERM?: boolean, | 
					
						
							|  |  |  |   handleSIGHUP?: boolean, | 
					
						
							| 
									
										
										
										
											2020-11-05 14:15:25 -08:00
										 |  |  |   stdio: 'pipe' | 'stdin', | 
					
						
							| 
									
										
										
										
											2020-05-22 16:06:00 -07:00
										 |  |  |   tempDirectories: string[], | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-11 14:42:13 -07:00
										 |  |  |   cwd?: string, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  |   // Note: attemptToGracefullyClose should reject if it does not close the browser.
 | 
					
						
							|  |  |  |   attemptToGracefullyClose: () => Promise<any>, | 
					
						
							| 
									
										
										
										
											2020-05-27 19:59:03 -07:00
										 |  |  |   onExit: (exitCode: number | null, signal: string | null) => void, | 
					
						
							| 
									
										
										
										
											2020-12-08 09:35:28 -08:00
										 |  |  |   log: (message: string) => void, | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 17:56:14 -07:00
										 |  |  | type LaunchResult = { | 
					
						
							|  |  |  |   launchedProcess: childProcess.ChildProcess, | 
					
						
							|  |  |  |   gracefullyClose: () => Promise<void>, | 
					
						
							| 
									
										
										
										
											2022-11-09 09:18:33 -08:00
										 |  |  |   kill: () => Promise<void>, | 
					
						
							| 
									
										
										
										
											2020-04-02 17:56:14 -07:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  | export const gracefullyCloseSet = new Set<() => Promise<void>>(); | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  | const killSet = new Set<() => void>(); | 
					
						
							| 
									
										
										
										
											2020-07-17 16:14:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export async function gracefullyCloseAll() { | 
					
						
							|  |  |  |   await Promise.all(Array.from(gracefullyCloseSet).map(gracefullyClose => gracefullyClose().catch(e => {}))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-24 08:29:29 -07:00
										 |  |  | export function gracefullyProcessExitDoNotHang(code: number) { | 
					
						
							|  |  |  |   // Force exit after 30 seconds.
 | 
					
						
							|  |  |  |   // eslint-disable-next-line no-restricted-properties
 | 
					
						
							|  |  |  |   setTimeout(() => process.exit(code), 30000); | 
					
						
							|  |  |  |   // Meanwhile, try to gracefully close all browsers.
 | 
					
						
							|  |  |  |   gracefullyCloseAll().then(() => { | 
					
						
							|  |  |  |     // eslint-disable-next-line no-restricted-properties
 | 
					
						
							|  |  |  |     process.exit(code); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  | function exitHandler() { | 
					
						
							|  |  |  |   for (const kill of killSet) | 
					
						
							|  |  |  |     kill(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let sigintHandlerCalled = false; | 
					
						
							|  |  |  | function sigintHandler() { | 
					
						
							|  |  |  |   const exitWithCode130 = () => { | 
					
						
							|  |  |  |     // Give tests a chance to see that launched process did exit and dispatch any async calls.
 | 
					
						
							| 
									
										
										
										
											2023-07-24 08:29:29 -07:00
										 |  |  |     if (isUnderTest()) { | 
					
						
							|  |  |  |       // eslint-disable-next-line no-restricted-properties
 | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |       setTimeout(() => process.exit(130), 1000); | 
					
						
							| 
									
										
										
										
											2023-07-24 08:29:29 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       // eslint-disable-next-line no-restricted-properties
 | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |       process.exit(130); | 
					
						
							| 
									
										
										
										
											2023-07-24 08:29:29 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (sigintHandlerCalled) { | 
					
						
							|  |  |  |     // Resort to default handler from this point on, just in case we hang/stall.
 | 
					
						
							|  |  |  |     process.off('SIGINT', sigintHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Upon second Ctrl+C, immediately kill browsers and exit.
 | 
					
						
							|  |  |  |     // This prevents hanging in the case where closing the browser takes a lot of time or is buggy.
 | 
					
						
							|  |  |  |     for (const kill of killSet) | 
					
						
							|  |  |  |       kill(); | 
					
						
							|  |  |  |     exitWithCode130(); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     sigintHandlerCalled = true; | 
					
						
							|  |  |  |     gracefullyCloseAll().then(() => exitWithCode130()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function sigtermHandler() { | 
					
						
							|  |  |  |   gracefullyCloseAll(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function sighupHandler() { | 
					
						
							|  |  |  |   gracefullyCloseAll(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const installedHandlers = new Set<'exit' | 'SIGINT' | 'SIGTERM' | 'SIGHUP'>(); | 
					
						
							|  |  |  | const processHandlers = { | 
					
						
							|  |  |  |   exit: exitHandler, | 
					
						
							|  |  |  |   SIGINT: sigintHandler, | 
					
						
							|  |  |  |   SIGTERM: sigtermHandler, | 
					
						
							|  |  |  |   SIGHUP: sighupHandler, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | function addProcessHandlerIfNeeded(name: 'exit' | 'SIGINT' | 'SIGTERM' | 'SIGHUP') { | 
					
						
							|  |  |  |   if (!installedHandlers.has(name)) { | 
					
						
							|  |  |  |     installedHandlers.add(name); | 
					
						
							|  |  |  |     process.on(name, processHandlers[name]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-01-16 14:41:26 -08:00
										 |  |  | function removeProcessHandlersIfNeeded() { | 
					
						
							|  |  |  |   if (killSet.size) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   for (const handler of installedHandlers) | 
					
						
							|  |  |  |     process.off(handler, processHandlers[handler]); | 
					
						
							|  |  |  |   installedHandlers.clear(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-23 10:38:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  | export async function launchProcess(options: LaunchProcessOptions): Promise<LaunchResult> { | 
					
						
							| 
									
										
										
										
											2020-11-05 14:15:25 -08:00
										 |  |  |   const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe']; | 
					
						
							| 
									
										
										
										
											2021-07-07 20:19:42 +02:00
										 |  |  |   options.log(`<launching> ${options.command} ${options.args ? options.args.join(' ') : ''}`); | 
					
						
							|  |  |  |   const spawnOptions: childProcess.SpawnOptions = { | 
					
						
							|  |  |  |     // On non-windows platforms, `detached: true` makes child process a leader of a new
 | 
					
						
							|  |  |  |     // process group, making it possible to kill child process tree with `.kill(-pid)` command.
 | 
					
						
							|  |  |  |     // @see https://nodejs.org/api/child_process.html#child_process_options_detached
 | 
					
						
							|  |  |  |     detached: process.platform !== 'win32', | 
					
						
							|  |  |  |     env: (options.env as {[key: string]: string}), | 
					
						
							|  |  |  |     cwd: options.cwd, | 
					
						
							|  |  |  |     shell: options.shell, | 
					
						
							|  |  |  |     stdio, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2021-10-01 19:40:47 -07:00
										 |  |  |   const spawnedProcess = childProcess.spawn(options.command, options.args || [], spawnOptions); | 
					
						
							| 
									
										
										
										
											2020-10-27 11:09:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-26 10:49:38 -07:00
										 |  |  |   const cleanup = async () => { | 
					
						
							|  |  |  |     options.log(`[pid=${spawnedProcess.pid || 'N/A'}] starting temporary directories cleanup`); | 
					
						
							|  |  |  |     const errors = await removeFolders(options.tempDirectories); | 
					
						
							|  |  |  |     for (let i = 0; i < options.tempDirectories.length; ++i) { | 
					
						
							|  |  |  |       if (errors[i]) | 
					
						
							|  |  |  |         options.log(`[pid=${spawnedProcess.pid || 'N/A'}] exception while removing ${options.tempDirectories[i]}: ${errors[i]}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     options.log(`[pid=${spawnedProcess.pid || 'N/A'}] finished temporary directories cleanup`); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-27 11:09:41 -07:00
										 |  |  |   // Prevent Unhandled 'error' event.
 | 
					
						
							|  |  |  |   spawnedProcess.on('error', () => {}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   if (!spawnedProcess.pid) { | 
					
						
							| 
									
										
										
										
											2020-05-22 16:06:00 -07:00
										 |  |  |     let failed: (e: Error) => void; | 
					
						
							|  |  |  |     const failedPromise = new Promise<Error>((f, r) => failed = f); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |     spawnedProcess.once('error', error => { | 
					
						
							| 
									
										
										
										
											2021-03-18 10:19:44 +08:00
										 |  |  |       failed(new Error('Failed to launch: ' + error)); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-05-22 16:06:00 -07:00
										 |  |  |     return cleanup().then(() => failedPromise).then(e => Promise.reject(e)); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-08 09:35:28 -08:00
										 |  |  |   options.log(`<launched> pid=${spawnedProcess.pid}`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-01 19:40:47 -07:00
										 |  |  |   const stdout = readline.createInterface({ input: spawnedProcess.stdout! }); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |   stdout.on('line', (data: string) => { | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}][out] ` + data); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-01 19:40:47 -07:00
										 |  |  |   const stderr = readline.createInterface({ input: spawnedProcess.stderr! }); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |   stderr.on('line', (data: string) => { | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}][err] ` + data); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   let processClosed = false; | 
					
						
							| 
									
										
										
										
											2020-05-20 14:58:27 -07:00
										 |  |  |   let fulfillCleanup = () => {}; | 
					
						
							| 
									
										
										
										
											2020-05-27 19:59:03 -07:00
										 |  |  |   const waitForCleanup = new Promise<void>(f => fulfillCleanup = f); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:58:27 -07:00
										 |  |  |   spawnedProcess.once('exit', (exitCode, signal) => { | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}] <process did exit: exitCode=${exitCode}, signal=${signal}>`); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:58:27 -07:00
										 |  |  |     processClosed = true; | 
					
						
							| 
									
										
										
										
											2020-07-17 16:14:23 -07:00
										 |  |  |     gracefullyCloseSet.delete(gracefullyClose); | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |     killSet.delete(killProcessAndCleanup); | 
					
						
							| 
									
										
										
										
											2024-01-16 14:41:26 -08:00
										 |  |  |     removeProcessHandlersIfNeeded(); | 
					
						
							| 
									
										
										
										
											2020-05-27 19:59:03 -07:00
										 |  |  |     options.onExit(exitCode, signal); | 
					
						
							| 
									
										
										
										
											2020-05-22 16:06:00 -07:00
										 |  |  |     // Cleanup as process exits.
 | 
					
						
							|  |  |  |     cleanup().then(fulfillCleanup); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |   addProcessHandlerIfNeeded('exit'); | 
					
						
							|  |  |  |   if (options.handleSIGINT) | 
					
						
							|  |  |  |     addProcessHandlerIfNeeded('SIGINT'); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   if (options.handleSIGTERM) | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |     addProcessHandlerIfNeeded('SIGTERM'); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   if (options.handleSIGHUP) | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |     addProcessHandlerIfNeeded('SIGHUP'); | 
					
						
							| 
									
										
										
										
											2020-07-17 16:14:23 -07:00
										 |  |  |   gracefullyCloseSet.add(gracefullyClose); | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |   killSet.add(killProcessAndCleanup); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |   let gracefullyClosing = false; | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   async function gracefullyClose(): Promise<void> { | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  |     // We keep listeners until we are done, to handle 'exit' and 'SIGINT' while
 | 
					
						
							|  |  |  |     // asynchronously closing to prevent zombie processes. This might introduce
 | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |     // reentrancy to this function, for example user sends SIGINT second time.
 | 
					
						
							|  |  |  |     // In this case, let's forcefully kill the process.
 | 
					
						
							|  |  |  |     if (gracefullyClosing) { | 
					
						
							| 
									
										
										
										
											2023-10-04 22:56:42 -04:00
										 |  |  |       options.log(`[pid=${spawnedProcess.pid}] <forcefully close>`); | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |       killProcess(); | 
					
						
							| 
									
										
										
										
											2022-04-07 19:20:54 -07:00
										 |  |  |       await waitForCleanup;  // Ensure the process is dead and we have cleaned up.
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  |       return; | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-08 13:55:38 -08:00
										 |  |  |     gracefullyClosing = true; | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}] <gracefully close start>`); | 
					
						
							| 
									
										
										
										
											2020-04-02 16:57:12 -07:00
										 |  |  |     await options.attemptToGracefullyClose().catch(() => killProcess()); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:58:27 -07:00
										 |  |  |     await waitForCleanup;  // Ensure the process is dead and we have cleaned up.
 | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}] <gracefully close end>`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |   // This method has to be sync to be used in the 'exit' event handler.
 | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   function killProcess() { | 
					
						
							| 
									
										
										
										
											2023-05-30 13:54:04 -07:00
										 |  |  |     gracefullyCloseSet.delete(gracefullyClose); | 
					
						
							|  |  |  |     killSet.delete(killProcessAndCleanup); | 
					
						
							| 
									
										
										
										
											2024-01-16 14:41:26 -08:00
										 |  |  |     removeProcessHandlersIfNeeded(); | 
					
						
							| 
									
										
										
										
											2021-02-24 19:21:47 -08:00
										 |  |  |     options.log(`[pid=${spawnedProcess.pid}] <kill>`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |     if (spawnedProcess.pid && !spawnedProcess.killed && !processClosed) { | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |       options.log(`[pid=${spawnedProcess.pid}] <will force kill>`); | 
					
						
							| 
									
										
										
										
											2020-01-22 17:42:10 -08:00
										 |  |  |       // Force kill the browser.
 | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |       try { | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |         if (process.platform === 'win32') { | 
					
						
							| 
									
										
										
										
											2022-04-08 09:37:51 -07:00
										 |  |  |           const taskkillProcess = childProcess.spawnSync(`taskkill /pid ${spawnedProcess.pid} /T /F`, { shell: true }); | 
					
						
							|  |  |  |           const [stdout, stderr] = [taskkillProcess.stdout.toString(), taskkillProcess.stderr.toString()]; | 
					
						
							| 
									
										
										
										
											2021-11-11 18:32:22 +01:00
										 |  |  |           if (stdout) | 
					
						
							|  |  |  |             options.log(`[pid=${spawnedProcess.pid}] taskkill stdout: ${stdout}`); | 
					
						
							|  |  |  |           if (stderr) | 
					
						
							|  |  |  |             options.log(`[pid=${spawnedProcess.pid}] taskkill stderr: ${stderr}`); | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |           process.kill(-spawnedProcess.pid, 'SIGKILL'); | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |       } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |         options.log(`[pid=${spawnedProcess.pid}] exception while trying to kill process: ${e}`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |         // the process might have already stopped
 | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-03-31 12:17:16 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       options.log(`[pid=${spawnedProcess.pid}] <skipped force kill spawnedProcess.killed=${spawnedProcess.killed} processClosed=${processClosed}>`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:16 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function killProcessAndCleanup() { | 
					
						
							|  |  |  |     killProcess(); | 
					
						
							|  |  |  |     options.log(`[pid=${spawnedProcess.pid || 'N/A'}] starting temporary directories cleanup`); | 
					
						
							| 
									
										
										
										
											2023-07-24 15:24:29 -07:00
										 |  |  |     for (const dir of options.tempDirectories) { | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2023-10-26 19:41:00 +02:00
										 |  |  |         fs.rmSync(dir, { force: true, recursive: true, maxRetries: 5 }); | 
					
						
							| 
									
										
										
										
											2023-07-24 15:24:29 -07:00
										 |  |  |       } catch (e) { | 
					
						
							|  |  |  |         options.log(`[pid=${spawnedProcess.pid || 'N/A'}] exception while removing ${dir}: ${e}`); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:16 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     options.log(`[pid=${spawnedProcess.pid || 'N/A'}] finished temporary directories cleanup`); | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 09:18:33 -08:00
										 |  |  |   function killAndWait() { | 
					
						
							|  |  |  |     killProcess(); | 
					
						
							|  |  |  |     return waitForCleanup; | 
					
						
							| 
									
										
										
										
											2020-05-27 19:59:03 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { launchedProcess: spawnedProcess, gracefullyClose, kill: killAndWait }; | 
					
						
							| 
									
										
										
										
											2019-12-12 17:55:54 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-07 21:14:16 +02:00
										 |  |  | export function envArrayToObject(env: { name: string, value: string }[]): Env { | 
					
						
							| 
									
										
										
										
											2020-08-18 09:37:40 -07:00
										 |  |  |   const result: Env = {}; | 
					
						
							|  |  |  |   for (const { name, value } of env) | 
					
						
							|  |  |  |     result[name] = value; | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } |