-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fsharp/FAKE
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |