Skip to content

Commit 51e2b02

Browse files
committed
add runTsd method
1 parent d8d117f commit 51e2b02

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace ts.server.typingsInstaller {
44
export class NodeTypingsInstaller extends TypingsInstaller {
55
private execSync: { (command: string, options: { stdio: "ignore" }): any };
6+
private exec: { (command: string, options: {}, callback?: (error: Error, stdout: string, stderr: string) => void): any };
67
constructor() {
78
super();
89
this.execSync = require("child_process").execSync;
10+
this.exec = require("child_process").exec;
911
}
1012

1113
init() {
@@ -35,13 +37,31 @@ namespace ts.server.typingsInstaller {
3537
}
3638
}
3739

38-
protected getTypingResolutionHost() {
40+
protected getInstallTypingHost() {
3941
return sys;
4042
}
4143

4244
protected sendResponse(response: InstallTypingsResponse) {
4345
process.send(response);
4446
}
47+
48+
protected runTsd(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void {
49+
this.exec(`tsd install ${typingsToInstall.join(" ")} -ros`, {}, (err, stdout, stderr) => {
50+
const i = stdout.indexOf("running install");
51+
if (i < 0) {
52+
return;
53+
}
54+
const installedTypings: string[] = [];
55+
56+
const expr = /^\s*-\s*(\S+)\s*$/gm;
57+
expr.lastIndex = i;
58+
let match: RegExpExecArray;
59+
while (match = expr.exec(stdout)) {
60+
installedTypings.push(match[1]);
61+
}
62+
postInstallAction(installedTypings);
63+
})
64+
}
4565
}
4666

4767
const installer = new NodeTypingsInstaller();

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/// <reference path="../../services/services.ts"/>
22
/// <reference path="../utilities.ts"/>
33

4-
5-
64
namespace ts.server.typingsInstaller {
75

86
const DefaultTsdSettings = JSON.stringify({
@@ -40,18 +38,30 @@ namespace ts.server.typingsInstaller {
4038

4139
// respond with whatever cached typings we have now
4240
this.sendResponse(this.createResponse(req, discoverTypingsResult.cachedTypingPaths));
41+
// start watching files
4342
this.watchFiles(discoverTypingsResult.filesToWatch);
44-
this.installTypings(req, discoverTypingsResult.newTypingNames);
43+
// install typings and
44+
this.installTypings(req, discoverTypingsResult.cachedTypingPaths, discoverTypingsResult.newTypingNames);
4545
}
4646

47-
private installTypings(req: InstallTypingsRequest, typingsToInstall: string[]) {
47+
private installTypings(req: InstallTypingsRequest, currentlyCachedTypings: string[], typingsToInstall: string[]) {
48+
typingsToInstall = filter(typingsToInstall, x => !hasProperty(this.missingTypings, x));
49+
if (typingsToInstall.length === 0) {
50+
return;
51+
}
52+
4853
// TODO: install typings and send response when they are ready
49-
const existingTypings = typingsToInstall.fi
5054
const host = this.getInstallTypingHost();
5155
const tsdPath = combinePaths(req.cachePath, "tsd.json");
5256
if (!host.fileExists(tsdPath)) {
5357
host.writeFile(tsdPath, DefaultTsdSettings);
5458
}
59+
60+
this.runTsd(tsdPath, typingsToInstall, installedTypings => {
61+
// TODO: record new missing package names
62+
// TODO: watch project directory
63+
this.sendResponse(this.createResponse(req, currentlyCachedTypings.concat(installedTypings)));
64+
});
5565
}
5666

5767
private watchFiles(files: string[]) {
@@ -71,5 +81,6 @@ namespace ts.server.typingsInstaller {
7181
protected abstract installPackage(packageName: string): boolean;
7282
protected abstract getInstallTypingHost(): InstallTypingHost;
7383
protected abstract sendResponse(response: InstallTypingsResponse): void;
84+
protected abstract runTsd(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void;
7485
}
7586
}

0 commit comments

Comments
 (0)