From 27f20c9faeeac489bc52be30d4280b8121bbfdf7 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 22 Feb 2023 11:40:48 -0800 Subject: [PATCH] Defer creation of node typings installer (#52904) --- src/typingsInstaller/nodeTypingsInstaller.ts | 83 ++++++++++---------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 146d392d255..91be7a4b8c6 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -158,49 +158,47 @@ export class NodeTypingsInstaller extends TypingsInstaller { this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(globalTypingsCacheLocation), this.installTypingHost, this.log); } - listen() { - process.on("message", (req: TypingInstallerRequestUnion) => { - if (this.delayedInitializationError) { - // report initializationFailed error - this.sendResponse(this.delayedInitializationError); - this.delayedInitializationError = undefined; + handleRequest(req: TypingInstallerRequestUnion) { + if (this.delayedInitializationError) { + // report initializationFailed error + this.sendResponse(this.delayedInitializationError); + this.delayedInitializationError = undefined; + } + switch (req.kind) { + case "discover": + this.install(req); + break; + case "closeProject": + this.closeProject(req); + break; + case "typesRegistry": { + const typesRegistry: { [key: string]: MapLike } = {}; + this.typesRegistry.forEach((value, key) => { + typesRegistry[key] = value; + }); + const response: TypesRegistryResponse = { kind: EventTypesRegistry, typesRegistry }; + this.sendResponse(response); + break; } - switch (req.kind) { - case "discover": - this.install(req); - break; - case "closeProject": - this.closeProject(req); - break; - case "typesRegistry": { - const typesRegistry: { [key: string]: MapLike } = {}; - this.typesRegistry.forEach((value, key) => { - typesRegistry[key] = value; - }); - const response: TypesRegistryResponse = { kind: EventTypesRegistry, typesRegistry }; - this.sendResponse(response); - break; - } - case "installPackage": { - const { fileName, packageName, projectName, projectRootPath } = req; - const cwd = getDirectoryOfPackageJson(fileName, this.installTypingHost) || projectRootPath; - if (cwd) { - this.installWorker(-1, [packageName], cwd, success => { - const message = success ? `Package ${packageName} installed.` : `There was an error installing ${packageName}.`; - const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success, message }; - this.sendResponse(response); - }); - } - else { - const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success: false, message: "Could not determine a project root path." }; + case "installPackage": { + const { fileName, packageName, projectName, projectRootPath } = req; + const cwd = getDirectoryOfPackageJson(fileName, this.installTypingHost) || projectRootPath; + if (cwd) { + this.installWorker(-1, [packageName], cwd, success => { + const message = success ? `Package ${packageName} installed.` : `There was an error installing ${packageName}.`; + const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success, message }; this.sendResponse(response); - } - break; + }); } - default: - Debug.assertNever(req); + else { + const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success: false, message: "Could not determine a project root path." }; + this.sendResponse(response); + } + break; } - }); + default: + Debug.assertNever(req); + } } protected sendResponse(response: TypingInstallerResponseUnion) { @@ -272,8 +270,11 @@ process.on("disconnect", () => { } process.exit(0); }); -const installer = new NodeTypingsInstaller(globalTypingsCacheLocation!, typingSafeListLocation!, typesMapLocation!, npmLocation, validateDefaultNpmLocation, /*throttleLimit*/5, log); // TODO: GH#18217 -installer.listen(); +let installer: NodeTypingsInstaller | undefined; +process.on("message", (req: TypingInstallerRequestUnion) => { + installer ??= new NodeTypingsInstaller(globalTypingsCacheLocation!, typingSafeListLocation!, typesMapLocation!, npmLocation, validateDefaultNpmLocation, /*throttleLimit*/5, log); // TODO: GH#18217 + installer.handleRequest(req); +}); function indent(newline: string, str: string | undefined): string { return str && str.length