From 9343a9edf0776b7fe7fda2fafa94efafb9e9800a Mon Sep 17 00:00:00 2001 From: muhammedaltug Date: Mon, 26 Dec 2022 14:04:14 +0300 Subject: [PATCH] add functions for adding module to routing module and adding config module to app module --- .../karma.conf.js.template | 2 +- .../src/commands/create-lib/index.ts | 109 ++++++++++++++++-- .../src/commands/create-lib/schema.json | 15 ++- 3 files changed, 114 insertions(+), 12 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template b/npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template index a56984ac5b..e181d4088d 100644 --- a/npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template +++ b/npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template @@ -25,7 +25,7 @@ module.exports = function (config) { suppressAll: true // removes the duplicated traces }, coverageReporter: { - dir: require('path').join(__dirname, '../../coverage/my-project-name'), + dir: require('path').join(__dirname, '../../coverage/<%= kebab(libraryName) %>'), subdir: '.', reporters: [ { type: 'html' }, diff --git a/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts b/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts index e92800ddd5..2b92336c0e 100644 --- a/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts @@ -8,25 +8,31 @@ import { Tree, url, } from '@angular-devkit/schematics'; -import { GenerateLibSchema } from './models/generate-lib-schema'; +import * as ts from 'typescript'; + +import { join, normalize } from '@angular-devkit/core'; import { + addImportToModule, + addRouteDeclarationToModule, applyWithOverwrite, + camel, + getFirstApplication, getWorkspace, + InsertChange, interpolate, isLibrary, JSONFile, kebab, + pascal, + readWorkspaceSchema, resolveProject, updateWorkspace, } from '../../utils'; -import * as cases from '../../utils/text'; -import { Exception } from '../../enums'; -import { join, normalize } from '@angular-devkit/core'; -import { - ProjectDefinition, - WorkspaceDefinition, -} from '@angular-devkit/core/src/workspace/definitions'; +import { ProjectDefinition, WorkspaceDefinition } from '../../utils/angular/workspace'; import { addLibToWorkspaceFile } from '../../utils/angular-schematic/generate-lib'; +import * as cases from '../../utils/text'; +import { Exception } from '../../enums/exception'; +import { GenerateLibSchema } from './models/generate-lib-schema'; export default function (schema: GenerateLibSchema) { return async (tree: Tree) => { @@ -120,6 +126,8 @@ export function addLibToWorkspaceIfNotExist(name: string, packagesDir: string): : noop(), addLibToWorkspaceFile(projectRoot, packageName), updateTsConfig(packageName, pathImportLib), + importConfigModuleToDefaultProjectAppModule(workspace, packageName), + addRoutingToAppRoutingModule(workspace, packageName), ]); }; } @@ -134,7 +142,9 @@ export function updateTsConfig(packageName: string, path: string) { const file = new JSONFile(host, tsConfig); const jsonPath = ['compilerOptions', 'paths', packageName]; + const jsonPathConfig = ['compilerOptions', 'paths', `${packageName}/config`]; file.modify(jsonPath, [`${path}/src/public-api.ts`]); + file.modify(jsonPathConfig, [`${path}/config/src/public-api.ts`]); }; } @@ -154,3 +164,86 @@ export async function createLibSecondaryEntry(tree: Tree, options: GenerateLibSc ]), ]); } + +export function importConfigModuleToDefaultProjectAppModule( + workspace: WorkspaceDefinition, + packageName: string, +) { + return (tree: Tree) => { + const projectName = readWorkspaceSchema(tree).defaultProject || getFirstApplication(tree).name!; + const project = workspace.projects.get(projectName); + const appModulePath = `${project?.sourceRoot}/app/app.module.ts`; + const appModule = tree.read(appModulePath); + if (!appModule) { + return; + } + const appModuleContent = appModule.toString(); + if (appModuleContent.includes(`${camel(packageName)}ConfigModule`)) { + return; + } + + const forRootStatement = `${pascal(packageName)}ConfigModule.forRoot()`; + const text = tree.read(appModulePath); + if (!text) { + return; + } + const sourceText = text.toString(); + if (sourceText.includes(forRootStatement)) { + return; + } + const source = ts.createSourceFile(appModulePath, sourceText, ts.ScriptTarget.Latest, true); + + const changes = addImportToModule( + source, + appModulePath, + forRootStatement, + `${kebab(packageName)}/config`, + ); + const recorder = tree.beginUpdate(appModulePath); + for (const change of changes) { + if (change instanceof InsertChange) { + recorder.insertLeft(change.pos, change.toAdd); + } + } + tree.commitUpdate(recorder); + + return; + }; +} + +export function addRoutingToAppRoutingModule(workspace: WorkspaceDefinition, packageName: string) { + return (tree: Tree) => { + const projectName = readWorkspaceSchema(tree).defaultProject || getFirstApplication(tree).name!; + const project = workspace.projects.get(projectName); + const appRoutingModulePath = `${project?.sourceRoot}/app/app-routing.module.ts`; + const appRoutingModule = tree.read(appRoutingModulePath); + if (!appRoutingModule) { + return; + } + const appRoutingModuleContent = appRoutingModule.toString(); + const moduleName = `${pascal(packageName)}Module`; + if (appRoutingModuleContent.includes(moduleName)) { + return; + } + + const source = ts.createSourceFile( + appRoutingModulePath, + appRoutingModuleContent, + ts.ScriptTarget.Latest, + true, + ); + const importPath = `${kebab(packageName)}`; + const importStatement = `() => import('${importPath}').then(m => m.${moduleName}.forLazy())`; + const routeDefinition = `{ path: '${kebab(packageName)}', loadChildren: ${importStatement} }`; + const change = addRouteDeclarationToModule(source, `${kebab(packageName)}`, routeDefinition); + + const recorder = tree.beginUpdate(appRoutingModulePath); + if (change instanceof InsertChange) { + recorder.insertLeft(change.pos, change.toAdd); + } + tree.commitUpdate(recorder); + + return; + }; +} + diff --git a/npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json b/npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json index 64d64c1d80..19df1a5427 100644 --- a/npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json +++ b/npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json @@ -16,19 +16,28 @@ "isSecondaryEntrypoint": { "description": "Is secondary entrypoint", "type": "boolean", - "$default": false, + "$default": { + "$source": "argv", + "index": 1 + }, "x-prompt": "Is secondary entrypoint?" }, "isModuleTemplate": { "description": "Is module template", "type": "boolean", - "$default": true, + "$default": { + "$source": "argv", + "index": 2 + }, "x-prompt": "Is module template?" }, "override": { "description": "Override existing files", "type": "boolean", - "$default": false, + "$default": { + "$source": "argv", + "index": 3 + }, "x-prompt": "Override existing files?" } },