Skip to content

Instantly share code, notes, and snippets.

@davidebbo
Created April 25, 2012 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidebbo/2487658 to your computer and use it in GitHub Desktop.
Save davidebbo/2487658 to your computer and use it in GitHub Desktop.
Node detection
private const string PackageJsonFile = "package.json";
private readonly string[] NodeDetectionFiles = new[] { "server.js", "app.js" };
private const string WebConfigFile = "web.config";
/// <summary>
/// Add a web.config file if we detect a Node site
/// </summary>
private void AddIISNodeConfig(DeploymentContext context)
{
// If there is a config file already, don't do anything
string webConfig = Path.Combine(context.OutputPath, WebConfigFile);
if (File.Exists(webConfig)) return;
foreach (var nodeDetectionFile in NodeDetectionFiles)
{
// If the node detection file exists, create an iisnode web.config file for it
string fullPath = Path.Combine(context.OutputPath, nodeDetectionFile);
if (File.Exists(fullPath))
{
using (context.Tracer.Step(Resources.Log_CreatingNodeConfig))
{
context.Logger.Log(Resources.Log_CreatingNodeConfig);
File.WriteAllText(webConfig, String.Format(Resources.IisNodeWebConfig, nodeDetectionFile));
return;
}
}
}
// If we couldn't treat it as a Node site, but it appears that the user expects it to be,
// give a warning.
if (LooksLikeNodeSite(context.OutputPath))
{
context.Logger.Log(Resources.Log_NodeWithMissingServerJs);
}
}
private bool LooksLikeNodeSite(string webRoot)
{
// If it has a node_modules folder, it's likely Node
if (Directory.Exists(Path.Combine(webRoot, "node_modules")))
{
return true;
}
// If it has any PHP/HTML files at the root, treat it as non-Node
if (Directory.EnumerateFiles(webRoot, "*.php").Any() || Directory.EnumerateFiles(webRoot, "*.htm").Any() || Directory.EnumerateFiles(webRoot, "*.html").Any())
{
return false;
}
// Treat it as Node if it has at least one .js file at the root
return Directory.EnumerateFiles(webRoot, "*.js").Any();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment