diff --git a/src/client/pythonEnvironments/common/externalDependencies.ts b/src/client/pythonEnvironments/common/externalDependencies.ts index 6eb525b92e31..357967236c9e 100644 --- a/src/client/pythonEnvironments/common/externalDependencies.ts +++ b/src/client/pythonEnvironments/common/externalDependencies.ts @@ -3,6 +3,7 @@ import * as fsapi from 'fs-extra'; import * as path from 'path'; +import { Worker } from 'worker_threads'; import * as vscode from 'vscode'; import { IWorkspaceService } from '../../common/application/types'; import { ExecutionResult, IProcessServiceFactory, ShellOptions, SpawnOptions } from '../../common/process/types'; @@ -200,3 +201,25 @@ export function onDidChangePythonSetting(name: string, callback: () => void, roo } }); } + +// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types +export async function executeWorkerFile(workerFileName: string, workerData: any): Promise { + return new Promise((resolve, reject) => { + const worker = new Worker(workerFileName, { workerData }); + worker.on('message', (res: { err: Error; res: unknown }) => { + if (res.err) { + reject(res.err); + } + resolve(res.res); + }); + worker.on('error', (ex: Error) => { + traceError(`Error in worker ${workerFileName}`, ex); + reject(ex); + }); + worker.on('exit', (code) => { + if (code !== 0) { + reject(new Error(`Worker ${workerFileName} stopped with exit code ${code}`)); + } + }); + }); +} diff --git a/src/client/pythonEnvironments/common/windowsRegistry.ts b/src/client/pythonEnvironments/common/windowsRegistry.ts index 0fc5f078192a..efac5bb3209f 100644 --- a/src/client/pythonEnvironments/common/windowsRegistry.ts +++ b/src/client/pythonEnvironments/common/windowsRegistry.ts @@ -4,9 +4,8 @@ import { HKCU, HKLM, Options, REG_SZ, Registry, RegistryItem } from 'winreg'; import * as path from 'path'; -import { Worker } from 'worker_threads'; import { createDeferred } from '../../common/utils/async'; -import { traceError } from '../../logging'; +import { executeWorkerFile } from './externalDependencies'; export { HKCU, HKLM, REG_SZ, Options }; @@ -40,7 +39,7 @@ export async function readRegistryValues(options: Options, useWorkerThreads: boo }); return deferred.promise; } - return executeWorkerFile('registryValuesWorker.js', options); + return executeWorkerFile(path.join(__dirname, 'registryValuesWorker.js'), options); } export async function readRegistryKeys(options: Options, useWorkerThreads: boolean): Promise { @@ -57,26 +56,5 @@ export async function readRegistryKeys(options: Options, useWorkerThreads: boole }); return deferred.promise; } - return executeWorkerFile('registryKeysWorker.js', options); -} - -async function executeWorkerFile(workerFileName: string, workerData: any): Promise { - return new Promise((resolve, reject) => { - const worker = new Worker(path.join(__dirname, workerFileName), { workerData }); - worker.on('message', (res: { err: Error; res: unknown }) => { - if (res.err) { - reject(res.err); - } - resolve(res.res); - }); - worker.on('error', (ex: Error) => { - traceError(`Error in worker ${workerFileName}`, ex); - reject(ex); - }); - worker.on('exit', (code) => { - if (code !== 0) { - reject(new Error(`Worker ${workerFileName} stopped with exit code ${code}`)); - } - }); - }); + return executeWorkerFile(path.join(__dirname, 'registryKeysWorker.js'), options); }