From 8e511b86c4b4957b004eabcded1dcfcb098a0500 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 23 Feb 2021 12:35:43 +0300 Subject: [PATCH] CLI: Create tsconfig.prod.json when run the abp add-module command resolves https://github.com/volosoft/volo/issues/5574 --- .../AngularModuleSourceCodeAdder.cs | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularModuleSourceCodeAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularModuleSourceCodeAdder.cs index e4e5f0bb2e..08912f2f25 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularModuleSourceCodeAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularModuleSourceCodeAdder.cs @@ -34,7 +34,8 @@ namespace Volo.Abp.Cli.ProjectModification } await AddPathsToTsConfigAsync(angularPath, angularProjectsPath, projects); - + await CreateTsConfigProdJsonAsync(angularPath); + await AddScriptsToPackageJsonAsync(angularPath); await AddProjectToAngularJsonAsync(angularPath, projects); } catch (Exception e) @@ -93,6 +94,61 @@ namespace Volo.Abp.Cli.ProjectModification File.WriteAllText(angularJsonFilePath, json.ToString(Formatting.Indented)); } + private async Task AddScriptsToPackageJsonAsync(string angularPath) + { + var packageJsonFilePath = Path.Combine(angularPath, "package.json"); + var fileContent = File.ReadAllText(packageJsonFilePath); + + var json = JObject.Parse(fileContent); + + var scriptsJobject = (JObject) json["scripts"]; + + if (scriptsJobject == null || scriptsJobject["postinstall"] != null || scriptsJobject["compile:ivy"] != null) + { + return; + } + + scriptsJobject["postinstall"] = "npm run compile:ivy"; + scriptsJobject["compile:ivy"] = "yarn ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points --tsconfig './tsconfig.prod.json' --source node_modules"; + + File.WriteAllText(packageJsonFilePath, json.ToString(Formatting.Indented)); + } + + private async Task CreateTsConfigProdJsonAsync(string angularPath) + { + var tsConfigProdJsonFilePath = Path.Combine(angularPath, "tsconfig.prod.json"); + + if (File.Exists(tsConfigProdJsonFilePath)) + { + return; + } + + var json = new JObject( + new JProperty("compileOnSave", false), + new JProperty("compilerOptions", new JObject( + new JProperty("baseUrl", "./"), + new JProperty("outDir", "./dist/out-tsc"), + new JProperty("sourceMap", true), + new JProperty("declaration", false), + new JProperty("downlevelIteration", true), + new JProperty("experimentalDecorators", true), + new JProperty("module", "esnext"), + new JProperty("moduleResolution", "node"), + new JProperty("importHelpers", true), + new JProperty("target", "es2015"), + new JProperty("typeRoots", new JArray(new JValue("node_modules/@types"))), + new JProperty("lib", new JArray(new JValue("es2018"), new JValue("dom"))), + new JProperty("types", new JArray(new JValue("jest"))) + )), + new JProperty("angularCompilerOptions", new JObject( + new JProperty("fullTemplateTypeCheck", true), + new JProperty("strictInjectionParameters", true) + )) + ); + + File.WriteAllText(tsConfigProdJsonFilePath, json.ToString(Formatting.Indented)); + } + private async Task AddPathsToTsConfigAsync(string angularPath, string angularProjectsPath, List projects) { @@ -105,7 +161,8 @@ namespace Volo.Abp.Cli.ProjectModification { var projectPackageName = await GetProjectPackageNameAsync(angularProjectsPath, project); - var publicApis = Directory.GetFiles(Path.Combine(angularProjectsPath, project), "*public-api.ts", SearchOption.AllDirectories) + var publicApis = Directory.GetFiles(Path.Combine(angularProjectsPath, project), "*public-api.ts", + SearchOption.AllDirectories) .Where(p => !p.Contains("\\node_modules\\")) .Select(p => p.RemovePreFix(angularPath).Replace("\\", "/").RemovePreFix("/")); @@ -171,8 +228,10 @@ namespace Volo.Abp.Cli.ProjectModification { continue; } + Directory.Move(folderUnderProject, Path.Combine(folder, Path.GetFileName(folderUnderProject))); } + projectsInFolder = Directory.GetDirectories(folder); }