Skip to content

Commit

Permalink
Merge pull request #1143 from mjbvz/typings-wrapper
Browse files Browse the repository at this point in the history
Create Wrapper Script for Installing Typings
  • Loading branch information
mousetraps committed Jul 20, 2016
2 parents 10c28f3 + bd591e5 commit e148ccc
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
13 changes: 13 additions & 0 deletions Nodejs/Product/Nodejs/Nodejs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,18 @@
</ConditionalEmbeddedResource>
</ItemGroup>
<ItemGroup>
<Content Include="TypingsAcquisitionTool\index.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="TypingsAcquisitionTool\package.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="TypingsAcquisitionTool\bin\install_typings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<ZipProject Include="ProjectTemplates\CloudService\CloudService.ccproj" />
<ZipProject Include="ProjectTemplates\CloudService\ServiceConfiguration.Cloud.cscfg" />
<ZipProject Include="ProjectTemplates\CloudService\ServiceConfiguration.Local.cscfg" />
Expand Down Expand Up @@ -1699,6 +1711,7 @@
<ZipProject Include="ProjectTemplates\TypeScriptAzureNodejsWorkerRole\startup.ts" />
<ZipProject Include="ProjectTemplates\TypeScriptAzureWebRole\server.ts" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<!--
To specify a different registry root to register your package, uncomment the TargetRegistryRoot
Expand Down
34 changes: 27 additions & 7 deletions Nodejs/Product/Nodejs/TypingsAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@

namespace Microsoft.NodejsTools {
internal class TypingsAcquisition {
private const string TypingsTool = "typings";
private const string TypingsToolVersion = "1.0.5"; // Lock to stable version of the 'typings' tool with known behavior.
private const string TypingsTool = "ntvs_install_typings";
private const string TypingsToolExe = TypingsTool + ".cmd";

private const string TypingsDirectoryName = "typings";

private static SemaphoreSlim typingsToolGlobalWorkSemaphore = new SemaphoreSlim(1);

/// <summary>
/// Full path to the local copy of the typings acquision tool.
/// </summary>
private static string LocalTypingsToolPath {
get {
string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var root = Path.GetDirectoryName(path);

return Path.Combine(
root,
"TypingsAquisitionTool");
}
}

/// <summary>
/// Full path to the typings acquisition tool.
/// </summary>
Expand Down Expand Up @@ -78,7 +94,7 @@ internal class TypingsAcquisition {
if (!File.Exists(Path.Combine(_pathToRootProjectDirectory, "typings.json"))) {
return true;
}
return await ExecuteTypingsTool(new[] { "install" }, redirector);
return await ExecuteTypingsTool(new string[] { }, redirector);
}

private async Task<bool> DownloadTypingsForPackages(IEnumerable<string> packages, Redirector redirector) {
Expand Down Expand Up @@ -156,16 +172,20 @@ internal class TypingsAcquisition {

// install typings
using (var commander = _npmController.CreateNpmCommander()) {
return await commander.InstallPackageToFolderByVersionAsync(NodejsConstants.ExternalToolsPath, TypingsTool, TypingsToolVersion, false);
return await commander.InstallPackageToFolderByVersionAsync(
NodejsConstants.ExternalToolsPath,
string.Format(@"""{0}""", LocalTypingsToolPath),
string.Empty,
false);
}
}

private static IEnumerable<string> GetTypingsToolInstallArguments(IEnumerable<string> packages) {
var arguments = new[] { "install" }.Concat(packages.Select(name => string.Format("dt~{0}", name)));
var arguments = packages;
if (NodejsPackage.Instance.IntellisenseOptionsPage.SaveChangesToConfigFile) {
arguments = arguments.Concat(new[] { "--save" });
return arguments.Concat(new[] { "--save" });
}
return arguments.Concat(new[] { "--global" });
return arguments;
}

private static IEnumerable<string> CurrentTypingsPackages(string pathToRootProjectDirectory) {
Expand Down
37 changes: 37 additions & 0 deletions Nodejs/Product/Nodejs/TypingsAcquisitionTool/bin/install_typings
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node
/**
Tool to aquire typings used for NTVS IntelliSense.
This is a simple wrapper around `typings-core`, with the main
difference being that missing packages do not stop other typings
from being aquired.
*/
"use strict";
var events = require('events');
var minimist = require('minimist');
var typingsTool = require('../index');

var argv = minimist(process.argv.slice(2), {
boolean: ['save', 'verbose'],
string: ['cwd']
});

var emitter = new events.EventEmitter();

var options = {
save: argv.save,
global: true,
emitter: emitter,
cwd: argv.cwd || process.cwd()
};

var packagesToInstall = argv._;

if (!packagesToInstall.length) {
// top level package install
typingsTool.installTypingsForProject(options)
} else {
typingsTool.runAll(packagesToInstall.map(function (name) {
return typingsTool.installTypingsForPackage(name, options);
}));
}
38 changes: 38 additions & 0 deletions Nodejs/Product/Nodejs/TypingsAcquisitionTool/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var typings = require('typings-core');

/**
* Create a promise that runs one or more promises sequentially.
*/
module.exports.runAll = function (promises) {
return promises.reduce(function (p1, p2) {
return p1.then(function () {
return p2;
});
});
};

/**
* Installs the typings for `packageName` with `options`.
*/
module.exports.installTypingsForPackage = function (packageName, options) {
return typings.installDependenciesRaw(["dt~" + packageName], options)
.then(function () {
console.log("Acquired typings for '" + packageName + "'");
})
.catch(function (e) {
console.error("Could not acquire typings for '" + packageName + "'");
});
};

/**
* Installs the typings for the current project.
*/
module.exports.installTypingsForProject = function(options) {
return typings.install(options)
.then(function () {
console.log("Acquired typings for project");
})
.catch(function (e) {
console.error("Could not acquire typings for project");
});
};
15 changes: 15 additions & 0 deletions Nodejs/Product/Nodejs/TypingsAcquisitionTool/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ntvs-typings-acquisition-tool",
"author": "Microsoft",
"version": "1.0.0",
"description": "Internal tool that aquires typings to power Node.js Tools For Visual Studio IntelliSense",
"license": "Apache 2",
"main": "index.js",
"dependencies": {
"minimist": "1.2.0",
"typings-core": "1.3.1"
},
"bin": {
"ntvs_install_typings": "./bin/install_typings"
}
}

0 comments on commit e148ccc

Please sign in to comment.