Skip to content

Commit

Permalink
Add tag and access option for NpmPublish
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed May 18, 2017
1 parent b290cfa commit fb71288
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/Cake.Npm/Publish/NpmPublishAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Cake.Npm.Publish
{
/// <summary>
/// Possible values for <see cref="NpmPublishSettings.Access"/>.
/// </summary>
public enum NpmPublishAccess
{
/// <summary>
/// Default value for <c>npm publish</c>.
/// </summary>
Default,

/// <summary>
/// Public access.
/// </summary>
Public,

/// <summary>
/// Restricted access.
/// </summary>
Restricted
}
}
25 changes: 23 additions & 2 deletions src/Cake.Npm/Publish/NpmPublishSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ public NpmPublishSettings()
}

/// <summary>
/// Source to publish.
/// Gets or sets the source to publish.
/// A folder containing a package.json file or an url or file path to a gzipped tar archive
/// containing a single folder with a package.json file inside.
/// </summary>
public string Source { get; set; }

/// <summary>
/// Registry where the package should be published to.
/// Gets or sets the tag with which the package will be published.
/// By default the <c>latest</c> tag is used.
/// </summary>
public string Tag { get; set; }

/// <summary>
/// Gets or sets whether the package should be published as public or restricted.
/// </summary>
public NpmPublishAccess Access { get; set; }

/// <summary>
/// Gets or sets the registry where the package should be published to.
/// </summary>
public Uri Registry { get; set; }

Expand All @@ -42,6 +53,16 @@ protected override void EvaluateCore(ProcessArgumentBuilder args)
args.AppendQuoted(Source);
}

if (!string.IsNullOrWhiteSpace(Tag))
{
args.AppendSwitch("--tag", Tag);
}

if (Access != NpmPublishAccess.Default)
{
args.AppendSwitch("--access", Access.ToString().ToLowerInvariant());
}

if (Registry != null)
{
args.AppendSwitch("--registry", Registry.ToString());
Expand Down
41 changes: 41 additions & 0 deletions src/Cake.Npm/Publish/NpmPublishSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@ public static NpmPublishSettings FromSource(this NpmPublishSettings settings, st
return settings;
}

/// <summary>
/// Sets the tag of the package to publish.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="tag">Tag with which the package will be published.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Tag"/> set to <paramref name="tag"/>.</returns>
public static NpmPublishSettings WithTag(this NpmPublishSettings settings, string tag)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

if (string.IsNullOrWhiteSpace(tag))
{
throw new ArgumentNullException(nameof(tag));
}

settings.Tag = tag;

return settings;
}

/// <summary>
/// Sets the access of the published package.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="access">Access of the published package.</param>
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmPublishSettings.Access"/> set to <paramref name="access"/>.</returns>
public static NpmPublishSettings WithAccess(this NpmPublishSettings settings, NpmPublishAccess access)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

settings.Access = access;

return settings;
}

/// <summary>
/// Sets the registry where the package will be published to.
/// </summary>
Expand Down
67 changes: 67 additions & 0 deletions tests/Cake.Npm.Tests/Publish/NpmPublishSettingsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,73 @@ public void Should_Set_Source()
settings.Source.ShouldBe(source);
}

[Fact]
public void Should_Throw_If_Tag_Is_Null()
{
// Given
var settings = new NpmPublishSettings();

// When
var result = Record.Exception(() => settings.WithTag(null));

// Then
result.IsArgumentNullException("tag");
}

[Fact]
public void Should_Throw_If_Tag_Is_Empty()
{
// Given
var settings = new NpmPublishSettings();

// When
var result = Record.Exception(() => settings.WithTag(string.Empty));

// Then
result.IsArgumentNullException("tag");
}

[Fact]
public void Should_Throw_If_Tag_Is_WhiteSpace()
{
// Given
var settings = new NpmPublishSettings();

// When
var result = Record.Exception(() => settings.WithTag(" "));

// Then
result.IsArgumentNullException("tag");
}

[Fact]
public void Should_Set_Tag()
{
// Given
var settings = new NpmPublishSettings();
var tag = "1.2.3";

// When
settings.WithTag(tag);

// Then
settings.Tag.ShouldBe(tag);
}

[Fact]
public void Should_Set_Access()
{
// Given
var settings = new NpmPublishSettings();
var access = NpmPublishAccess.Restricted;

// When
settings.WithAccess(access);

// Then
settings.Access.ShouldBe(access);
}

[Fact]
public void Should_Throw_If_Registry_Is_Null()
{
Expand Down
29 changes: 29 additions & 0 deletions tests/Cake.Npm.Tests/Publish/NpmPublisherTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Cake.Npm.Tests.Publish
{
using System;
using Cake.Npm.Publish;
using Core.Diagnostics;
using Xunit;

Expand Down Expand Up @@ -49,6 +50,34 @@ public void Should_Add_Source_To_Arguments_If_Not_Null()
Assert.Equal("publish \"c:\\mypackage\"", result.Args);
}

[Fact]
public void Should_Add_Tag_To_Arguments_If_Not_Null()
{
// Given
var fixture = new NpmPublisherFixture();
fixture.Settings.Tag = "foo";

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

// Then
Assert.Equal("publish --tag foo", result.Args);
}

[Fact]
public void Should_Add_Access_To_Arguments_If_Not_Default()
{
// Given
var fixture = new NpmPublisherFixture();
fixture.Settings.Access = NpmPublishAccess.Public;

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

// Then
Assert.Equal("publish --access public", result.Args);
}

[Fact]
public void Should_Add_Registry_To_Arguments_If_Not_Null()
{
Expand Down

0 comments on commit fb71288

Please sign in to comment.