Skip to content

Commit

Permalink
(cake-contribGH-121) Add support for reading npm configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuinox authored and pascalberger committed Aug 29, 2020
1 parent af4da1b commit 66ebbf0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Cake.Npm/Get/NpmGetSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Cake.Core;
using Cake.Core.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cake.Npm.Get
{
class NpmGetSettings : NpmSettings
{
public NpmGetSettings() : base("get")
{
RedirectStandardOutput = true;
}
public string Key { get; set; }

protected override void EvaluateCore(ProcessArgumentBuilder args)
{
base.EvaluateCore(args);
args.Append(Key);
}
}
}
39 changes: 39 additions & 0 deletions src/Cake.Npm/Get/NpmGetTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cake.Npm.Get
{
class NpmGetTools : NpmTool<NpmSettings>
{
public NpmGetTools(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}


public string Get(NpmGetSettings settings)
{
if (string.IsNullOrWhiteSpace(settings.Key))
{
throw new ArgumentException();
}
IEnumerable<string> output = new List<string>();
RunCore(settings, new ProcessSettings(), process =>
{
output = process.GetStandardOutput();
});
return output.SingleOrDefault();
}
}
}
40 changes: 40 additions & 0 deletions src/Cake.Npm/NpmGetAliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Npm.Get;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cake.Npm
{
/// <summary>
/// Npm get aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmGetAliases
{
/// <summary>
/// Gets the npm config on the given key
/// </summary>
/// <param name="context">The cake context.</param>
/// <param name="key">The config key.</param>
/// <param name="workingDirectory">The working directory</param>
/// <returns>The value on the given key.</returns>
[CakeMethodAlias]
[CakeAliasCategory("Get")]
public static string NpmGet(this ICakeContext context, string key, string workingDirectory = null)
{
if (key == null)
{
throw new ArgumentNullException("key can't be null");
}
NpmGetSettings settings = new NpmGetSettings()
{
Key = key,
WorkingDirectory = workingDirectory
};
return new NpmGetTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).Get(settings);
}
}
}

0 comments on commit 66ebbf0

Please sign in to comment.