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 basic npm support #993

Merged
merged 2 commits into from
Nov 4, 2015
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
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