Skip to content

Commit

Permalink
Merge branch 'yarnhelper' of https://github.com/wooderz/FAKE
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Mar 17, 2017
2 parents defb413 + 843374c commit bd0377e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app/FakeLib/FakeLib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="PsExecHelper.fs" />
<Compile Include="NpmHelper.fs" />
<Compile Include="BowerHelper.fs" />
<Compile Include="YarnHelper.fs" />
<Compile Include="AppVeyor.fs" />
<Compile Include="BitbucketPipelines.fs" />
<Compile Include="TaskRunnerHelper.fs" />
Expand Down Expand Up @@ -531,4 +532,4 @@
</ItemGroup>
</When>
</Choose>
</Project>
</Project>
115 changes: 115 additions & 0 deletions src/app/FakeLib/YarnHelper.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/// Contains function to run yarn tasks
module Fake.YarnHelper
open Fake
open System
open System.IO
open System.Diagnostics

/// Default paths to Yarn
let private yarnFileName =
match isWindows with
| true ->
System.Environment.GetEnvironmentVariable("PATH")
|> fun path -> path.Split ';'
|> Seq.tryFind (fun p -> p.IndexOf("yarn", StringComparison.OrdinalIgnoreCase) >= 0)
|> fun res ->
match res with
| Some yarn when File.Exists (sprintf @"%s\yarn.cmd" yarn) -> (sprintf @"%s\yarn.cmd" yarn)
| _ -> "./packages/Yarnpkg.js/tools/yarn.cmd"
| _ ->
let info = new ProcessStartInfo("which","yarn")
info.StandardOutputEncoding <- System.Text.Encoding.UTF8
info.RedirectStandardOutput <- true
info.UseShellExecute <- false
info.CreateNoWindow <- true
use proc = Process.Start info
proc.WaitForExit()
match proc.ExitCode with
| 0 when not proc.StandardOutput.EndOfStream ->
proc.StandardOutput.ReadLine()
| _ -> "/usr/bin/yarn"

/// Arguments for the Yarn install command
type InstallArgs =
| Standard
| Flat
| Force
| Har
| NoLockFile
| Production
| PureLockfile

/// The list of supported Yarn commands. The `Custom` alternative
/// can be used for other commands not in the list until they are
/// implemented
type YarnCommand =
| Install of InstallArgs
| Add of string
| Custom of string

/// The Yarn parameter type
[<CLIMutable>]
type YarnParams =
{ Src: string
YarnFilePath: string
WorkingDirectory: string
Command: YarnCommand
Timeout: TimeSpan }

/// Yarn default parameters
let defaultYarnParams =
{ Src = ""
YarnFilePath = yarnFileName
Command = Install Standard
WorkingDirectory = "."
Timeout = TimeSpan.MaxValue }

let private parseInstallArgs = function
| Standard -> ""
| Flat -> " --flat"
| Force -> " --force"
| Har -> " --har"
| NoLockFile -> " --no-lockfile"
| Production -> " --production"
| PureLockFile -> " --pure-lockfile"

let private parse = function
| Install installArgs -> sprintf "install%s" (installArgs |> parseInstallArgs)
| Add str -> sprintf "add %s" str
| Custom str -> str

let run yarnParams =
let yarnPath = Path.GetFullPath(yarnParams.YarnFilePath)
let arguments = yarnParams.Command |> parse
let ok =
execProcess (fun info ->
info.FileName <- yarnPath
info.WorkingDirectory <- yarnParams.WorkingDirectory
info.Arguments <- arguments) yarnParams.Timeout
if not ok then failwith (sprintf "'yarn %s' task failed" arguments)

/// Runs yarn with the given modification function. Make sure to have yarn installed,
/// you can install yarn with nuget or a regular install. To change which `Yarn` executable
/// to use you can set the `YarnFilePath` parameter with the `setParams` function.
///
/// ## Parameters
///
/// - `setParams` - Function used to overwrite the Yarn default parameters.
///
/// ## Sample
///
/// Target "Web" (fun _ ->
/// Yarn (fun p ->
/// { p with
/// Command = Install Standard
/// WorkingDirectory = "./src/FAKESimple.Web/"
/// })
///
/// Yarn (fun p ->
/// { p with
/// Command = (Run "build")
/// WorkingDirectory = "./src/FAKESimple.Web/"
/// })
/// )
let Yarn setParams =
defaultYarnParams |> setParams |> run

0 comments on commit bd0377e

Please sign in to comment.