add runTsd method

This commit is contained in:
Vladimir Matveev 2016-08-12 12:14:25 -07:00
parent d8d117ffaf
commit 51e2b02a31
2 changed files with 37 additions and 6 deletions

View file

@ -3,9 +3,11 @@
namespace ts.server.typingsInstaller { namespace ts.server.typingsInstaller {
export class NodeTypingsInstaller extends TypingsInstaller { export class NodeTypingsInstaller extends TypingsInstaller {
private execSync: { (command: string, options: { stdio: "ignore" }): any }; private execSync: { (command: string, options: { stdio: "ignore" }): any };
private exec: { (command: string, options: {}, callback?: (error: Error, stdout: string, stderr: string) => void): any };
constructor() { constructor() {
super(); super();
this.execSync = require("child_process").execSync; this.execSync = require("child_process").execSync;
this.exec = require("child_process").exec;
} }
init() { init() {
@ -35,13 +37,31 @@ namespace ts.server.typingsInstaller {
} }
} }
protected getTypingResolutionHost() { protected getInstallTypingHost() {
return sys; return sys;
} }
protected sendResponse(response: InstallTypingsResponse) { protected sendResponse(response: InstallTypingsResponse) {
process.send(response); process.send(response);
} }
protected runTsd(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void {
this.exec(`tsd install ${typingsToInstall.join(" ")} -ros`, {}, (err, stdout, stderr) => {
const i = stdout.indexOf("running install");
if (i < 0) {
return;
}
const installedTypings: string[] = [];
const expr = /^\s*-\s*(\S+)\s*$/gm;
expr.lastIndex = i;
let match: RegExpExecArray;
while (match = expr.exec(stdout)) {
installedTypings.push(match[1]);
}
postInstallAction(installedTypings);
})
}
} }
const installer = new NodeTypingsInstaller(); const installer = new NodeTypingsInstaller();

View file

@ -1,8 +1,6 @@
/// <reference path="../../services/services.ts"/> /// <reference path="../../services/services.ts"/>
/// <reference path="../utilities.ts"/> /// <reference path="../utilities.ts"/>
namespace ts.server.typingsInstaller { namespace ts.server.typingsInstaller {
const DefaultTsdSettings = JSON.stringify({ const DefaultTsdSettings = JSON.stringify({
@ -40,18 +38,30 @@ namespace ts.server.typingsInstaller {
// respond with whatever cached typings we have now // respond with whatever cached typings we have now
this.sendResponse(this.createResponse(req, discoverTypingsResult.cachedTypingPaths)); this.sendResponse(this.createResponse(req, discoverTypingsResult.cachedTypingPaths));
// start watching files
this.watchFiles(discoverTypingsResult.filesToWatch); this.watchFiles(discoverTypingsResult.filesToWatch);
this.installTypings(req, discoverTypingsResult.newTypingNames); // install typings and
this.installTypings(req, discoverTypingsResult.cachedTypingPaths, discoverTypingsResult.newTypingNames);
} }
private installTypings(req: InstallTypingsRequest, typingsToInstall: string[]) { private installTypings(req: InstallTypingsRequest, currentlyCachedTypings: string[], typingsToInstall: string[]) {
typingsToInstall = filter(typingsToInstall, x => !hasProperty(this.missingTypings, x));
if (typingsToInstall.length === 0) {
return;
}
// TODO: install typings and send response when they are ready // TODO: install typings and send response when they are ready
const existingTypings = typingsToInstall.fi
const host = this.getInstallTypingHost(); const host = this.getInstallTypingHost();
const tsdPath = combinePaths(req.cachePath, "tsd.json"); const tsdPath = combinePaths(req.cachePath, "tsd.json");
if (!host.fileExists(tsdPath)) { if (!host.fileExists(tsdPath)) {
host.writeFile(tsdPath, DefaultTsdSettings); host.writeFile(tsdPath, DefaultTsdSettings);
} }
this.runTsd(tsdPath, typingsToInstall, installedTypings => {
// TODO: record new missing package names
// TODO: watch project directory
this.sendResponse(this.createResponse(req, currentlyCachedTypings.concat(installedTypings)));
});
} }
private watchFiles(files: string[]) { private watchFiles(files: string[]) {
@ -71,5 +81,6 @@ namespace ts.server.typingsInstaller {
protected abstract installPackage(packageName: string): boolean; protected abstract installPackage(packageName: string): boolean;
protected abstract getInstallTypingHost(): InstallTypingHost; protected abstract getInstallTypingHost(): InstallTypingHost;
protected abstract sendResponse(response: InstallTypingsResponse): void; protected abstract sendResponse(response: InstallTypingsResponse): void;
protected abstract runTsd(cachePath: string, typingsToInstall: string[], postInstallAction: (installedTypings: string[]) => void): void;
} }
} }