Skip to content

Commit 4cef4d7

Browse files
committed
update index.html when theme changed
1 parent 9c94522 commit 4cef4d7

File tree

1 file changed

+38
-0
lines changed
  • npm/ng-packs/packages/schematics/src/commands/change-theme

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
getAppConfigPath,
1616
cleanEmptyExprFromModule,
1717
cleanEmptyExprFromProviders,
18+
getWorkspace,
1819
} from '../../utils';
1920
import { ThemeOptionsEnum } from './theme-options.enum';
2021
import { findNodes, getDecoratorMetadata, getMetadataField } from '../../utils/angular/ast-utils';
@@ -82,6 +83,7 @@ function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsE
8283
insertImports(selectedProject, targetThemeName),
8384
insertProviders(selectedProject, targetThemeName),
8485
adjustProvideAbpThemeShared(appModulePath, targetThemeName),
86+
updateIndexHtml(selectedProject, targetThemeName),
8587
formatFile(appModulePath),
8688
cleanEmptyExpressions(appModulePath, isStandalone),
8789
]);
@@ -534,3 +536,39 @@ function findProvideAbpThemeSharedCalls(source: ts.SourceFile): ts.CallExpressio
534536

535537
return result;
536538
}
539+
540+
export function updateIndexHtml(projectName: string, themeName: ThemeOptionsEnum): Rule {
541+
return async (host: Tree) => {
542+
const workspace = await getWorkspace(host);
543+
const project = workspace.projects.get(projectName);
544+
545+
if (!project) {
546+
throw new Error(`Project "${projectName}" not found in workspace.`);
547+
}
548+
549+
const buildOptions = project.targets.get('build')?.options;
550+
const indexPath = buildOptions?.index as string;
551+
552+
if (!indexPath || !host.exists(indexPath)) {
553+
throw new Error(`index.html not found at path: ${indexPath}`);
554+
}
555+
556+
const buffer = host.read(indexPath);
557+
if (!buffer) return;
558+
const content = buffer.toString('utf-8');
559+
560+
const loaderDiv = `<div id="lp-page-loader"></div>`;
561+
let updatedContent = content;
562+
563+
if (themeName === ThemeOptionsEnum.LeptonX) {
564+
if (!content.includes(loaderDiv)) {
565+
updatedContent = content.replace(/<body([^>]*)>/i, `<body$1>\n ${loaderDiv}`);
566+
}
567+
} else {
568+
if (content.includes(loaderDiv)) {
569+
updatedContent = content.replace(loaderDiv, '');
570+
}
571+
}
572+
host.overwrite(indexPath, updatedContent);
573+
};
574+
}

0 commit comments

Comments
 (0)