Skip to content

Commit

Permalink
Merge pull request #2586 from FoothillSolutions/port-typescript-module
Browse files Browse the repository at this point in the history
Porting TypeScript module to FAKE 5
  • Loading branch information
matthid authored Apr 14, 2021
2 parents 9ab339b + d7eda49 commit e7461b6
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 7 deletions.
15 changes: 15 additions & 0 deletions Fake.sln
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.Testing.Fixie", "src\a
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.BuildServer.Bitbucket", "src\app\Fake.BuildServer.Bitbucket\Fake.BuildServer.Bitbucket.fsproj", "{5472237F-020B-4F9A-8E06-01D553576387}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.JavaScript.TypeScript", "src\app\Fake.JavaScript.TypeScript\Fake.JavaScript.TypeScript.fsproj", "{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1210,6 +1212,18 @@ Global
{5472237F-020B-4F9A-8E06-01D553576387}.Release|x64.Build.0 = Release|Any CPU
{5472237F-020B-4F9A-8E06-01D553576387}.Release|x86.ActiveCfg = Release|Any CPU
{5472237F-020B-4F9A-8E06-01D553576387}.Release|x86.Build.0 = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|x64.ActiveCfg = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|x64.Build.0 = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|x86.ActiveCfg = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Debug|x86.Build.0 = Debug|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|Any CPU.Build.0 = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|x64.ActiveCfg = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|x64.Build.0 = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|x86.ActiveCfg = Release|Any CPU
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1300,6 +1314,7 @@ Global
{AB09E8EB-A66E-4D60-B47C-4F35F439D020} = {7BFFAE76-DEE9-417A-A79B-6A6644C4553A}
{5FEC4AB5-D5EE-43B5-B176-0A91D951817A} = {7BFFAE76-DEE9-417A-A79B-6A6644C4553A}
{5472237F-020B-4F9A-8E06-01D553576387} = {7BFFAE76-DEE9-417A-A79B-6A6644C4553A}
{1C476373-FF62-4EA7-8CCC-6A0D0DB96B6D} = {7BFFAE76-DEE9-417A-A79B-6A6644C4553A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {058A0C5E-2216-4306-8AFB-0AE28320C26A}
Expand Down
1 change: 1 addition & 0 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ let dotnetAssemblyInfos =
"Fake.IO.Zip", "Core Zip functionality"
"Fake.JavaScript.Npm", "Running npm commands"
"Fake.JavaScript.Yarn", "Running Yarn commands"
"Fake.JavaScript.TypeScript", "Running TypeScript compiler"
"Fake.Net.Http", "HTTP Client"
"Fake.netcore", "Command line tool"
"Fake.Runtime", "Core runtime features"
Expand Down
1 change: 1 addition & 0 deletions help/templates/template.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<div class="navbar-dropdown" class="navbar-item">
<a href="/apidocs/v5/fake-javascript-npm.html" class="navbar-item">Npm</a>
<a href="/apidocs/v5/fake-javascript-yarn.html" class="navbar-item">Yarn</a>
<a href="/apidocs/v5/fake-javascript-typescript.html" class="navbar-item">TypeScript</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
Expand Down
19 changes: 19 additions & 0 deletions src/app/Fake.JavaScript.TypeScript/AssemblyInfo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Auto-Generated by FAKE; do not edit
namespace System
open System.Reflection

[<assembly: AssemblyTitleAttribute("FAKE - F# Make Running TypeScript Compiler")>]
[<assembly: AssemblyProductAttribute("FAKE - F# Make")>]
[<assembly: AssemblyVersionAttribute("5.20.4")>]
[<assembly: AssemblyInformationalVersionAttribute("5.20.4")>]
[<assembly: AssemblyFileVersionAttribute("5.20.4")>]
[<assembly: AssemblyMetadataAttribute("BuildDate","2021-04-09")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "FAKE - F# Make Running TypeScript Compiler"
let [<Literal>] AssemblyProduct = "FAKE - F# Make"
let [<Literal>] AssemblyVersion = "5.20.4"
let [<Literal>] AssemblyInformationalVersion = "5.20.4"
let [<Literal>] AssemblyFileVersion = "5.20.4"
let [<Literal>] AssemblyMetadata_BuildDate = "2021-04-09"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AssemblyName>Fake.JavaScript.TypeScript</AssemblyName>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="TypeScript.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fake.Core.Environment\Fake.Core.Environment.fsproj" />
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" />
<ProjectReference Include="..\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj" />
<ProjectReference Include="..\Fake.Core.Trace\Fake.Core.Trace.fsproj" />
<ProjectReference Include="..\Fake.Core.String\Fake.Core.String.fsproj" />
</ItemGroup>
<Import Project="..\..\..\.paket\Paket.Restore.targets" />
</Project>
167 changes: 167 additions & 0 deletions src/app/Fake.JavaScript.TypeScript/TypeScript.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
namespace Fake.JavaScript

open Fake.Core
open Fake.IO.FileSystemOperators
open System
open System.IO
open System.Text

/// Helpers to run the typeScript compiler.
///
/// ## Sample
///
/// !! "src/**/*.ts"
/// |> TypeScriptCompiler (fun p -> { p with TimeOut = TimeSpan.MaxValue })
[<RequireQualifiedAccess>]
module TypeScript =
/// Generated ECMAScript version
type ECMAScript =
| ES3
| ES5
| ES6
| ES7
| ES2017
| ES2018
| ES2019
| ES2020
| ESNext

/// Generated JavaScript module type
type ModuleGeneration =
| CommonJs
| ES6
| ES2020
| None
| UMD
| AMD
| System
| ESNext

/// TypeScript task parameter type
type TypeScriptParams =
{
/// Specifies which ECMAScript version the TypeScript compiler should generate. Default is ES3.
ECMAScript : ECMAScript
/// Specifies if the TypeScript compiler should generate a single output file and its filename.
OutputSingleFile : string option
/// Specifies if the TypeScript compiler should generate declaration. Default is false.
EmitDeclaration : bool
/// Specifies which JavaScript module type the TypeScript compiler should generate. Default is CommonJs.
ModuleGeneration : ModuleGeneration
/// Specifies if the TypeScript compiler should emit source maps. Default is false.
EmitSourceMaps : bool
/// Specifies if the TypeScript compiler should not use libs. Default is false.
NoLib : bool
/// Specifies if the TypeScript compiler should remove comments. Default is false.
RemoveComments : bool
/// Specifies the TypeScript compiler path.
ToolPath : string
/// Specifies the TypeScript compiler output path.
OutputPath : string
/// Specifies the timeout for the TypeScript compiler.
TimeOut : TimeSpan }

let internal TypeScriptCompilerPrefix = "Microsoft SDKs" </> "TypeScript"

let extractVersionNumber (di : DirectoryInfo) =
match Double.TryParse di.Name with
| true, d -> d
| false, _ -> 0.0

/// Default parameters for the TypeScript task
let TypeScriptDefaultParams =
{ ECMAScript = ES3
OutputSingleFile = Option.None
EmitDeclaration = false
ModuleGeneration = CommonJs
EmitSourceMaps = false
NoLib = false
RemoveComments = false
OutputPath = null
ToolPath =
if Environment.isUnix then "tsc"
else
let paths =
[ System.Environment.GetFolderPath Environment.SpecialFolder.ProgramFiles; System.Environment.GetFolderPath Environment.SpecialFolder.ProgramFilesX86]
|> List.map (fun p -> p </> TypeScriptCompilerPrefix)
|> List.collect (fun p -> try DirectoryInfo(p).GetDirectories() |> List.ofArray with | _ -> [])
|> List.sortByDescending extractVersionNumber
|> List.map (fun di -> di.FullName)
ProcessUtils.tryFindPath paths "tsc.exe"
|> Option.defaultWith (fun _ -> "tsc.exe")
TimeOut = TimeSpan.FromMinutes 5. }

/// [omit]
let buildArguments parameters file =
let version =
match parameters.ECMAScript with
| ECMAScript.ES3 -> "ES3"
| ECMAScript.ES5 -> "ES5"
| ECMAScript.ES6 -> "ES6"
| ECMAScript.ES7 -> "ES7"
| ECMAScript.ES2017 -> "ES2017"
| ECMAScript.ES2018 -> "ES2018"
| ECMAScript.ES2019 -> "ES2019"
| ECMAScript.ES2020 -> "ES2020"
| ECMAScript.ESNext -> "ESNext"

let moduleGeneration =
match parameters.ModuleGeneration with
| ModuleGeneration.CommonJs -> "CommonJS"
| ModuleGeneration.ES6 -> "ES6"
| ModuleGeneration.ES2020 -> "ES2020"
| ModuleGeneration.None -> "None"
| ModuleGeneration.UMD -> "UMD"
| ModuleGeneration.AMD -> "AMD"
| ModuleGeneration.System -> "System"
| ModuleGeneration.ESNext -> "ESNext"

let args =
new StringBuilder()
|> StringBuilder.appendWithoutQuotes (" --target " + version)
|> StringBuilder.appendIfSome parameters.OutputSingleFile (fun s -> sprintf " --outFile %s" s)
|> StringBuilder.appendQuotedIfNotNull parameters.OutputPath " --outDir "
|> StringBuilder.appendIfTrueWithoutQuotes parameters.EmitDeclaration " --declaration"
|> StringBuilder.appendWithoutQuotes (" --module " + moduleGeneration)
|> StringBuilder.appendIfTrueWithoutQuotes parameters.EmitSourceMaps " --sourceMap"
|> StringBuilder.appendIfTrueWithoutQuotes parameters.NoLib " --noLib"
|> StringBuilder.appendIfTrueWithoutQuotes parameters.RemoveComments " --removeComments"
|> StringBuilder.appendWithoutQuotes " "
|> StringBuilder.append file

args.ToString()

/// Run `tsc --declaration src/app/index.ts`
/// ## Parameters
///
/// - `setParams` - Function used to overwrite the TypeScript compiler flags.
/// - `files` - The type script files to compile.
///
/// ## Sample
///
/// !! "src/**/*.ts"
/// |> TypeScript.compile (fun p -> { p with TimeOut = TimeSpan.MaxValue })
let compile setParams files =
use __ = Trace.traceTask "TypeScript" ""
let parameters = setParams TypeScriptDefaultParams

let callResults =
files
|> Seq.map (buildArguments parameters)
|> Seq.map (fun arguments ->
Diagnostics.ProcessStartInfo(FileName = parameters.ToolPath, Arguments = arguments)
|> CreateProcess.ofStartInfo
|> CreateProcess.redirectOutput
|> CreateProcess.withTimeout parameters.TimeOut
|> Proc.run)

let hasErrors =
callResults
|> Seq.fold (fun acc result ->
match result.ExitCode = 0 with
| true -> Trace.trace result.Result.Output
| false -> Trace.traceError result.Result.Output
if result.ExitCode = 0 then acc else acc + 1) 0

if hasErrors > 0 then
failwith "TypeScript compiler encountered errors!"
4 changes: 4 additions & 0 deletions src/app/Fake.JavaScript.TypeScript/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
group netcore

FSharp.Core
NETStandard.Library
3 changes: 3 additions & 0 deletions src/legacy/FakeLib/FakeLib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@
<Compile Include="..\..\app\Fake.JavaScript.Yarn\Yarn.fs">
<Link>Fake.JavaScript.Yarn/Yarn.fs</Link>
</Compile>
<Compile Include="..\..\app\Fake.JavaScript.TypeScript\TypeScript.fs">
<Link>Fake.JavaScript.TypeScript/TypeScript.fs</Link>
</Compile>
<Compile Include="..\..\app\Fake.Tools.Git\CommandHelper.fs">
<Link>Fake.Tools.Git/CommandHelper.fs</Link>
</Compile>
Expand Down
14 changes: 7 additions & 7 deletions src/legacy/FakeLib/TypeScript.fs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/// Contains code to call the typescript compiler. There is also a [tutorial](../typescript.html) for this task available.
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
module Fake.TypeScript

open System
open System.Text
open System.IO

/// Generated ECMAScript version
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
type ECMAScript =
| ES3
| ES5

/// Generated JavaScript module type
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
type ModuleGeneration =
| CommonJs
| AMD

/// TypeScript task parameter type
[<CLIMutable>]
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
type TypeScriptParams =
{
/// Specifies which ECMAScript version the TypeScript compiler should generate. Default is ES3.
Expand Down Expand Up @@ -48,14 +48,14 @@ type TypeScriptParams =

let private TypeScriptCompilerPrefix = "Microsoft SDKs" </> "TypeScript"

[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
let extractVersionNumber (di : DirectoryInfo) =
match Double.TryParse di.Name with
| true, d -> d
| false, _ -> 0.0

/// Default parameters for the TypeScript task
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
let TypeScriptDefaultParams =
{ ECMAScript = ES3
EmitComments = false
Expand Down Expand Up @@ -116,7 +116,7 @@ let private buildArguments parameters file =
///
/// !! "src/**/*.ts"
/// |> TypeScriptCompiler (fun p -> { p with TimeOut = TimeSpan.MaxValue })
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
[<System.Obsolete("FAKE0001 Use the Fake.JavaScript.TypeScript module instead.")>]
let TypeScriptCompiler setParams files =
use __ = traceStartTaskUsing "TypeScript" ""
let parameters = setParams TypeScriptDefaultParams
Expand Down
2 changes: 2 additions & 0 deletions src/test/Fake.Core.UnitTests/Fake.Core.UnitTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
<ProjectReference Include="..\..\app\Fake.BuildServer.GitLab\Fake.BuildServer.GitLab.fsproj" />
<ProjectReference Include="..\..\app\Fake.Sql.SqlPackage\Fake.Sql.SqlPackage.fsproj" />
<ProjectReference Include="..\..\app\Fake.BuildServer.Bitbucket\Fake.BuildServer.Bitbucket.fsproj" />
<ProjectReference Include="..\..\app\Fake.JavaScript.TypeScript\Fake.JavaScript.TypeScript.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="Fake.JavaScript.TypeScript.fs" />
<Content Include="runtimeconfig.template.json" />
<None Include="paket.references" />
<Compile Include="Fake.ContextHelper.fs" />
Expand Down
Loading

0 comments on commit e7461b6

Please sign in to comment.