diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularSourceCodeAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularSourceCodeAdder.cs index 84e995f1fd..48413da847 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularSourceCodeAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/AngularSourceCodeAdder.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -66,6 +67,12 @@ public class AngularSourceCodeAdder : ITransientDependency } } + public async Task AddModuleConfigurationAsync(string angularPath, string moduleName) + { + await AddProjectToEnvironmentTsAsync(angularPath, moduleName); + await AddProjectToAppModuleTsAsync(angularPath, moduleName); + } + private async Task AddProjectsToAngularJsonAsync(string angularPath, List projects) { var angularJsonFilePath = Path.Combine(angularPath, "angular.json"); @@ -229,6 +236,46 @@ public class AngularSourceCodeAdder : ITransientDependency File.WriteAllText(tsConfigPath, tsConfigAsJson.ToString(Formatting.Indented)); } + private async Task AddProjectToEnvironmentTsAsync(string angularPath, string moduleName) + { + var filePath = Path.Combine(angularPath, "src", "environments", "environment.ts"); + + if (!File.Exists(filePath)) + { + return; + } + + var fileContent = File.ReadAllText(filePath); + + fileContent = Regex.Replace(fileContent, @"apis\s*:\s*{", + "apis: {"+ Environment.NewLine + + " " + moduleName.Split(".").Last() + ":"+ Environment.NewLine + + " rootNamespace: '" + moduleName + "',"+ Environment.NewLine + + " },"); + + File.WriteAllText(filePath, fileContent); + } + + private async Task AddProjectToAppModuleTsAsync(string angularPath, string moduleName) + { + var filePath = Path.Combine(angularPath, "src", "app", "app.module.ts"); + + if (!File.Exists(filePath)) + { + return; + } + + var fileContent = File.ReadAllText(filePath); + + fileContent = "import { "+moduleName.Split(".").Last()+"Module } from '@"+moduleName.Split(".").Last().ToKebabCase()+"/config';" + Environment.NewLine + fileContent; + + fileContent = Regex.Replace(fileContent, "imports\\s*:\\s*\\[", + "imports: ["+ Environment.NewLine + + " " + moduleName.Split(".").Last() + "Module.forRoot(),"); + + File.WriteAllText(filePath, fileContent); + } + private async Task> CopyAndGetNamesOfAngularProjectsAsync(string solutionFilePath, string angularProjectsPath) { @@ -305,7 +352,7 @@ public class AngularSourceCodeAdder : ITransientDependency return projects; } - + private async Task GetProjectPackageNameAsync(string angularProjectsPath, string project) { var packageJsonPath = Path.Combine(angularProjectsPath, project, "package.json"); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs index ed90d16cfd..5d02ad7835 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs @@ -428,6 +428,11 @@ public class SolutionModuleAdder : ITransientDependency await PublishEventAsync(9, $"Adding angular source code"); await AngularSourceCodeAdder.AddFromModuleAsync(solutionFilePath, angularPath); + + if (newTemplate) + { + await AngularSourceCodeAdder.AddModuleConfigurationAsync(angularPath, moduleName); + } } private static void DeleteAngularDirectoriesInModulesFolder(string modulesFolderInSolution) diff --git a/npm/ng-packs/.prettierignore b/npm/ng-packs/.prettierignore index d0b804da2a..ab46b38bd5 100644 --- a/npm/ng-packs/.prettierignore +++ b/npm/ng-packs/.prettierignore @@ -2,3 +2,9 @@ /dist /coverage +/.angular +/.vscode +/node_modules +/scripts +/tools +/source-code-requirements \ No newline at end of file diff --git a/npm/ng-packs/README.md b/npm/ng-packs/README.md index 790a48fd0c..213362a938 100644 --- a/npm/ng-packs/README.md +++ b/npm/ng-packs/README.md @@ -22,4 +22,12 @@ The `dev-app` project is the same as the Angular UI template project. `dev-app` For more information, see the [docs.abp.io](https://docs.abp.io) If would you like contribute, see the [contribution guideline](./CONTRIBUTING.md). - + +### Git Hooks + There is a pre-defined git hook for Angular project. This hook is `pre-commit`. The hook run prettier for staged files. If you want to use this hook. (This steps tested for linux and Mac os) + + - Go to `npm/ng-packs` + - Run `chmod +x ./pre-commit` in terminal + - Run `git config core.hooksPath npm/ng-packs` in terminal + and now, when you create a git commit, git hook execute prettier for staged files. + \ No newline at end of file diff --git a/npm/ng-packs/package.json b/npm/ng-packs/package.json index caf8f6b67d..de2fc294cb 100644 --- a/npm/ng-packs/package.json +++ b/npm/ng-packs/package.json @@ -10,6 +10,7 @@ "build:all": "nx run-many --target=build --all --exclude=dev-app,schematics --prod && yarn build:schematics", "test": "ng test --detect-open-handles=true --run-in-band=true --watch-all=true", "test:all": "nx run-many --target=test --all", + "lint-staged": "lint-staged", "lint": "nx workspace-lint && ng lint", "lint:all": "nx run-many --target=lint --all", "e2e": "ng e2e", @@ -40,7 +41,6 @@ }, "private": true, "devDependencies": { - "@abp/utils": "~7.0.0-rc.2", "@angular-devkit/build-angular": "14.2.1", "@angular-devkit/build-ng-packagr": "^0.1002.0", "@angular-devkit/schematics-cli": "~14.2.1", @@ -105,6 +105,7 @@ "just-clone": "^3.2.1", "just-compare": "^1.4.0", "lerna": "^4.0.0", + "lint-staged": "^13.0.3", "ng-packagr": "14.2.1", "ng-zorro-antd": "^14.0.0", "nx": "14.7.5", @@ -125,5 +126,10 @@ "typescript": "4.7.4", "zone.js": "0.11.4" }, - "dependencies": {} + "dependencies": {}, + "lint-staged": { + "**/*.{js,jsx,ts,tsx}":[ + "npx prettier --write --config .prettierrc " + ] + } } diff --git a/npm/ng-packs/pre-commit b/npm/ng-packs/pre-commit new file mode 100755 index 0000000000..5000e1c00b --- /dev/null +++ b/npm/ng-packs/pre-commit @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +cd npm/ng-packs && npm run lint-staged \ No newline at end of file