Browse Source

refactor

pull/22645/head
erdemcaygor 1 year ago
parent
commit
8e90ffd868
  1. 174
      npm/ng-packs/packages/schematics/src/commands/change-theme/index.ts
  2. 6
      npm/ng-packs/packages/schematics/src/commands/change-theme/style-map.ts

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

@ -184,26 +184,71 @@ export function removeImportsFromStandaloneProviders(
for (const expr of callExpressions) { for (const expr of callExpressions) {
const exprText = expr.getText(); const exprText = expr.getText();
const match = impMap.find(({ importName, provider }) => { if (expr.expression.getText() === 'importProvidersFrom') {
const moduleSymbol = importName?.split('.')[0]; const arrayArg = expr.arguments[0];
return (
(moduleSymbol && exprText.includes(moduleSymbol)) || if (ts.isArrayLiteralExpression(arrayArg)) {
(provider && exprText.includes(provider)) const elements = arrayArg.elements;
);
}); const elementsToRemove = elements.filter(el =>
impMap.some(({ importName }) => el.getText().includes(importName)),
);
if (match) { if (elementsToRemove.length) {
const start = expr.getFullStart(); for (const removeEl of elementsToRemove) {
const end = expr.getEnd(); const start = removeEl.getFullStart();
const nextChar = sourceText.slice(end, end + 1); const end = removeEl.getEnd();
const prevChar = sourceText.slice(start - 1, start);
const nextChar = sourceText.slice(end, end + 1);
if (nextChar === ',') { const prevChar = sourceText.slice(start - 1, start);
recorder.remove(start, end - start + 1);
} else if (prevChar === ',') { if (nextChar === ',') {
recorder.remove(start - 1, end - start + 1); recorder.remove(start, end - start + 1);
} else { } else if (prevChar === ',') {
recorder.remove(start, end - start); recorder.remove(start - 1, end - start + 1);
} else {
recorder.remove(start, end - start);
}
}
}
const remaining = arrayArg.elements.filter(el => !elementsToRemove.includes(el));
if (remaining.length === 0) {
const start = expr.getFullStart();
const end = expr.getEnd();
const nextChar = sourceText.slice(end, end + 1);
const prevChar = sourceText.slice(start - 1, start);
if (nextChar === ',') {
recorder.remove(start, end - start + 1);
} else if (prevChar === ',') {
recorder.remove(start - 1, end - start + 1);
} else {
recorder.remove(start, end - start);
}
}
}
} else {
const match = impMap.find(({ importName, provider }) => {
const moduleSymbol = importName?.split('.')[0];
return (
(moduleSymbol && exprText.includes(moduleSymbol)) ||
(provider && exprText.includes(provider))
);
});
if (match) {
const start = expr.getFullStart();
const end = expr.getEnd();
const nextChar = sourceText.slice(end, end + 1);
const prevChar = sourceText.slice(start - 1, start);
if (nextChar === ',') {
recorder.remove(start, end - start + 1);
} else if (prevChar === ',') {
recorder.remove(start - 1, end - start + 1);
} else {
recorder.remove(start, end - start);
}
} }
} }
} }
@ -222,31 +267,67 @@ export function removeProviderFromNgModuleMetadata(
const source = createSourceFile(host, appModulePath); const source = createSourceFile(host, appModulePath);
const impMap = getImportPaths(selectedTheme, true); const impMap = getImportPaths(selectedTheme, true);
const node = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] || {}; const node = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0];
if (!node) { if (!node) {
throw new SchematicsException('The app module does not found'); throw new SchematicsException('The app module does not found');
} }
const matchingProperties = getMetadataField(node as ts.ObjectLiteralExpression, 'providers'); const providersProperty = getMetadataField(
const assignment = matchingProperties[0] as ts.PropertyAssignment; node as ts.ObjectLiteralExpression,
const assignmentInit = assignment.initializer as ts.ArrayLiteralExpression; 'providers',
)[0] as ts.PropertyAssignment;
const providersArray = providersProperty.initializer as ts.ArrayLiteralExpression;
const elements = assignmentInit.elements; if (!providersArray.elements.length) return host;
if (!elements || elements.length < 1) {
throw new SchematicsException(`Elements could not found: ${elements}`);
}
const filteredElements = elements.filter(f => for (const element of providersArray.elements) {
impMap.filter(f => !!f.provider).some(s => f.getText().match(s.provider!)), if (ts.isCallExpression(element) && element.expression.getText() === 'importProvidersFrom') {
); const arrayArg = element.arguments[0];
if (!filteredElements || filteredElements.length < 1) { if (ts.isArrayLiteralExpression(arrayArg)) {
return; const elementsToRemove = arrayArg.elements.filter(el =>
impMap.some(s => el.getText().includes(s.importName)),
);
if (elementsToRemove.length) {
for (const removeEl of elementsToRemove) {
const start = removeEl.getFullStart();
const end = removeEl.getEnd();
const nextChar = source.text.slice(end, end + 1);
const prevChar = source.text.slice(start - 1, start);
if (nextChar === ',') {
recorder.remove(start, end - start + 1); // sağındaki virgülle birlikte sil
} else if (prevChar === ',') {
recorder.remove(start - 1, end - start + 1); // solundaki virgülle birlikte sil
} else {
recorder.remove(start, end - start); // virgül yoksa sadece kendisini sil
}
}
}
// Eğer array boşaldıysa, importProvidersFrom çağrısını da kaldır
const remainingElements = arrayArg.elements.filter(el => !elementsToRemove.includes(el));
if (remainingElements.length === 0) {
const callStart = element.getFullStart();
const callEnd = element.getEnd();
const callNextChar = source.text.slice(callEnd, callEnd + 1);
const callPrevChar = source.text.slice(callStart - 1, callStart);
if (callNextChar === ',') {
recorder.remove(callStart, callEnd - callStart + 1);
} else if (callPrevChar === ',') {
recorder.remove(callStart - 1, callEnd - callStart + 1);
} else {
recorder.remove(callStart, callEnd - callStart);
}
}
}
}
} }
filteredElements.map(willRemoveModule => {
recorder.remove(willRemoveModule.getStart(), willRemoveModule.getWidth());
});
host.commitUpdate(recorder); host.commitUpdate(recorder);
return host; return host;
}; };
@ -407,11 +488,14 @@ export function adjustProvideAbpThemeShared(
const exprEnd = expr.getEnd(); const exprEnd = expr.getEnd();
const originalText = sourceText.substring(exprStart, exprEnd); const originalText = sourceText.substring(exprStart, exprEnd);
let newText = ''; let newText = originalText;
const hasHttpErrorConfig = originalText.includes('withHttpErrorConfig');
const hasValidationBluePrint = originalText.includes('withValidationBluePrint');
if (selectedTheme === ThemeOptionsEnum.LeptonX) { if (selectedTheme === ThemeOptionsEnum.LeptonX) {
if (!originalText.includes('withHttpErrorConfig')) { if (!hasHttpErrorConfig) {
newText = originalText.replace( newText = newText.replace(
'(', '(',
`( `(
withHttpErrorConfig({ withHttpErrorConfig({
@ -424,7 +508,19 @@ export function adjustProvideAbpThemeShared(
); );
} }
} else { } else {
newText = originalText.replace(/withHttpErrorConfig\([^)]*\),?/, ''); if (hasHttpErrorConfig) {
newText = newText.replace(/withHttpErrorConfig\([^)]*\),?/, '');
}
}
if (!hasValidationBluePrint) {
newText = newText.replace(
'(',
`(
withValidationBluePrint({
wrongPassword: 'Please choose 1q2w3E*'
}),`,
);
} }
if (newText && newText !== originalText) { if (newText && newText !== originalText) {

6
npm/ng-packs/packages/schematics/src/commands/change-theme/style-map.ts

@ -285,13 +285,14 @@ importMap.set(ThemeOptionsEnum.Basic, [
{ {
path: '@abp/ng.theme.shared', path: '@abp/ng.theme.shared',
importName: 'provideAbpThemeShared', importName: 'provideAbpThemeShared',
provider: 'provideAbpThemeShared',
}, },
]); ]);
importMap.set(ThemeOptionsEnum.Lepton, [ importMap.set(ThemeOptionsEnum.Lepton, [
{ {
path: '@volo/abp.ng.theme.lepton', path: '@volo/abp.ng.theme.lepton',
importName: 'ThemeLeptonModule', importName: 'provideThemeLepton',
provider: 'provideThemeLepton', provider: 'provideThemeLepton',
}, },
{ {
@ -310,6 +311,7 @@ importMap.set(ThemeOptionsEnum.Lepton, [
{ {
path: '@abp/ng.theme.shared', path: '@abp/ng.theme.shared',
importName: 'provideAbpThemeShared', importName: 'provideAbpThemeShared',
provider: 'provideAbpThemeShared',
}, },
]); ]);
@ -345,6 +347,7 @@ importMap.set(ThemeOptionsEnum.LeptonXLite, [
{ {
path: '@abp/ng.theme.shared', path: '@abp/ng.theme.shared',
importName: 'provideAbpThemeShared', importName: 'provideAbpThemeShared',
provider: 'provideAbpThemeShared',
}, },
]); ]);
@ -379,5 +382,6 @@ importMap.set(ThemeOptionsEnum.LeptonX, [
{ {
path: '@abp/ng.theme.shared', path: '@abp/ng.theme.shared',
importName: 'provideAbpThemeShared', importName: 'provideAbpThemeShared',
provider: 'provideAbpThemeShared',
}, },
]); ]);

Loading…
Cancel
Save