Skip to content
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

Add YarnHelper that works the same as NpmHelper but calls yarn #1494

Merged
merged 2 commits into from
Mar 17, 2017
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
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