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 b256db5
Show file tree
Hide file tree
Showing 3 changed files with 129 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 NpmViewTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).View(settings);
}
}
}
35 changes: 35 additions & 0 deletions src/Cake.Npm/View/NpmViewSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Cake.Npm.View
{
using System.Linq;
using Cake.Core;
using Cake.Core.IO;

/// <summary>
/// Contains settings used by <see cref="NpmViewTool"/>.
/// </summary>
public class NpmViewSettings : NpmSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmViewSettings"/> class.
/// </summary>
public NpmViewSettings()
: base("view")
{
RedirectStandardOutput = true;
}

/// <summary>
/// Gets or sets the name of the package for which the registry entry should be shown.
/// </summary>
public string PackageName { get; set; }

/// <inheritdoc />
protected override void EvaluateCore(ProcessArgumentBuilder args)
{
base.EvaluateCore(args);

args.Append("--json");
args.Append(PackageName);
}
}
}
58 changes: 58 additions & 0 deletions src/Cake.Npm/View/NpmViewTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Cake.Npm.View
{
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;
using System.Collections.Generic;

/// <summary>
/// Tool for viewing registry info.
/// </summary>
public class NpmViewTool : NpmTool<NpmSettings>
{
/// <summary>
/// Initializes a new instance of the <see cref="NpmViewTool"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
/// <param name="log">Cake log instance.</param>
public NpmViewTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}

/// <summary>
/// Returns information about a package registry entry.
/// </summary>
/// <param name="settings">The settings.</param>
public string View(NpmViewSettings settings)
{
try
{
IEnumerable<string> output = new List<string>();
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 b256db5

Please sign in to comment.