diff --git a/src/Cake.Npm/NpmViewAliases.cs b/src/Cake.Npm/NpmViewAliases.cs new file mode 100644 index 0000000..0565327 --- /dev/null +++ b/src/Cake.Npm/NpmViewAliases.cs @@ -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 +{ + /// + /// Npm view aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm")] + public static class NpmViewAliases + { + /// + /// Call npm view with --json attribute. + /// + /// + /// Name of the package + /// + /// An empty string if the package was not found on the repository + [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); + } + } +} diff --git a/src/Cake.Npm/View/NpmViewSettings.cs b/src/Cake.Npm/View/NpmViewSettings.cs new file mode 100644 index 0000000..8bb5c44 --- /dev/null +++ b/src/Cake.Npm/View/NpmViewSettings.cs @@ -0,0 +1,35 @@ +namespace Cake.Npm.View +{ + using System.Linq; + using Cake.Core; + using Cake.Core.IO; + + /// + /// Contains settings used by . + /// + public class NpmViewSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmViewSettings() + : base("view") + { + RedirectStandardOutput = true; + } + + /// + /// Gets or sets the name of the package for which the registry entry should be shown. + /// + public string PackageName { get; set; } + + /// + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + args.Append("--json"); + args.Append(PackageName); + } + } +} diff --git a/src/Cake.Npm/View/NpmViewTool.cs b/src/Cake.Npm/View/NpmViewTool.cs new file mode 100644 index 0000000..6f40458 --- /dev/null +++ b/src/Cake.Npm/View/NpmViewTool.cs @@ -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; + + /// + /// Tool for viewing registry info. + /// + public class NpmViewTool : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmViewTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Returns information about a package registry entry. + /// + /// The settings. + public string View(NpmViewSettings settings) + { + try + { + IEnumerable output = new List(); + 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 ""; + } + } + } +}