Skip to content

Commit 27f20c9

Browse files
authored
Defer creation of node typings installer (#52904)
1 parent 3267c76 commit 27f20c9

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/typingsInstaller/nodeTypingsInstaller.ts

+41-40
Original file line numberDiff line numberDiff line change
@@ -158,49 +158,47 @@ export class NodeTypingsInstaller extends TypingsInstaller {
158158
this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(globalTypingsCacheLocation), this.installTypingHost, this.log);
159159
}
160160

161-
listen() {
162-
process.on("message", (req: TypingInstallerRequestUnion) => {
163-
if (this.delayedInitializationError) {
164-
// report initializationFailed error
165-
this.sendResponse(this.delayedInitializationError);
166-
this.delayedInitializationError = undefined;
161+
handleRequest(req: TypingInstallerRequestUnion) {
162+
if (this.delayedInitializationError) {
163+
// report initializationFailed error
164+
this.sendResponse(this.delayedInitializationError);
165+
this.delayedInitializationError = undefined;
166+
}
167+
switch (req.kind) {
168+
case "discover":
169+
this.install(req);
170+
break;
171+
case "closeProject":
172+
this.closeProject(req);
173+
break;
174+
case "typesRegistry": {
175+
const typesRegistry: { [key: string]: MapLike<string> } = {};
176+
this.typesRegistry.forEach((value, key) => {
177+
typesRegistry[key] = value;
178+
});
179+
const response: TypesRegistryResponse = { kind: EventTypesRegistry, typesRegistry };
180+
this.sendResponse(response);
181+
break;
167182
}
168-
switch (req.kind) {
169-
case "discover":
170-
this.install(req);
171-
break;
172-
case "closeProject":
173-
this.closeProject(req);
174-
break;
175-
case "typesRegistry": {
176-
const typesRegistry: { [key: string]: MapLike<string> } = {};
177-
this.typesRegistry.forEach((value, key) => {
178-
typesRegistry[key] = value;
183+
case "installPackage": {
184+
const { fileName, packageName, projectName, projectRootPath } = req;
185+
const cwd = getDirectoryOfPackageJson(fileName, this.installTypingHost) || projectRootPath;
186+
if (cwd) {
187+
this.installWorker(-1, [packageName], cwd, success => {
188+
const message = success ? `Package ${packageName} installed.` : `There was an error installing ${packageName}.`;
189+
const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success, message };
190+
this.sendResponse(response);
179191
});
180-
const response: TypesRegistryResponse = { kind: EventTypesRegistry, typesRegistry };
181-
this.sendResponse(response);
182-
break;
183192
}
184-
case "installPackage": {
185-
const { fileName, packageName, projectName, projectRootPath } = req;
186-
const cwd = getDirectoryOfPackageJson(fileName, this.installTypingHost) || projectRootPath;
187-
if (cwd) {
188-
this.installWorker(-1, [packageName], cwd, success => {
189-
const message = success ? `Package ${packageName} installed.` : `There was an error installing ${packageName}.`;
190-
const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success, message };
191-
this.sendResponse(response);
192-
});
193-
}
194-
else {
195-
const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success: false, message: "Could not determine a project root path." };
196-
this.sendResponse(response);
197-
}
198-
break;
193+
else {
194+
const response: PackageInstalledResponse = { kind: ActionPackageInstalled, projectName, success: false, message: "Could not determine a project root path." };
195+
this.sendResponse(response);
199196
}
200-
default:
201-
Debug.assertNever(req);
197+
break;
202198
}
203-
});
199+
default:
200+
Debug.assertNever(req);
201+
}
204202
}
205203

206204
protected sendResponse(response: TypingInstallerResponseUnion) {
@@ -272,8 +270,11 @@ process.on("disconnect", () => {
272270
}
273271
process.exit(0);
274272
});
275-
const installer = new NodeTypingsInstaller(globalTypingsCacheLocation!, typingSafeListLocation!, typesMapLocation!, npmLocation, validateDefaultNpmLocation, /*throttleLimit*/5, log); // TODO: GH#18217
276-
installer.listen();
273+
let installer: NodeTypingsInstaller | undefined;
274+
process.on("message", (req: TypingInstallerRequestUnion) => {
275+
installer ??= new NodeTypingsInstaller(globalTypingsCacheLocation!, typingSafeListLocation!, typesMapLocation!, npmLocation, validateDefaultNpmLocation, /*throttleLimit*/5, log); // TODO: GH#18217
276+
installer.handleRequest(req);
277+
});
277278

278279
function indent(newline: string, str: string | undefined): string {
279280
return str && str.length

0 commit comments

Comments
 (0)