From 8f141ee9375dae30fc32a53042be55ef470e7d8e Mon Sep 17 00:00:00 2001 From: Kuinox Date: Mon, 29 Apr 2019 00:06:54 +0200 Subject: [PATCH] (GH-120) Add support for npm view --- src/Cake.Npm/NpmViewAliases.cs | 36 +++++++++++++++++++++++ src/Cake.Npm/View/NpmViewSettings.cs | 26 +++++++++++++++++ src/Cake.Npm/View/NpmViewTools.cs | 43 ++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/Cake.Npm/NpmViewAliases.cs create mode 100644 src/Cake.Npm/View/NpmViewSettings.cs create mode 100644 src/Cake.Npm/View/NpmViewTools.cs diff --git a/src/Cake.Npm/NpmViewAliases.cs b/src/Cake.Npm/NpmViewAliases.cs new file mode 100644 index 0000000..0541f5c --- /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 NpmViewTools(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..19eca2f --- /dev/null +++ b/src/Cake.Npm/View/NpmViewSettings.cs @@ -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); + } + } +} diff --git a/src/Cake.Npm/View/NpmViewTools.cs b/src/Cake.Npm/View/NpmViewTools.cs new file mode 100644 index 0000000..56c18ed --- /dev/null +++ b/src/Cake.Npm/View/NpmViewTools.cs @@ -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 + { + public NpmViewTools( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + public string View(NpmViewSettings settings) + { + IEnumerable output = new List(); + 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 ""; + } + + } + } +}