diff --git a/nuspec/nuget/Cake.Npm.nuspec b/nuspec/nuget/Cake.Npm.nuspec index 0b2c62f..8391399 100644 --- a/nuspec/nuget/Cake.Npm.nuspec +++ b/nuspec/nuget/Cake.Npm.nuspec @@ -13,12 +13,9 @@ https://cdn.rawgit.com/cake-contrib/graphics/a5cf0f881c390650144b2243ae551d5b9f836196/png/cake-contrib-medium.png false cake npm cake-build cake-contrib - https://github.com/cake-contrib/Cake.Npm/releases/tag/0.13.0 + https://github.com/cake-contrib/Cake.Npm/releases/tag/0.14.0 - - - diff --git a/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj b/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj index 0c33ba9..0a8cd5e 100644 --- a/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj +++ b/src/Cake.Npm.Tests/Cake.Npm.Tests.csproj @@ -1,8 +1,7 @@  - net46;netcoreapp2.0 - 2.0 + netcoreapp2.0 false false false @@ -11,9 +10,9 @@ - - - + + + diff --git a/src/Cake.Npm.Tests/Ci/NpmCiToolTests.cs b/src/Cake.Npm.Tests/Ci/NpmCiToolTests.cs index f71e644..2ee4de0 100644 --- a/src/Cake.Npm.Tests/Ci/NpmCiToolTests.cs +++ b/src/Cake.Npm.Tests/Ci/NpmCiToolTests.cs @@ -1,5 +1,6 @@ using System; using Cake.Core.Diagnostics; +using Cake.Npm.Ci; using Xunit; namespace Cake.Npm.Tests.Ci @@ -90,6 +91,21 @@ public void Should_Use_Cake_LogLevel_If_LogLevel_Is_Set_To_Default( // Then Assert.Equal(expected, result.Args); } + + [Fact] + public void Should_Include_Production_Flag() + { + // Given + var fixture = new NpmCiToolFixture(); + fixture.Settings.ForProduction(); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("ci --production", result.Args); + } + } } } diff --git a/src/Cake.Npm.Tests/Set/NpmSetToolFixture.cs b/src/Cake.Npm.Tests/Set/NpmSetToolFixture.cs new file mode 100644 index 0000000..89ce94b --- /dev/null +++ b/src/Cake.Npm.Tests/Set/NpmSetToolFixture.cs @@ -0,0 +1,17 @@ +using Cake.Npm.Set; + +namespace Cake.Npm.Tests.Set +{ + internal sealed class NpmSetToolFixture : NpmFixture + { + public NpmSetToolFixture() + { + } + + protected override void RunTool() + { + var tool = new NpmSetTool(FileSystem, Environment, ProcessRunner, Tools, Log); + tool.Set(Settings); + } + } +} diff --git a/src/Cake.Npm.Tests/Set/NpmSetToolTests.cs b/src/Cake.Npm.Tests/Set/NpmSetToolTests.cs new file mode 100644 index 0000000..e30e047 --- /dev/null +++ b/src/Cake.Npm.Tests/Set/NpmSetToolTests.cs @@ -0,0 +1,73 @@ +using Cake.Npm.Set; +using Shouldly; +using Xunit; + +namespace Cake.Npm.Tests.Set +{ + public class NpmSetToolTests + { + public sealed class TheSetMethod + { + [Fact] + public void Should_Redirect_Standard_Error() + { + var fixture = new NpmSetToolFixture(); + fixture.Settings.RedirectStandardError = true; + fixture.Settings.Key = "progress"; + fixture.Settings.Value = "false"; + var result = fixture.Run(); + + Assert.True(result.Process.RedirectStandardError); + } + + [Fact] + public void Should_Throw_If_Settings_Are_Null() + { + // Given + var fixture = new NpmSetToolFixture(); + fixture.Settings = null; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + result.IsArgumentNullException("settings"); + } + + [Fact] + public void Should_Add_Mandatory_Arguments() + { + // Given + var fixture = new NpmSetToolFixture(); + fixture.Settings.Key = "progress"; + fixture.Settings.Value = "false"; + + // When + var result = fixture.Run(); + + // Then + result.Args.ShouldStartWith("set"); + } + + [Theory] + [InlineData("progress", "false", false, "\"progress\"=\"false\"")] + [InlineData("progress", "false", true, "\"progress\"=\"false\" -g")] + [InlineData("progress state", "hello world", true, "\"progress state\"=\"hello world\" -g")] + public void Should_Create_Set_Command_Arguments(string key, string value, bool global, string expected) + { + // Given + var fixture = new NpmSetToolFixture(); + fixture.Settings.Key = key; + fixture.Settings.Value = value; + fixture.Settings.Global = global; + + // When + var result = fixture.Run(); + + // Then + result.Args.ShouldEndWith(expected); + } + + } + } +} diff --git a/src/Cake.Npm.Tests/Update/NpmUpdateToolFixture.cs b/src/Cake.Npm.Tests/Update/NpmUpdateToolFixture.cs new file mode 100644 index 0000000..12191b7 --- /dev/null +++ b/src/Cake.Npm.Tests/Update/NpmUpdateToolFixture.cs @@ -0,0 +1,17 @@ +using Cake.Npm.Update; + +namespace Cake.Npm.Tests.Update +{ + internal sealed class NpmUpdateToolFixture : NpmFixture + { + public NpmUpdateToolFixture() + { + } + + protected override void RunTool() + { + var tool = new NpmUpdateTool(FileSystem, Environment, ProcessRunner, Tools, Log); + tool.Update(Settings); + } + } +} diff --git a/src/Cake.Npm.Tests/Update/NpmUpdateToolTests.cs b/src/Cake.Npm.Tests/Update/NpmUpdateToolTests.cs new file mode 100644 index 0000000..0f8df5d --- /dev/null +++ b/src/Cake.Npm.Tests/Update/NpmUpdateToolTests.cs @@ -0,0 +1,64 @@ +using Cake.Npm.Update; +using Xunit; + +namespace Cake.Npm.Tests.Update +{ + public class NpmUpdateToolTests + { + public sealed class TheUpdateMethod + { + [Fact] + public void Should_Redirect_Standard_Error() + { + var fixture = new NpmUpdateToolFixture(); + fixture.Settings.RedirectStandardError = true; + + var result = fixture.Run(); + + Assert.True(result.Process.RedirectStandardError); + } + + [Fact] + public void Should_Throw_If_Settings_Are_Null() + { + // Given + var fixture = new NpmUpdateToolFixture(); + fixture.Settings = null; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + result.IsArgumentNullException("settings"); + } + + [Fact] + public void Should_Add_Mandatory_Arguments() + { + // Given + var fixture = new NpmUpdateToolFixture(); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("update", result.Args); + } + + [Fact] + public void Should_Include_Global_Flag() + { + // Given + var fixture = new NpmUpdateToolFixture(); + fixture.Settings.UpdateGlobalPackages(); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("update -g", result.Args); + } + + } + } +} diff --git a/src/Cake.Npm.Tests/Version/NpmVersionToolFixture.cs b/src/Cake.Npm.Tests/Version/NpmVersionToolFixture.cs new file mode 100644 index 0000000..f0b42da --- /dev/null +++ b/src/Cake.Npm.Tests/Version/NpmVersionToolFixture.cs @@ -0,0 +1,20 @@ +using System.Text; +using Cake.Npm.Version; + +namespace Cake.Npm.Tests.Version +{ + internal sealed class NpmVersionToolFixture : NpmFixture + { + public NpmVersionToolFixture() + { + } + + protected override void RunTool() + { + var tool = new NpmVersionTool(FileSystem, Environment, ProcessRunner, Tools, Log); + Version = tool.Version(Settings); + } + + public string Version { get; private set; } + } +} diff --git a/src/Cake.Npm.Tests/Version/NpmVersionToolTests.cs b/src/Cake.Npm.Tests/Version/NpmVersionToolTests.cs new file mode 100644 index 0000000..f1ec8b3 --- /dev/null +++ b/src/Cake.Npm.Tests/Version/NpmVersionToolTests.cs @@ -0,0 +1,102 @@ +using Cake.Core; +using Shouldly; +using Xunit; + +namespace Cake.Npm.Tests.Version +{ + public class NpmVersionToolTests + { + public sealed class TheVersionMethod + { + [Fact] + public void Should_Throw_If_Settings_Are_Null() + { + // Given + var fixture = new NpmVersionToolFixture(); + fixture.Settings = null; + + // When + var result = Record.Exception(() => fixture.Run()); + + // Then + result.IsArgumentNullException("settings"); + } + + [Fact] + public void Should_Add_Mandatory_Arguments() + { + // Given + var fixture = new NpmVersionToolFixture(); + + // When + var result = fixture.Run(); + + // Then + Assert.Equal("version", result.Args); + } + + [Fact] + public void Should_Determine_Version_From_StandardOutput() + { + string[] versionInfo = new[] + { + "{", + " npm: '5.8.0',", + " ares: '1.10.1-DEV',", + " cldr: '32.0',", + " http_parser: '2.8.0',", + " icu: '60.1',", + " modules: '57',", + " nghttp2: '1.25.0',", + " node: '8.11.1',", + " openssl: '1.0.2o',", + " tz: '2017c',", + " unicode: '10.0',", + " uv: '1.19.1',", + " v8: '6.2.414.50',", + " zlib: '1.2.11'", + "}", +"", +"", + "╭─────────────────────────────────────╮", + "│ │", + "│ Update available 5.6.0 → 6.1.0 │", + "│ Run npm i -g npm to update │", + "│ │", + "╰─────────────────────────────────────╯" + }; + + // Given + var fixture = new NpmVersionToolFixture(); + fixture.ProcessRunner.Process.SetStandardOutput(versionInfo); + + // When + var result = fixture.Run(); + + // Then + fixture.Version.ShouldBe("5.8.0"); + } + + [Theory] + [InlineData("{npm: '1.0.0'}", "1.0.0")] + [InlineData("{ npm: '1.0.0',\r\n{stuff: '0.0.1' }", "1.0.0")] + [InlineData("", "")] + [InlineData(null, "")] + [InlineData("not valid", "")] + public void Should_Determine_Version_From_StandardOutput_Scenarios(string standardOutput, string expectedVersion) + { + string[] output = standardOutput.SplitLines(); + + // Given + var fixture = new NpmVersionToolFixture(); + fixture.ProcessRunner.Process.SetStandardOutput(output); + + // When + var result = fixture.Run(); + + // Then + fixture.Version.ShouldBe(expectedVersion); + } + } + } +} diff --git a/src/Cake.Npm/Cake.Npm.csproj b/src/Cake.Npm/Cake.Npm.csproj index 8f1f138..3e60449 100644 --- a/src/Cake.Npm/Cake.Npm.csproj +++ b/src/Cake.Npm/Cake.Npm.csproj @@ -1,10 +1,9 @@  - net46;netstandard2.0 + netstandard2.0 Cake.Npm Cake.Npm - 2.0 false false false @@ -24,24 +23,13 @@ False - - bin\Debug\net46\Cake.Npm.xml - - - - bin\Release\net46\Cake.Npm.xml - - - + bin\Debug\netstandard2.0\Cake.Npm.xml - - - bin\Release\netstandard2.0\Cake.Npm.xml - + - + All diff --git a/src/Cake.Npm/Ci/NpmCiSettings.cs b/src/Cake.Npm/Ci/NpmCiSettings.cs index 95d26fa..19e1103 100644 --- a/src/Cake.Npm/Ci/NpmCiSettings.cs +++ b/src/Cake.Npm/Ci/NpmCiSettings.cs @@ -1,4 +1,7 @@ -namespace Cake.Npm.Ci +using Cake.Core; +using Cake.Core.IO; + +namespace Cake.Npm.Ci { /// /// Contains settings used by . @@ -12,5 +15,21 @@ public NpmCiSettings() : base("ci") { } + + /// + /// Gets a value indicating whether npm should ignore modules listed in devDependencies. + /// + public bool Production { get; set; } + + /// + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + if (Production) + { + args.Append("--production"); + } + } } } diff --git a/src/Cake.Npm/Ci/NpmCiSettingsExtensions.cs b/src/Cake.Npm/Ci/NpmCiSettingsExtensions.cs new file mode 100644 index 0000000..6e9ae67 --- /dev/null +++ b/src/Cake.Npm/Ci/NpmCiSettingsExtensions.cs @@ -0,0 +1,30 @@ +using Cake.Npm.Ci; + +namespace Cake.Npm.Ci +{ + using System; + using Core; + + /// + /// Extensions for . + /// + public static class NpmCiSettingsExtensions + { + + /// + /// Defines that npm should ignore modules listed in devDependencies. + /// + /// The settings. + /// The instance with set to true. + public static NpmCiSettings ForProduction(this NpmCiSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + settings.Production = true; + return settings; + } + } +} diff --git a/src/Cake.Npm/Namespaces.cs b/src/Cake.Npm/Namespaces.cs index 8334205..fe1572b 100644 --- a/src/Cake.Npm/Namespaces.cs +++ b/src/Cake.Npm/Namespaces.cs @@ -59,3 +59,51 @@ internal class NamespaceDoc { } } + +// ReSharper disable once CheckNamespace +namespace Cake.Npm.Ci +{ + /// + /// This namespace contain types used for install npm packages (using CI commands). + /// + [CompilerGenerated] + internal class NamespaceDoc + { + } +} + +// ReSharper disable once CheckNamespace +namespace Cake.Npm.Set +{ + /// + /// This namespace contain types used for managing npm config settings. + /// + [CompilerGenerated] + internal class NamespaceDoc + { + } +} + +// ReSharper disable once CheckNamespace +namespace Cake.Npm.Version +{ + /// + /// This namespace contain types used for determining installed npm version. + /// + [CompilerGenerated] + internal class NamespaceDoc + { + } +} + +// ReSharper disable once CheckNamespace +namespace Cake.Npm.Update +{ + /// + /// This namespace contain types used for updating npm packages. + /// + [CompilerGenerated] + internal class NamespaceDoc + { + } +} \ No newline at end of file diff --git a/src/Cake.Npm/NpmSetAliases.cs b/src/Cake.Npm/NpmSetAliases.cs new file mode 100644 index 0000000..70869f4 --- /dev/null +++ b/src/Cake.Npm/NpmSetAliases.cs @@ -0,0 +1,82 @@ +namespace Cake.Npm +{ + using System; + using Cake.Npm.Set; + using Core; + using Core.Annotations; + + /// + /// Npm Set aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm.Set")] + public static class NpmSetAliases + { + /// + /// Sets an npm configuration setting. + /// + /// The context. + /// The key + /// The value + /// Set globally + /// + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Set")] + public static void NpmSet(this ICakeContext context, string key, string value, bool global = false) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + context.NpmSet(new NpmSetSettings() + { + Key = key, + Value = value, + Global = global + }); + } + + /// + /// Sets an npm configuration setting. + /// + /// The context. + /// The settings. + /// + /// Use speSetfic log level ('npm Set') + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Set")] + public static void NpmSet(this ICakeContext context, NpmSetSettings settings) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + AddinInformation.LogVersionInformation(context.Log); + var tool = new NpmSetTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log); + + tool.Set(settings); + } + } +} \ No newline at end of file diff --git a/src/Cake.Npm/NpmUpdateAliases.cs b/src/Cake.Npm/NpmUpdateAliases.cs new file mode 100644 index 0000000..6ef192f --- /dev/null +++ b/src/Cake.Npm/NpmUpdateAliases.cs @@ -0,0 +1,71 @@ +namespace Cake.Npm +{ + using System; + using Cake.Npm.Update; + using Core; + using Core.Annotations; + + /// + /// Npm Update aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm.Update")] + public static class NpmUpdateAliases + { + /// + /// Updates all packages for the project in the current working directory. + /// + /// The context. + /// + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Update")] + public static void NpmUpdate(this ICakeContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + context.NpmUpdate(new NpmUpdateSettings()); + } + + /// + /// Updates all packages for the project in the current working directory. + /// + /// The context. + /// The settings. + /// + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Update")] + public static void NpmUpdate(this ICakeContext context, NpmUpdateSettings settings) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + AddinInformation.LogVersionInformation(context.Log); + var tool = new NpmUpdateTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log); + tool.Update(settings); + } + } +} \ No newline at end of file diff --git a/src/Cake.Npm/NpmVersionAliases.cs b/src/Cake.Npm/NpmVersionAliases.cs new file mode 100644 index 0000000..6b16551 --- /dev/null +++ b/src/Cake.Npm/NpmVersionAliases.cs @@ -0,0 +1,40 @@ +namespace Cake.Npm +{ + using System; + using Cake.Npm.Version; + using Core; + using Core.Annotations; + + /// + /// Npm Version aliases + /// + [CakeAliasCategory("Npm")] + [CakeNamespaceImport("Cake.Npm.Version")] + public static class NpmVersionAliases + { + /// + /// Versions all packages for the project in the current working directory. + /// + /// The context. + /// + /// + /// + /// + /// + [CakeMethodAlias] + [CakeAliasCategory("Version")] + public static string NpmVersion(this ICakeContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + AddinInformation.LogVersionInformation(context.Log); + var settings =new NpmVersionSettings(); + return new NpmVersionTool(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log).Version(settings); + } + } +} \ No newline at end of file diff --git a/src/Cake.Npm/Set/NpmSetSettings.cs b/src/Cake.Npm/Set/NpmSetSettings.cs new file mode 100644 index 0000000..189982e --- /dev/null +++ b/src/Cake.Npm/Set/NpmSetSettings.cs @@ -0,0 +1,58 @@ +using Cake.Core; +using Cake.Core.IO; +using System; + +namespace Cake.Npm.Set +{ + /// + /// Contains settings used by . + /// + public class NpmSetSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmSetSettings() + : base("set") + { + } + + /// + /// The config key + /// + public string Key { get; set; } + + /// + /// The config value + /// + public string Value { get; set; } + + /// + /// Determines whether to set the config key value globally + /// + public bool Global { get; set; } + + /// + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + if (string.IsNullOrWhiteSpace(Key)) + { + throw new ArgumentNullException(nameof(Key)); + } + + if (string.IsNullOrWhiteSpace(Value)) + { + throw new ArgumentNullException(nameof(Value)); + } + + args.Append($"\"{Key}\"=\"{Value}\""); + + if (Global) + { + args.Append("-g"); + } + + base.EvaluateCore(args); + } + } +} diff --git a/src/Cake.Npm/Set/NpmSetTool.cs b/src/Cake.Npm/Set/NpmSetTool.cs new file mode 100644 index 0000000..3dc00dd --- /dev/null +++ b/src/Cake.Npm/Set/NpmSetTool.cs @@ -0,0 +1,48 @@ +using System.Linq; + +namespace Cake.Npm.Set +{ + using System; + using Core; + using Core.Diagnostics; + using Core.IO; + using Core.Tooling; + + /// + /// Tool for installing all npm packages for a project from package-lock.json. + /// + public class NpmSetTool : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmSetTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Sets a configuration key/value + /// + /// The settings + public void Set(NpmSetSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + RunCore(settings); + } + } +} diff --git a/src/Cake.Npm/Update/NpmUpdateSettings.cs b/src/Cake.Npm/Update/NpmUpdateSettings.cs new file mode 100644 index 0000000..84e3cfd --- /dev/null +++ b/src/Cake.Npm/Update/NpmUpdateSettings.cs @@ -0,0 +1,35 @@ +using Cake.Core; +using Cake.Core.IO; + +namespace Cake.Npm.Update +{ + /// + /// Contains settings used by . + /// + public class NpmUpdateSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmUpdateSettings() + : base("update") + { + } + + /// + /// Gets a value indicating whether to update globally installed packages. + /// + public bool Global { get; internal set; } + + /// + protected override void EvaluateCore(ProcessArgumentBuilder args) + { + base.EvaluateCore(args); + + if (Global) + { + args.Append("-g"); + } + } + } +} diff --git a/src/Cake.Npm/Update/NpmUpdateSettingsExtensions.cs b/src/Cake.Npm/Update/NpmUpdateSettingsExtensions.cs new file mode 100644 index 0000000..b633596 --- /dev/null +++ b/src/Cake.Npm/Update/NpmUpdateSettingsExtensions.cs @@ -0,0 +1,29 @@ +using Cake.Npm.Update; + +namespace Cake.Npm.Update +{ + using System; + using Core; + + /// + /// Extensions for . + /// + public static class NpmUpdateSettingsExtensions + { + /// + /// Will update globally installed packages + /// + /// The settings. + /// The instance with set to true. + public static NpmUpdateSettings UpdateGlobalPackages(this NpmUpdateSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + settings.Global = true; + return settings; + } + } +} diff --git a/src/Cake.Npm/Update/NpmUpdateTool.cs b/src/Cake.Npm/Update/NpmUpdateTool.cs new file mode 100644 index 0000000..70cc848 --- /dev/null +++ b/src/Cake.Npm/Update/NpmUpdateTool.cs @@ -0,0 +1,46 @@ +namespace Cake.Npm.Update +{ + using System; + using Core; + using Core.Diagnostics; + using Core.IO; + using Core.Tooling; + + /// + /// Tool for installing all npm packages for a project from package-lock.json. + /// + public class NpmUpdateTool : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmUpdateTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Updates all npm packages. + /// + /// The settings. + public void Update(NpmUpdateSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + RunCore(settings); + } + } +} diff --git a/src/Cake.Npm/Version/NpmVersionSettings.cs b/src/Cake.Npm/Version/NpmVersionSettings.cs new file mode 100644 index 0000000..52159ca --- /dev/null +++ b/src/Cake.Npm/Version/NpmVersionSettings.cs @@ -0,0 +1,19 @@ +using Cake.Core; +using Cake.Core.IO; + +namespace Cake.Npm.Version +{ + /// + /// Contains settings used by . + /// + public class NpmVersionSettings : NpmSettings + { + /// + /// Initializes a new instance of the class. + /// + public NpmVersionSettings() + : base("version") + { + } + } +} diff --git a/src/Cake.Npm/Version/NpmVersionTool.cs b/src/Cake.Npm/Version/NpmVersionTool.cs new file mode 100644 index 0000000..877abb0 --- /dev/null +++ b/src/Cake.Npm/Version/NpmVersionTool.cs @@ -0,0 +1,65 @@ +using System.Linq; +using System.Text.RegularExpressions; + +namespace Cake.Npm.Version +{ + using System; + using Core; + using Core.Diagnostics; + using Core.IO; + using Core.Tooling; + + /// + /// Tool for installing all npm packages for a project from package-lock.json. + /// + public class NpmVersionTool : NpmTool + { + /// + /// Initializes a new instance of the class. + /// + /// The file system. + /// The environment. + /// The process runner. + /// The tool locator. + /// Cake log instance. + public NpmVersionTool( + IFileSystem fileSystem, + ICakeEnvironment environment, + IProcessRunner processRunner, + IToolLocator tools, + ICakeLog log) + : base(fileSystem, environment, processRunner, tools, log) + { + } + + /// + /// Installs all npm packages from the specified settings. + /// + /// The settings. + public string Version(NpmVersionSettings settings) + { + if (settings == null) + { + throw new ArgumentNullException(nameof(settings)); + } + + var versionString = string.Empty; + + RunCore(settings, new ProcessSettings(), process => + { + var processOutput = process.GetStandardOutput(); + if (processOutput?.Any() ?? false) + { + var output = string.Join(Environment.NewLine, processOutput); + var match = Regex.Match(output, "(?npm): '(?.*)'"); + if (match.Success) + { + versionString = match.Groups["version"].Value; + } + } + }); + + return versionString; + } + } +}