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-120) Add support for npm view #122

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
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 "";
}
}
}
}