Refactor change-theme command

pull/17732/head
Masum ULU 1 year ago
parent d8f4bc1906
commit 929f637ae5

@ -4,7 +4,14 @@ import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
import * as ts from 'typescript';
import { allStyles, importMap, styleMap } from './style-map';
import { ChangeThemeOptions } from './model';
import { Change, InsertChange, isLibrary, updateWorkspace, WorkspaceDefinition } from '../../utils';
import {
Change,
createDefaultPath,
InsertChange,
isLibrary,
updateWorkspace,
WorkspaceDefinition,
} from '../../utils';
import { ThemeOptionsEnum } from './theme-options.enum';
import {
addImportToModule,
@ -59,20 +66,8 @@ function updateProjectStyle(
}
function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsEnum): Rule {
return (host: Tree) => {
const angularJSON = host.read('angular.json');
if (!angularJSON) {
throw new SchematicsException('The angular.json does not found');
}
const workspace = JSON.parse(angularJSON.toString());
const project = workspace.projects[selectedProject];
if (!project || !project.sourceRoot) {
throw new SchematicsException('The target project does not found');
}
const appModulePath = project.sourceRoot + '/app/app.module.ts';
return async (host: Tree) => {
const appModulePath = (await createDefaultPath(host, selectedProject)) + '/app.module.ts';
return chain([
removeImportPath(appModulePath, targetThemeName),
@ -85,21 +80,13 @@ function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsE
function removeImportPath(appModulePath: string, selectedTheme: ThemeOptionsEnum): Rule {
return (host: Tree) => {
const recorder = host.beginUpdate(appModulePath);
const sourceText = host.read(appModulePath)?.toString('utf-8');
const source = ts.createSourceFile(
appModulePath,
sourceText!,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
const impMap = Array.from(importMap.values())
.filter(f => f !== importMap.get(selectedTheme))
.reduce((acc, val) => [...acc, ...val], []);
const source = getSource(host, appModulePath);
const impMap = getImportPaths(selectedTheme);
const nodes = findNodes(source, ts.isImportDeclaration);
const filteredNodes = nodes.filter(n => impMap.some(f => n.getFullText().match(f.path)));
if (!filteredNodes || filteredNodes.length < 1) {
if (filteredNodes?.length < 1) {
return;
}
@ -118,17 +105,8 @@ function removeImportFromNgModuleMetadata(
): Rule {
return (host: Tree) => {
const recorder = host.beginUpdate(appModulePath);
const sourceText = host.read(appModulePath)?.toString('utf-8');
const source = ts.createSourceFile(
appModulePath,
sourceText!,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
const impMap = Array.from(importMap.values())
.filter(f => f !== importMap.get(selectedTheme))
.reduce((acc, val) => [...acc, ...val], []);
const source = getSource(host, appModulePath);
const impMap = getImportPaths(selectedTheme);
const node = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] || {};
if (!node) {
@ -136,7 +114,6 @@ function removeImportFromNgModuleMetadata(
}
const matchingProperties = getMetadataField(node as ts.ObjectLiteralExpression, 'imports');
const assignment = matchingProperties[0] as ts.PropertyAssignment;
const assignmentInit = assignment.initializer as ts.ArrayLiteralExpression;
@ -163,14 +140,7 @@ function removeImportFromNgModuleMetadata(
function insertImports(appModulePath: string, selectedTheme: ThemeOptionsEnum): Rule {
return (host: Tree) => {
const recorder = host.beginUpdate(appModulePath);
const sourceText = host.read(appModulePath)?.toString('utf-8');
const source = ts.createSourceFile(
appModulePath,
sourceText!,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
const source = getSource(host, appModulePath);
const selected = importMap.get(selectedTheme);
const changes: Change[] = [];
@ -190,6 +160,25 @@ function insertImports(appModulePath: string, selectedTheme: ThemeOptionsEnum):
};
}
function getSource(host: Tree, appModulePath: string): ts.SourceFile {
const sourceText = host.read(appModulePath)?.toString('utf-8');
const source = ts.createSourceFile(
appModulePath,
sourceText!,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
return source;
}
function getImportPaths(selectedTheme: ThemeOptionsEnum) {
return Array.from(importMap.values())
.filter(f => f !== importMap.get(selectedTheme))
.reduce((acc, val) => [...acc, ...val], []);
}
export function getProjectTargetOptions(
project: ProjectDefinition,
buildTarget: string,

Loading…
Cancel
Save