Browse Source

theme remove from import on standalone app

pull/22645/head
erdemcaygor 1 year ago
parent
commit
ef98505067
  1. 30
      npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts

30
npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts

@ -15,7 +15,8 @@ import {
} from '../../utils'; } from '../../utils';
import { ThemeOptionsEnum } from './theme-options.enum'; import { ThemeOptionsEnum } from './theme-options.enum';
import { findNodes, getDecoratorMetadata, getMetadataField } from '../../utils/angular/ast-utils'; import { findNodes, getDecoratorMetadata, getMetadataField } from '../../utils/angular/ast-utils';
import { getMainFilePath } from '../../utils/angular/standalone/util'; import { findBootstrapApplicationCall, getMainFilePath } from '../../utils/angular/standalone/util';
import { findAppConfig } from '../../utils/angular/standalone/app_config';
export default function (_options: ChangeThemeOptions): Rule { export default function (_options: ChangeThemeOptions): Rule {
return async () => { return async () => {
@ -65,11 +66,10 @@ function updateProjectStyle(
function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsEnum): Rule { function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsEnum): Rule {
return async (host: Tree) => { return async (host: Tree) => {
const mainFilePath = await getMainFilePath(host, selectedProject); const mainFilePath = await getMainFilePath(host, selectedProject);
console.log('main file path --->>>>>', mainFilePath);
const isStandalone = isStandaloneApp(host, mainFilePath); const isStandalone = isStandaloneApp(host, mainFilePath);
console.log('isStandalone --->>>>>', isStandalone); const appModulePath = isStandalone
const appModulePath = isStandalone ? mainFilePath : getAppModulePath(host, mainFilePath); ? getAppConfigPath(host, mainFilePath)
console.log('app module path --->>>>>', appModulePath); : getAppModulePath(host, mainFilePath);
return chain([ return chain([
removeImportPath(appModulePath, targetThemeName), removeImportPath(appModulePath, targetThemeName),
@ -81,16 +81,16 @@ function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsE
}; };
} }
export function removeImportPath(appModulePath: string, selectedTheme: ThemeOptionsEnum): Rule { export function removeImportPath(filePath: string, selectedTheme: ThemeOptionsEnum): Rule {
return (host: Tree) => { return (host: Tree) => {
const buffer = host.read(appModulePath); const buffer = host.read(filePath);
if (!buffer) return host; if (!buffer) return host;
const sourceText = buffer.toString('utf-8'); const sourceText = buffer.toString('utf-8');
const source = ts.createSourceFile(appModulePath, sourceText, ts.ScriptTarget.Latest, true); const source = ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.Latest, true);
const recorder = host.beginUpdate(appModulePath); const recorder = host.beginUpdate(filePath);
const impMap = getImportPaths(selectedTheme, true); const impMap = getImportPaths(selectedTheme, true); // all theme-related imports
const nodes = findNodes(source, ts.isImportDeclaration); const nodes = findNodes(source, ts.isImportDeclaration);
@ -99,9 +99,9 @@ export function removeImportPath(appModulePath: string, selectedTheme: ThemeOpti
const namedBindings = node.importClause?.namedBindings; const namedBindings = node.importClause?.namedBindings;
return impMap.some(({ path, importName }) => { return impMap.some(({ path, importName }) => {
const symbol = importName.split('.')[0]; // remove .forRoot() const symbol = importName.split('.')[0]; // Ex: ThemeXModule from ThemeXModule.forRoot()
const matchesPath = !!path && importPath === path;
const matchesPath = path && importPath === path;
const matchesSymbol = const matchesSymbol =
!!namedBindings && !!namedBindings &&
ts.isNamedImports(namedBindings) && ts.isNamedImports(namedBindings) &&
@ -300,3 +300,9 @@ export const styleCompareFn = (item1: string | object, item2: string | object) =
return o1.bundleName && o2.bundleName && o1.bundleName == o2.bundleName; return o1.bundleName && o2.bundleName && o1.bundleName == o2.bundleName;
}; };
export const getAppConfigPath = (host: Tree, mainFilePath: string): string => {
const bootstrapCall = findBootstrapApplicationCall(host, mainFilePath);
const appConfig = findAppConfig(bootstrapCall, host, mainFilePath);
return appConfig?.filePath || '';
};

Loading…
Cancel
Save