Skip to content

Commit

Permalink
(GH-120) Add support for npm view
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 8f141ee
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Cake.Npm/NpmViewAliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Npm.View;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cake.Npm
{
/// <summary>
/// Npm view aliases
/// </summary>
[CakeAliasCategory("Npm")]
[CakeNamespaceImport("Cake.Npm")]
public static class NpmViewAliases
{
/// <summary>
/// Call npm view with --json attribute.
/// </summary>
/// <param name="context"></param>
/// <param name="packageName">Name of the package</param>
/// <param name="workingDirectory"></param>
/// <returns>An empty string if the package was not found on the repository</returns>
[CakeMethodAlias]
[CakeAliasCategory("View")]
public static string NpmView(this ICakeContext context, string packageName = null, string workingDirectory = null)
{
NpmViewSettings settings = new NpmViewSettings()
{
PackageName = packageName,
WorkingDirectory = workingDirectory
};
return new NpmViewTools(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).View(settings);
}
}
}
26 changes: 26 additions & 0 deletions src/Cake.Npm/View/NpmViewSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Cake.Core;
using Cake.Core.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cake.Npm.View
{
class NpmViewSettings : NpmSettings
{
public NpmViewSettings() : base("view")
{
RedirectStandardOutput = true;
}

public string PackageName { get; set; }

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

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

public string View(NpmViewSettings settings)
{
IEnumerable<string> output = new List<string>();
try
{
RunCore(settings, new ProcessSettings(), process =>
{
output = process.GetStandardOutput();
});
return string.Join("\n", output);

}
catch (CakeException)
{
CakeLog.Information("Error should be a 404 and i ignore it.");
return "";
}

}
}
}

0 comments on commit 8f141ee

Please sign in to comment.