Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/Cake.Npm.Tests/RunScript/NpmScriptRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ public void Should_Add_ScriptName_To_Arguments_If_Not_Null()
Assert.Equal("run-script \"foo bar\"", result.Args);
}


[Fact]
public void Should_Add_IfPresent_If_Specified()
{
// Given
var fixture = new NpmRunScriptFixture();
fixture.Settings.ScriptName = "foo bar";
fixture.Settings.IfPresent = true;

// When
var result = fixture.Run();

// Then
Assert.Equal("run-script \"foo bar\" --if-present", result.Args);
}

[Fact]
public void Should_Add_IfPresent_and_Arguments_If_Specified()
{
// Given
var fixture = new NpmRunScriptFixture();
fixture.Settings.ScriptName = "foo bar";
fixture.Settings.IfPresent = true;
fixture.Settings.Arguments.Add("--foo=bar");

// When
var result = fixture.Run();

// Then
Assert.Equal("run-script \"foo bar\" --if-present -- --foo=bar", result.Args);
}

[Fact]
public void Should_Add_ScriptArguments_To_Arguments_If_Not_Empty()
{
Expand Down
11 changes: 11 additions & 0 deletions src/Cake.Npm/RunScript/NpmRunScriptSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ public NpmRunScriptSettings()
/// </summary>
public string ScriptName { get; set; }

/// <summary>
/// If true, the script will only be executed if it is present in package.json.
/// </summary>
public bool IfPresent { get; set; }

/// <summary>
/// Arguments to pass to the target script.
/// </summary>
public IList<string> Arguments => _arguments;


/// <summary>
/// Evaluates the settings and writes them to <paramref name="args"/>.
/// </summary>
Expand All @@ -45,6 +51,11 @@ protected override void EvaluateCore(ProcessArgumentBuilder args)
base.EvaluateCore(args);

args.AppendQuoted(ScriptName);

if (IfPresent)
{
args.Append("--if-present");
}

if (Arguments.Any())
{
Expand Down