Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add file name suffix option #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static int GenerateJavaScript(JavaScriptGeneratorSettings settings) =>
/// </summary>
public bool DisableESLint { get; set; }

/// <summary>
/// Suffix to append to generated file names before the extension.
/// </summary>
public string? FileNameSuffix { get; set; }

/// <summary>
/// Generates the JavaScript/TypeScript output.
/// </summary>
Expand All @@ -58,9 +63,9 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service)

var moduleName = ModuleName ?? service.Name;
var capModuleName = CodeGenUtility.Capitalize(moduleName);
var typesFileName = CodeGenUtility.Uncapitalize(moduleName) + "Types" + (TypeScript ? ".ts" : ".js");
var clientFileName = CodeGenUtility.Uncapitalize(moduleName) + (TypeScript ? ".ts" : ".js");
var serverFileName = CodeGenUtility.Uncapitalize(moduleName) + "Server" + (TypeScript ? ".ts" : ".js");
var typesFileName = CodeGenUtility.Uncapitalize(moduleName) + "Types" + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js");
var clientFileName = CodeGenUtility.Uncapitalize(moduleName) + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js");
var serverFileName = CodeGenUtility.Uncapitalize(moduleName) + "Server" + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js");

var errors = new List<ServiceDefinitionError>();
var namedTexts = new List<CodeGenFile>();
Expand Down Expand Up @@ -529,6 +534,7 @@ public override void ApplySettings(FileGeneratorSettings settings)
Express = ourSettings.Express;
Fastify = ourSettings.Fastify;
DisableESLint = ourSettings.DisableESLint;
FileNameSuffix = ourSettings.FileNameSuffix;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public sealed class JavaScriptGeneratorSettings : FileGeneratorSettings
/// True to disable ESLint via code comment.
/// </summary>
public bool DisableESLint { get; set; }

/// <summary>
/// Suffix to append to generated file names before the extension.
/// </summary>
public string? FileNameSuffix { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/fsdgenjs/FsdGenJavaScriptApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public sealed class FsdGenJavaScriptApp : CodeGeneratorApp
" Generates a Fastify plugin. When specified, only the server plugin is generated, not the client.",
" --disable-eslint",
" Disables ESLint via code comment.",
" --file-name-suffix",
" Suffix to append to generated file names before the file extension.",
];

protected override CodeGenerator CreateGenerator() => new JavaScriptGenerator();
Expand All @@ -38,6 +40,7 @@ protected override FileGeneratorSettings CreateSettings(ArgsReader args) =>
Express = args.ReadFlag("express"),
Fastify = args.ReadFlag("fastify"),
DisableESLint = args.ReadFlag("disable-eslint"),
FileNameSuffix = args.ReadOption("file-name-suffix"),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Facility.Definition;
using Facility.Definition.CodeGen;
using Facility.Definition.Fsd;
using FluentAssertions;
using NUnit.Framework;
Expand Down Expand Up @@ -277,6 +278,31 @@ export enum ExampleApiErrors {
Assert.That(typesFile.Text, Contains.Substring(expectedErrorSet));
}

[TestCase("", true)]
[TestCase("", false)]
[TestCase("suffix", true)]
[TestCase("suffix", false)]
[TestCase(".g", true)]
[TestCase(".g", false)]
public void GenerateWithCustomFileNameSuffix(string suffix, bool isTypeScript)
{
const string definition = "service TestApi { }";
var parser = new FsdParser();
var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition));
var generator = new JavaScriptGenerator { GeneratorName = "JavaScriptGeneratorTests", TypeScript = isTypeScript, Express = true, FileNameSuffix = suffix };
var result = generator.GenerateOutput(service);
Assert.That(result, Is.Not.Null);

Assert.That(result.Files, Has.Count.EqualTo(isTypeScript ? 3 : 2));
var fullSuffix = suffix + (isTypeScript ? ".ts" : ".js");
Assert.That(result.Files, Has.One.Matches<CodeGenFile>(f => f.Name == $"testApi{fullSuffix}"));
Assert.That(result.Files, Has.One.Matches<CodeGenFile>(f => f.Name == $"testApi{fullSuffix}"));
Assert.That(result.Files, Has.One.Matches<CodeGenFile>(f => f.Name == $"testApiServer{fullSuffix}"));

if (isTypeScript)
Assert.That(result.Files.SingleOrDefault(f => f.Name == $"testApiTypes{fullSuffix}"), Is.Not.Null);
}

private void ThrowsServiceDefinitionException(string definition, string message)
{
var parser = new FsdParser();
Expand Down