Browse Source

Refactor change-theme command

pull/17732/head
Masum ULU 3 years ago
parent
commit
929f637ae5
  1. 83
      npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts

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

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

Loading…
Cancel
Save