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

(GH-121) Add support for reading npm configuration #123

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
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);
}
}
}