diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 146d392d2553f..7711ce19dc82f 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -106,7 +106,7 @@ type ExecSync = (command: string, options: ExecSyncOptions) => string; export class NodeTypingsInstaller extends TypingsInstaller { private readonly nodeExecSync: ExecSync; private readonly npmPath: string; - readonly typesRegistry: Map>; + typesRegistry: Map> = undefined!; private delayedInitializationError: InitializationFailedResponse | undefined; @@ -131,31 +131,36 @@ export class NodeTypingsInstaller extends TypingsInstaller { this.log.writeLine(`validateDefaultNpmLocation: ${validateDefaultNpmLocation}`); } ({ execSync: this.nodeExecSync } = require("child_process")); + } - this.ensurePackageDirectoryExists(globalTypingsCacheLocation); + override ensureInitialized(): void { + if (!this.initDone) { + super.ensureInitialized(); - try { - if (this.log.isEnabled()) { - this.log.writeLine(`Updating ${typesRegistryPackageName} npm package...`); - } - this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}@${this.latestDistTag}`, { cwd: globalTypingsCacheLocation }); - if (this.log.isEnabled()) { - this.log.writeLine(`Updated ${typesRegistryPackageName} npm package`); + this.ensurePackageDirectoryExists(this.globalCachePath); + try { + if (this.log.isEnabled()) { + this.log.writeLine(`Updating ${typesRegistryPackageName} npm package...`); + } + this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}@${this.latestDistTag}`, { cwd: this.globalCachePath }); + if (this.log.isEnabled()) { + this.log.writeLine(`Updated ${typesRegistryPackageName} npm package`); + } } - } - catch (e) { - if (this.log.isEnabled()) { - this.log.writeLine(`Error updating ${typesRegistryPackageName} package: ${(e as Error).message}`); + catch (e) { + if (this.log.isEnabled()) { + this.log.writeLine(`Error updating ${typesRegistryPackageName} package: ${(e as Error).message}`); + } + // store error info to report it later when it is known that server is already listening to events from typings installer + this.delayedInitializationError = { + kind: "event::initializationFailed", + message: (e as Error).message, + stack: (e as Error).stack, + }; } - // store error info to report it later when it is known that server is already listening to events from typings installer - this.delayedInitializationError = { - kind: "event::initializationFailed", - message: (e as Error).message, - stack: (e as Error).stack, - }; - } - this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(globalTypingsCacheLocation), this.installTypingHost, this.log); + this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(this.globalCachePath), this.installTypingHost, this.log); + } } listen() { @@ -173,6 +178,7 @@ export class NodeTypingsInstaller extends TypingsInstaller { this.closeProject(req); break; case "typesRegistry": { + this.ensureInitialized(); const typesRegistry: { [key: string]: MapLike } = {}; this.typesRegistry.forEach((value, key) => { typesRegistry[key] = value; diff --git a/src/typingsInstallerCore/typingsInstaller.ts b/src/typingsInstallerCore/typingsInstaller.ts index b57d585e2f9a9..59561e03398e5 100644 --- a/src/typingsInstallerCore/typingsInstaller.ts +++ b/src/typingsInstallerCore/typingsInstaller.ts @@ -156,10 +156,11 @@ export abstract class TypingsInstaller { abstract readonly typesRegistry: Map>; /** @internal */ private readonly watchFactory: WatchFactory; + protected initDone?: true; constructor( protected readonly installTypingHost: InstallTypingHost, - private readonly globalCachePath: string, + protected readonly globalCachePath: string, private readonly safeListPath: Path, private readonly typesMapLocation: Path, private readonly throttleLimit: number, @@ -171,7 +172,13 @@ export abstract class TypingsInstaller { this.log.writeLine(`Global cache location '${globalCachePath}', safe file path '${safeListPath}', types map path ${typesMapLocation}`); } this.watchFactory = getWatchFactory(this.installTypingHost as WatchFactoryHost, isLoggingEnabled ? WatchLogLevel.Verbose : WatchLogLevel.None, s => this.log.writeLine(s), getDetailWatchInfo); - this.processCacheLocation(this.globalCachePath); + } + + ensureInitialized() { + if (!this.initDone) { + this.processCacheLocation(this.globalCachePath); + this.initDone = true; + } } closeProject(req: CloseProject) { @@ -202,6 +209,7 @@ export abstract class TypingsInstaller { this.log.writeLine(`Got install request ${JSON.stringify(req)}`); } + this.ensureInitialized(); // load existing typing information from the cache if (req.cachePath) { if (this.log.isEnabled()) {