jburzynski / TypeGen

Single-class-per-file C# to TypeScript generator
MIT License
197 stars 56 forks source link

Added support to Export Enums as UnionTypes, Added support to set Output path with Cli parameter #159

Closed dersia closed 1 year ago

dersia commented 1 year ago

2 new features in this PR

Feature 1: Enum UnionTypes

I had a need to export Enums as (Union-)Types. So for the following Enum:

public enum SocialMediaTypes 
{
    Facebook,
    Twitter,
    Instagram,
    Mastodon
}

I needed a typescript type to be generated:

export type SocialMediaTypes = "Facebook" | "Twitter" | "Instagram" | "Mastodon"

this PR adds an option to the ExportEnumAttribute to define it as UnionType

[AttributeUsage(AttributeTargets.Enum)]
public class ExportTsEnumAttribute : ExportAttribute
{
    /// <summary>
    /// Specifies whether an enum should be exported as TypeScript const enum
    /// </summary>
    public bool IsConst { get; set; }
    /// <summary>
    /// Specifies whether the enum should be exported as a TypeScript UnionType
    /// </summary>
    public bool AsUnionType { get; set; }
}

Feature 2: define output path on cli

I am not working with a config file, because i don't have a need for one. The only thing that I needed, that wasn't available on the cli is the option to define an output path when generating using the cli. I need this for my build pipelines, where I generate npm packages during build and deployment.

for this a new commandline option was added -o or --output-path. this will be preferred, over the output path in the config, if set.

jburzynski commented 1 year ago

Thanks very much for the pull request! I've added this functionality to version 4.2.0 (should be released shortly).