gpt4 book ai didi

c# - 在 C# 中以编程方式编译 typescript ?

转载 作者:可可西里 更新时间:2023-11-01 03:00:38 29 4
gpt4 key购买 nike

我正在尝试用 C# 编写一个函数,它接受一个包含 typescript 代码的字符串并返回一个包含 JavaScript 代码的字符串。有这方面的库函数吗?

最佳答案

可以使用Process调用编译器,指定--out file.js到一个临时文件夹,读取编译文件的内容。

我做了一个小应用程序来做到这一点:

用法

TypeScriptCompiler.Compile(@"C:\tmp\test.ts");

获取JS字符串

string javascriptSource = File.ReadAllText(@"C:\tmp\test.js");

带有示例和注释的完整源代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
// compiles a TS file
TypeScriptCompiler.Compile(@"C:\tmp\test.ts");

// if no errors were found, read the contents of the compile file
string javascriptSource = File.ReadAllText(@"C:\tmp\test.js");
}
catch (InvalidTypeScriptFileException ex)
{
// there was a compiler error, show the compiler output
Console.WriteLine(ex.Message);
}

Console.ReadKey();
}
}

public static class TypeScriptCompiler
{
// helper class to add parameters to the compiler
public class Options
{
private static Options @default;
public static Options Default
{
get
{
if (@default == null)
@default = new Options();

return @default;
}
}

public enum Version
{
ES5,
ES3,
}

public bool EmitComments { get; set; }
public bool GenerateDeclaration { get; set; }
public bool GenerateSourceMaps { get; set; }
public string OutPath { get; set; }
public Version TargetVersion { get; set; }

public Options() { }

public Options(bool emitComments = false
, bool generateDeclaration = false
, bool generateSourceMaps = false
, string outPath = null
, Version targetVersion = Version.ES5)
{
EmitComments = emitComments;
GenerateDeclaration = generateDeclaration;
GenerateSourceMaps = generateSourceMaps;
OutPath = outPath;
TargetVersion = targetVersion;
}
}

public static void Compile(string tsPath, Options options = null)
{
if (options == null)
options = Options.Default;

var d = new Dictionary<string,string>();

if (options.EmitComments)
d.Add("-c", null);

if (options.GenerateDeclaration)
d.Add("-d", null);

if (options.GenerateSourceMaps)
d.Add("--sourcemap", null);

if (!String.IsNullOrEmpty(options.OutPath))
d.Add("--out", options.OutPath);

d.Add("--target", options.TargetVersion.ToString());

// this will invoke `tsc` passing the TS path and other
// parameters defined in Options parameter
Process p = new Process();

ProcessStartInfo psi = new ProcessStartInfo("tsc", tsPath + " " + String.Join(" ", d.Select(o => o.Key + " " + o.Value)));

// run without showing console windows
psi.CreateNoWindow = true;
psi.UseShellExecute = false;

// redirects the compiler error output, so we can read
// and display errors if any
psi.RedirectStandardError = true;

p.StartInfo = psi;

p.Start();

// reads the error output
var msg = p.StandardError.ReadToEnd();

// make sure it finished executing before proceeding
p.WaitForExit();

// if there were errors, throw an exception
if (!String.IsNullOrEmpty(msg))
throw new InvalidTypeScriptFileException(msg);
}
}

public class InvalidTypeScriptFileException : Exception
{
public InvalidTypeScriptFileException() : base()
{

}
public InvalidTypeScriptFileException(string message) : base(message)
{

}
}
}

关于c# - 在 C# 中以编程方式编译 typescript ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046203/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com