From a9ad98d50924a30b127ee628869ccde5fe369f72 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 7 Apr 2020 15:53:33 +0300 Subject: [PATCH] Create .npmrc file when run abp switch-to-preview command cont. https://github.com/abpframework/abp/issues/3258 --- .../ProjectModification/NpmPackagesUpdater.cs | 56 ++++++++++++++++++- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs index 280af81687..c3434e6e27 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Volo.Abp.Cli.Http; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; @@ -54,7 +55,7 @@ namespace Volo.Abp.Cli.ProjectModification if (IsAngularProject(fileDirectory)) { - CreateNpmrcFile(Path.GetDirectoryName(file)); + await CreateNpmrcFileAsync(Path.GetDirectoryName(file)); } RunYarn(fileDirectory); @@ -68,21 +69,70 @@ namespace Volo.Abp.Cli.ProjectModification } } - private void CreateNpmrcFile(string directoryName) + private async Task CreateNpmrcFileAsync(string directoryName) { var fileName = Path.Combine(directoryName, ".npmrc"); + var abpRegistry = "@abp:registry:https://www.myget.org/F/abp-nightly/npm"; + var voloRegistry = await GetVoloRegistryAsync(); + if (File.Exists(fileName)) { + var fileContent = File.ReadAllText(fileName); + + if (!fileContent.Contains(abpRegistry)) + { + fileContent += Environment.NewLine + abpRegistry; + } + + if (!fileContent.Contains(voloRegistry)) + { + fileContent += Environment.NewLine + voloRegistry; + } + + File.WriteAllText(fileName, fileContent); + return; } using var fs = File.Create(fileName); - var content = new UTF8Encoding(true).GetBytes("@abp:registry:https://www.myget.org/F/abp-nightly/npm"); + var content = new UTF8Encoding(true) + .GetBytes(abpRegistry + Environment.NewLine + voloRegistry); fs.Write(content, 0, content.Length); } + private async Task GetVoloRegistryAsync() + { + var apikey = await GetApiKeyAsync(); + + if (string.IsNullOrWhiteSpace(apikey)) + { + return ""; + } + + return "@volo:registry=https://www.myget.org/F/abp-commercial/auth/" + apikey + "/npm/"; + } + + public async Task GetApiKeyAsync() + { + try + { + using (var client = new CliHttpClient(TimeSpan.FromMinutes(1))) + { + var responseMessage = await client.GetAsync( + $"{CliUrls.WwwAbpIo}api/myget/apikey/" + ); + + return Encoding.Default.GetString(await responseMessage.Content.ReadAsByteArrayAsync()); + } + } + catch (Exception) + { + return ""; + } + } + private bool IsAngularProject(string fileDirectory) { return File.Exists(Path.Combine(fileDirectory, "angular.json"));