Skip to content

Commit b256db5

Browse files
Kuinoxpascalberger
authored andcommitted
(GH-120) Add support for npm view
1 parent af4da1b commit b256db5

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/Cake.Npm/NpmViewAliases.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Cake.Core;
2+
using Cake.Core.Annotations;
3+
using Cake.Npm.View;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace Cake.Npm
9+
{
10+
/// <summary>
11+
/// Npm view aliases
12+
/// </summary>
13+
[CakeAliasCategory("Npm")]
14+
[CakeNamespaceImport("Cake.Npm")]
15+
public static class NpmViewAliases
16+
{
17+
/// <summary>
18+
/// Call npm view with --json attribute.
19+
/// </summary>
20+
/// <param name="context"></param>
21+
/// <param name="packageName">Name of the package</param>
22+
/// <param name="workingDirectory"></param>
23+
/// <returns>An empty string if the package was not found on the repository</returns>
24+
[CakeMethodAlias]
25+
[CakeAliasCategory("View")]
26+
public static string NpmView(this ICakeContext context, string packageName = null, string workingDirectory = null)
27+
{
28+
NpmViewSettings settings = new NpmViewSettings()
29+
{
30+
PackageName = packageName,
31+
WorkingDirectory = workingDirectory
32+
};
33+
return new NpmViewTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).View(settings);
34+
}
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Cake.Npm.View
2+
{
3+
using System.Linq;
4+
using Cake.Core;
5+
using Cake.Core.IO;
6+
7+
/// <summary>
8+
/// Contains settings used by <see cref="NpmViewTool"/>.
9+
/// </summary>
10+
public class NpmViewSettings : NpmSettings
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="NpmViewSettings"/> class.
14+
/// </summary>
15+
public NpmViewSettings()
16+
: base("view")
17+
{
18+
RedirectStandardOutput = true;
19+
}
20+
21+
/// <summary>
22+
/// Gets or sets the name of the package for which the registry entry should be shown.
23+
/// </summary>
24+
public string PackageName { get; set; }
25+
26+
/// <inheritdoc />
27+
protected override void EvaluateCore(ProcessArgumentBuilder args)
28+
{
29+
base.EvaluateCore(args);
30+
31+
args.Append("--json");
32+
args.Append(PackageName);
33+
}
34+
}
35+
}

src/Cake.Npm/View/NpmViewTool.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace Cake.Npm.View
2+
{
3+
using Cake.Core;
4+
using Cake.Core.Diagnostics;
5+
using Cake.Core.IO;
6+
using Cake.Core.Tooling;
7+
using System.Collections.Generic;
8+
9+
/// <summary>
10+
/// Tool for viewing registry info.
11+
/// </summary>
12+
public class NpmViewTool : NpmTool<NpmSettings>
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="NpmViewTool"/> class.
16+
/// </summary>
17+
/// <param name="fileSystem">The file system.</param>
18+
/// <param name="environment">The environment.</param>
19+
/// <param name="processRunner">The process runner.</param>
20+
/// <param name="tools">The tool locator.</param>
21+
/// <param name="log">Cake log instance.</param>
22+
public NpmViewTool(
23+
IFileSystem fileSystem,
24+
ICakeEnvironment environment,
25+
IProcessRunner processRunner,
26+
IToolLocator tools,
27+
ICakeLog log)
28+
: base(fileSystem, environment, processRunner, tools, log)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Returns information about a package registry entry.
34+
/// </summary>
35+
/// <param name="settings">The settings.</param>
36+
public string View(NpmViewSettings settings)
37+
{
38+
try
39+
{
40+
IEnumerable<string> output = new List<string>();
41+
RunCore(
42+
settings,
43+
new ProcessSettings(),
44+
process =>
45+
{
46+
output = process.GetStandardOutput();
47+
});
48+
return string.Join("\n", output);
49+
50+
}
51+
catch (CakeException)
52+
{
53+
CakeLog.Information("Error should be a 404 and i ignore it.");
54+
return "";
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)