Created
January 8, 2015 16:09
-
-
Save garunski/cf8ce3616fd904774c1b to your computer and use it in GitHub Desktop.
Convert JS files to TS files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Threading; | |
| using Microsoft.Build.Evaluation; | |
| namespace convertjstots | |
| { | |
| internal class Program | |
| { | |
| private static string _basePath; | |
| private static Project _project; | |
| private static void Main(string[] args) | |
| { | |
| // load the project file | |
| var projectFile = "C:\\Code\\Test\\test.csproj"; | |
| var directory = "C:\\Code\\Test\\js"; | |
| _basePath = "C:\\Code\\Test\\"; | |
| var collection = new ProjectCollection(); | |
| collection.DefaultToolsVersion = "4.0"; | |
| _project = collection.LoadProject(projectFile); | |
| ProcessDirectory(directory); | |
| _project.Save(); | |
| Console.Read(); | |
| } | |
| private static void ProcessDirectory(string path) | |
| { | |
| foreach (var dir in Directory.EnumerateDirectories(path)) | |
| ProcessDirectory(dir); | |
| foreach (var fileName in Directory.GetFiles(path)) | |
| { | |
| if (fileName.EndsWith(".js")) | |
| { | |
| var fileInfo = new FileInfo(fileName); | |
| var extensionlessFileName = fileInfo.Name.Replace(".js", string.Empty); | |
| if (!Directory.GetFiles(path, extensionlessFileName + ".ts").Any()) | |
| { | |
| fileInfo.MoveTo(Path.Combine(path, extensionlessFileName + ".ts")); | |
| Thread.Sleep(100); | |
| _project.AddItem("TypeScriptCompile", Path.Combine(path, extensionlessFileName + ".ts").Replace(_basePath, string.Empty)); | |
| var start = new ProcessStartInfo { WorkingDirectory = path, Arguments = extensionlessFileName + ".ts", FileName = "tsc" }; | |
| Process.Start(start); | |
| var item = _project.GetItems("Content").FirstOrDefault(i => i.EvaluatedInclude == Path.Combine(path, extensionlessFileName + ".js").Replace(_basePath, string.Empty)); | |
| if (item != null) | |
| _project.RemoveItem(item); | |
| _project.AddItem( | |
| "Content", | |
| Path.Combine(path, extensionlessFileName + ".js").Replace(_basePath, string.Empty), | |
| new List<KeyValuePair<string, string>> | |
| { | |
| new KeyValuePair<string, string>("DependentUpon", extensionlessFileName + ".ts") | |
| }); | |
| Console.WriteLine("TS file " + extensionlessFileName + ".js"); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment