From 798232c9752dba67380bd315c79eea0686651741 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Thu, 1 Sep 2016 18:28:37 -0700 Subject: [PATCH] Fixing unit tests, lint errors & addressing CR feedback --- src/harness/unittests/typingsInstaller.ts | 19 ++-- .../typingsInstaller/nodeTypingsInstaller.ts | 90 +++++++++---------- .../typingsInstaller/typingsInstaller.ts | 4 +- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/harness/unittests/typingsInstaller.ts b/src/harness/unittests/typingsInstaller.ts index 408d11a720..a8b4039e00 100644 --- a/src/harness/unittests/typingsInstaller.ts +++ b/src/harness/unittests/typingsInstaller.ts @@ -4,7 +4,8 @@ namespace ts.projectSystem { describe("typings installer", () => { - it("configured projects (tsd installed) 1", () => { + it("configured projects (typings installed) 1", () => { + debugger; const file1 = { path: "/a/b/app.js", content: "" @@ -31,7 +32,7 @@ namespace ts.projectSystem { }; const jquery = { - path: "/a/data/typings/jquery/jquery.d.ts", + path: "/a/data/node_modules/@types/jquery/index.d.ts", content: "declare const $: { x: number }" }; @@ -44,18 +45,18 @@ namespace ts.projectSystem { const p = projectService.configuredProjects[0]; checkProjectActualFiles(p, [file1.path]); - assert(host.fileExists(combinePaths(installer.globalTypingsCacheLocation, "tsd.json"))); + assert(host.fileExists(combinePaths(installer.globalTypingsCacheLocation, "package.json"))); installer.runPostInstallActions(t => { assert.deepEqual(t, ["jquery"]); host.createFileOrFolder(jquery, /*createParentDirectory*/ true); - return ["jquery/jquery.d.ts"]; + return ["@types/jquery"]; }); checkNumberOfProjects(projectService, { configuredProjects: 1 }); checkProjectActualFiles(p, [file1.path, jquery.path]); }); - it("inferred project (tsd installed)", () => { + it("inferred project (typings installed)", () => { const file1 = { path: "/a/b/app.js", content: "" @@ -71,7 +72,7 @@ namespace ts.projectSystem { }; const jquery = { - path: "/a/data/typings/jquery/jquery.d.ts", + path: "/a/data/node_modules/@types/jquery/index.d.ts", content: "declare const $: { x: number }" }; const host = createServerHost([file1, packageJson]); @@ -84,12 +85,12 @@ namespace ts.projectSystem { const p = projectService.inferredProjects[0]; checkProjectActualFiles(p, [file1.path]); - assert(host.fileExists(combinePaths(installer.globalTypingsCacheLocation, "tsd.json"))); + assert(host.fileExists(combinePaths(installer.globalTypingsCacheLocation, "package.json"))); installer.runPostInstallActions(t => { assert.deepEqual(t, ["jquery"]); host.createFileOrFolder(jquery, /*createParentDirectory*/ true); - return ["jquery/jquery.d.ts"]; + return ["@types/jquery"]; }); checkNumberOfProjects(projectService, { inferredProjects: 1 }); checkProjectActualFiles(p, [file1.path, jquery.path]); @@ -166,7 +167,7 @@ namespace ts.projectSystem { enqueueIsCalled = true; super.enqueueInstallTypingsRequest(project, typingOptions); } - runTsd(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void { + runInstall(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void { assert.deepEqual(typingsToInstall, ["node"]); runInstallIsCalled = true; super.runInstall(cachePath, typingsToInstall, postInstallAction); diff --git a/src/server/typingsInstaller/nodeTypingsInstaller.ts b/src/server/typingsInstaller/nodeTypingsInstaller.ts index d9dddfabc9..0d22b1b364 100644 --- a/src/server/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/server/typingsInstaller/nodeTypingsInstaller.ts @@ -93,58 +93,58 @@ namespace ts.server.typingsInstaller { const id = this.installRunCount; this.installRunCount++; let execInstallCmdCount = 0; - let filteredTypings: string[] = []; + const filteredTypings: string[] = []; for (const typing of typingsToInstall) { const command = `npm view @types/${typing} --silent name`; - if (this.log.isEnabled()) { - this.log.writeLine(`Running npm view @types ${id}, command '${command}'.`); - } - this.exec(command, { cwd: cachePath }, (err, stdout, stderr) => { - execInstallCmdCount++; - if (this.log.isEnabled()) { - this.log.writeLine(`npm install @types ${id} stdout: ${stdout}`); - this.log.writeLine(`npm install @types ${id} stderr: ${stderr}`); - } - if (stdout !== "") { + this.execAsync("npm view", command, cachePath, id, (err, stdout, stderr) => { + if (stdout) { filteredTypings.push(typing); } - if (execInstallCmdCount >= typingsToInstall.length) { - const command = `npm install ${filteredTypings.map(t => "@types/" + t).join(" ")} --save-dev`; - if (this.log.isEnabled()) { - this.log.writeLine(`Running npm install @types ${id}, command '${command}'. cache path '${cachePath}'`); - } - this.exec(command, { cwd: cachePath }, (err, stdout, stderr) => { - if (this.log.isEnabled()) { - this.log.writeLine(`npm install @types ${id} stdout: ${stdout}`); - this.log.writeLine(`npm install @types ${id} stderr: ${stderr}`); - } - if (stdout === "") { - return; - } - const command = "npm ls -json"; - this.exec(command, { cwd: cachePath }, (err, stdout, stderr) => { - if (this.log.isEnabled()) { - this.log.writeLine(`npm ls -json ${id} stdout: ${stdout}`); - this.log.writeLine(`npm ls -json ${id} stderr: ${stderr}`); - } - const installedTypings: string[] = []; - try { - const response = JSON.parse(stdout); - if (response.dependencies) { - for (const typing in response.dependencies) { - installedTypings.push(typing); - } - } - } - catch (e) { - this.log.writeLine(`Error parsing installed @types dependencies. Error details: ${e.message}`); - } - postInstallAction(installedTypings); - }); - }); + execInstallCmdCount++; + if (execInstallCmdCount === typingsToInstall.length) { + installFilteredTypings(this, filteredTypings); } }); } + + function installFilteredTypings(self: NodeTypingsInstaller, filteredTypings: string[]) { + const command = `npm install ${filteredTypings.map(t => "@types/" + t).join(" ")} --save-dev`; + self.execAsync("npm install", command, cachePath, id, (err, stdout, stderr) => { + if (stdout) { + reportInstalledTypings(self); + } + }); + } + + function reportInstalledTypings(self: NodeTypingsInstaller) { + const command = "npm ls -json"; + self.execAsync("npm ls", command, cachePath, id, (err, stdout, stderr) => { + let installedTypings: string[]; + try { + const response = JSON.parse(stdout); + if (response.dependencies) { + installedTypings = getOwnKeys(response.dependencies); + } + } + catch (e) { + self.log.writeLine(`Error parsing installed @types dependencies. Error details: ${e.message}`); + } + postInstallAction(installedTypings || []); + }); + } + } + + private execAsync(prefix: string, command: string, cwd: string, requestId: number, cb: (err: Error, stdout: string, stderr: string) => void) { + if (this.log.isEnabled()) { + this.log.writeLine(`#${requestId} running command '${command}'.`); + } + this.exec(command, { cwd }, (err, stdout, stderr) => { + if (this.log.isEnabled()) { + this.log.writeLine(`${prefix} #${requestId} stdout: ${stdout}`); + this.log.writeLine(`${prefix} #${requestId} stderr: ${stderr}`); + } + cb(err, stdout, stderr); + }); } } diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index 2905fb9aff..c96fe3c6c4 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -135,7 +135,7 @@ namespace ts.server.typingsInstaller { if (!packageName) { continue; } - var typingFile = typingToFileName(cacheLocation, packageName, this.installTypingHost); + const typingFile = typingToFileName(cacheLocation, packageName, this.installTypingHost); if (!typingFile) { continue; } @@ -198,7 +198,7 @@ namespace ts.server.typingsInstaller { continue; } installedPackages[packageName] = true; - var typingFile = typingToFileName(cachePath, packageName, this.installTypingHost); + const typingFile = typingToFileName(cachePath, packageName, this.installTypingHost); if (!typingFile) { continue; }