From 26b6c84ee88e32aa95e80284c8c631c0afa6b135 Mon Sep 17 00:00:00 2001 From: erdemcaygor Date: Wed, 16 Apr 2025 17:23:11 +0300 Subject: [PATCH] change theme command refactoring --- .../src/commands/change-theme/index.ts | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts b/npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts index 48e2ad1008..4f70d5ea29 100644 --- a/npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts @@ -73,8 +73,10 @@ function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsE return chain([ removeImportPath(appModulePath, targetThemeName), - removeImportFromNgModuleMetadata(appModulePath, targetThemeName), - removeProviderFromNgModuleMetadata(appModulePath, targetThemeName), + ...(!isStandalone ? [removeImportFromNgModuleMetadata(appModulePath, targetThemeName)] : []), + isStandalone + ? removeImportsFromStandaloneProviders(appModulePath, targetThemeName) + : removeProviderFromNgModuleMetadata(appModulePath, targetThemeName), insertImports(selectedProject, targetThemeName), insertProviders(selectedProject, targetThemeName), ]); @@ -159,6 +161,38 @@ export function removeImportFromNgModuleMetadata( }; } +export function removeImportsFromStandaloneProviders( + mainPath: string, + selectedTheme: ThemeOptionsEnum, +): Rule { + return (host: Tree) => { + const buffer = host.read(mainPath); + if (!buffer) return host; + + const sourceText = buffer.toString('utf-8'); + const source = ts.createSourceFile(mainPath, sourceText, ts.ScriptTarget.Latest, true); + console.log('remove from standalone main path --->>>>', mainPath); + const recorder = host.beginUpdate(mainPath); + + const impMap = getImportPaths(selectedTheme, true); + + const callExpressions = findNodes(source, ts.isCallExpression); + + for (const expr of callExpressions) { + const text = expr.getText(); + + const match = impMap.find(({ importName }) => text.includes(importName.split('.')[0])); + + if (match) { + recorder.remove(expr.getStart(), expr.getWidth() + 1); + } + } + + host.commitUpdate(recorder); + return host; + }; +} + export function removeProviderFromNgModuleMetadata( appModulePath: string, selectedTheme: ThemeOptionsEnum,