Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/fsharp/FAKE
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Nov 4, 2015
2 parents 7143200 + a9e289c commit fd3dd32
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/FakeLib/FakeLib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="ServiceControllerHelper.fs" />
<Compile Include="GuardedAwaitObservable.fs" />
<Compile Include="ProcessHelper.fs" />
<Compile Include="NpmHelper.fs" />
<Compile Include="AppVeyor.fs" />
<Compile Include="TaskRunnerHelper.fs" />
<Compile Include="Globbing\Globbing.fs" />
Expand Down
85 changes: 85 additions & 0 deletions src/app/FakeLib/NpmHelper.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/// Contains function to run npm tasks
module Fake.NpmHelper
open Fake
open System
open System.IO

/// Default paths to Npm
let private npmFileName =
match isUnix with
| true -> "/usr/local/bin/npm"
| _ -> "./packages/Npm.js/tools/npm.cmd"

/// Arguments for the Npm install command
type InstallArgs =
| Standard
| Forced

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

/// The Npm parameter type
type NpmParams =
{ Src: string
NpmFilePath: string
WorkingDirectory: string
Command: NpmCommand
Timeout: TimeSpan }

/// Npm default parameters
let defaultNpmParams =
{ Src = ""
NpmFilePath = npmFileName
Command = Install Standard
WorkingDirectory = "."
Timeout = TimeSpan.MaxValue }

let private parseInstallArgs = function
| Standard -> ""
| Forced -> " --force"

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

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

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

0 comments on commit fd3dd32

Please sign in to comment.