-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathindex.ts
389 lines (320 loc) · 12.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { JsonArray, JsonValue } from '@angular-devkit/core';
import { Rule, SchematicsException, Tree, chain } from '@angular-devkit/schematics';
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 {
addRootImport,
addRootProvider,
getAppModulePath,
isLibrary,
isStandaloneApp,
updateWorkspace,
WorkspaceDefinition,
getAppConfigPath,
cleanEmptyExprFromModule,
cleanEmptyExprFromProviders,
} from '../../utils';
import { ThemeOptionsEnum } from './theme-options.enum';
import { findNodes, getDecoratorMetadata, getMetadataField } from '../../utils/angular/ast-utils';
import { getMainFilePath } from '../../utils/angular/standalone/util';
export default function (_options: ChangeThemeOptions): Rule {
return async () => {
const targetThemeName = _options.name;
const selectedProject = _options.targetProject;
if (!targetThemeName) {
throw new SchematicsException('The theme name does not selected');
}
return chain([
updateWorkspace(storedWorkspace => {
updateProjectStyle(selectedProject, storedWorkspace, targetThemeName);
}),
updateAppModule(selectedProject, targetThemeName),
]);
};
}
function updateProjectStyle(
projectName: string,
workspace: WorkspaceDefinition,
targetThemeName: ThemeOptionsEnum,
) {
const project = workspace.projects.get(projectName);
if (!project) {
throw new SchematicsException('The target project does not selected');
}
if (isLibrary(project)) {
throw new SchematicsException('The library project does not supported');
}
const targetOption = getProjectTargetOptions(project, 'build');
const styles = targetOption.styles as (string | { input: string })[];
const sanitizedStyles = removeThemeBasedStyles(styles);
const newStyles = styleMap.get(targetThemeName);
if (!newStyles) {
throw new SchematicsException('The theme does not found');
}
targetOption.styles = [...newStyles, ...sanitizedStyles] as JsonArray;
}
function updateAppModule(selectedProject: string, targetThemeName: ThemeOptionsEnum): Rule {
return async (host: Tree) => {
const mainFilePath = await getMainFilePath(host, selectedProject);
const isStandalone = isStandaloneApp(host, mainFilePath);
const appModulePath = isStandalone
? getAppConfigPath(host, mainFilePath)
: getAppModulePath(host, mainFilePath);
return chain([
removeImportPath(appModulePath, targetThemeName),
...(!isStandalone ? [removeImportFromNgModuleMetadata(appModulePath, targetThemeName)] : []),
isStandalone
? removeImportsFromStandaloneProviders(appModulePath, targetThemeName)
: removeProviderFromNgModuleMetadata(appModulePath, targetThemeName),
insertImports(selectedProject, targetThemeName),
insertProviders(selectedProject, targetThemeName),
formatFile(appModulePath),
cleanEmptyExpressions(appModulePath, isStandalone),
]);
};
}
export function removeImportPath(filePath: string, selectedTheme: ThemeOptionsEnum): Rule {
return (host: Tree) => {
const buffer = host.read(filePath);
if (!buffer) return host;
const sourceText = buffer.toString('utf-8');
const source = ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.Latest, true);
const recorder = host.beginUpdate(filePath);
const impMap = getImportPaths(selectedTheme, true);
const nodes = findNodes(source, ts.isImportDeclaration);
const filteredNodes = nodes.filter(node => {
const importPath = (node.moduleSpecifier as ts.StringLiteral).text;
const namedBindings = node.importClause?.namedBindings;
return impMap.some(({ path, importName }) => {
const symbol = importName.split('.')[0];
const matchesPath = !!path && importPath === path;
const matchesSymbol =
!!namedBindings &&
ts.isNamedImports(namedBindings) &&
namedBindings.elements.some(e => e.name.text === symbol);
return matchesPath || matchesSymbol;
});
});
for (const node of filteredNodes) {
recorder.remove(node.getStart(), node.getWidth());
}
host.commitUpdate(recorder);
return host;
};
}
export function removeImportFromNgModuleMetadata(
appModulePath: string,
selectedTheme: ThemeOptionsEnum,
): Rule {
return (host: Tree) => {
const recorder = host.beginUpdate(appModulePath);
const source = createSourceFile(host, appModulePath);
const impMap = getImportPaths(selectedTheme, true);
const node = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] || {};
if (!node) {
throw new SchematicsException('The app module does not found');
}
const matchingProperties = getMetadataField(node as ts.ObjectLiteralExpression, 'imports');
const assignment = matchingProperties[0] as ts.PropertyAssignment;
const assignmentInit = assignment.initializer as ts.ArrayLiteralExpression;
const elements = assignmentInit.elements;
if (!elements || elements.length < 1) {
throw new SchematicsException(`Elements could not found: ${elements}`);
}
const filteredElements = elements.filter(f =>
impMap.some(s => f.getText().match(s.importName)),
);
if (!filteredElements || filteredElements.length < 1) {
return;
}
filteredElements.map(willRemoveModule =>
recorder.remove(willRemoveModule.getStart(), willRemoveModule.getWidth() + 1),
);
host.commitUpdate(recorder);
return host;
};
}
export function removeImportsFromStandaloneProviders(
mainPath: string,
selectedTheme: ThemeOptionsEnum,
): Rule {
return (host: Tree) => {
const buffer = host.read(mainPath);
if (!buffer) return host;
const sourceText = buffer.toString('utf-8');
const source = ts.createSourceFile(mainPath, sourceText, ts.ScriptTarget.Latest, true);
const recorder = host.beginUpdate(mainPath);
const impMap = getImportPaths(selectedTheme, true);
const callExpressions = findNodes(source, ts.isCallExpression);
for (const expr of callExpressions) {
const exprText = expr.getText();
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);
}
}
}
host.commitUpdate(recorder);
return host;
};
}
export function removeProviderFromNgModuleMetadata(
appModulePath: string,
selectedTheme: ThemeOptionsEnum,
): Rule {
return (host: Tree) => {
const recorder = host.beginUpdate(appModulePath);
const source = createSourceFile(host, appModulePath);
const impMap = getImportPaths(selectedTheme, true);
const node = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0] || {};
if (!node) {
throw new SchematicsException('The app module does not found');
}
const matchingProperties = getMetadataField(node as ts.ObjectLiteralExpression, 'providers');
const assignment = matchingProperties[0] as ts.PropertyAssignment;
const assignmentInit = assignment.initializer as ts.ArrayLiteralExpression;
const elements = assignmentInit.elements;
if (!elements || elements.length < 1) {
throw new SchematicsException(`Elements could not found: ${elements}`);
}
const filteredElements = elements.filter(f =>
impMap.filter(f => !!f.provider).some(s => f.getText().match(s.provider!)),
);
if (!filteredElements || filteredElements.length < 1) {
return;
}
filteredElements.map(willRemoveModule => {
recorder.remove(willRemoveModule.getStart(), willRemoveModule.getWidth());
});
host.commitUpdate(recorder);
return host;
};
}
export function insertImports(projectName: string, selectedTheme: ThemeOptionsEnum): Rule {
return addRootImport(projectName, code => {
const selected = importMap.get(selectedTheme);
if (!selected || selected.length === 0) return code.code``;
const expressions: string[] = [];
for (const { importName, path, expression } of selected) {
const imported = code.external(importName, path);
expressions.push(expression ?? imported); // default fallback
}
return code.code`${expressions.join(',\n')}`;
});
}
export function insertProviders(projectName: string, selectedTheme: ThemeOptionsEnum): Rule {
return addRootProvider(projectName, code => {
const selected = importMap.get(selectedTheme);
if (!selected || selected.length === 0) return code.code``;
const providers = selected
.filter(s => !!s.provider)
.map(({ provider, path }) => {
const symbol = code.external(provider!, path);
return `${symbol}()`;
});
return code.code`${providers}`;
});
}
export function createSourceFile(host: Tree, appModulePath: string): ts.SourceFile {
const buffer = host.read(appModulePath);
if (!buffer || buffer.length === 0) {
throw new SchematicsException(`${appModulePath} file could not be read.`);
}
const sourceText = buffer.toString('utf-8');
return ts.createSourceFile(
appModulePath,
sourceText,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS,
);
}
/**
* Returns all import paths except the selected theme
* @param selectedTheme The selected theme
* @param getAll If true, returns all import paths
*/
export function getImportPaths(selectedTheme: ThemeOptionsEnum, getAll = false) {
if (getAll) {
return Array.from(importMap.values()).reduce((acc, val) => [...acc, ...val], []);
}
return Array.from(importMap.values())
.filter(f => f !== importMap.get(selectedTheme))
.reduce((acc, val) => [...acc, ...val], []);
}
export function getProjectTargetOptions(
project: ProjectDefinition,
buildTarget: string,
): Record<string, JsonValue | undefined> {
const options = project.targets?.get(buildTarget)?.options;
if (!options) {
throw new SchematicsException(
`Cannot determine project target configuration for: ${buildTarget}.`,
);
}
return options;
}
export function removeThemeBasedStyles(styles: (string | object)[]) {
return styles.filter(s => !allStyles.some(x => styleCompareFn(s, x)));
}
export const styleCompareFn = (item1: string | object, item2: string | object) => {
const type1 = typeof item1;
const type2 = typeof item1;
if (type1 !== type2) {
return false;
}
if (type1 === 'string') {
return item1 === item2;
}
const o1 = item1 as { bundleName?: string };
const o2 = item2 as { bundleName?: string };
return o1.bundleName && o2.bundleName && o1.bundleName == o2.bundleName;
};
export const formatFile = (filePath: string): Rule => {
return (tree: Tree) => {
const buffer = tree.read(filePath);
if (!buffer) return tree;
const source = ts.createSourceFile(filePath, buffer.toString(), ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const formatted = printer.printFile(source);
tree.overwrite(filePath, formatted);
return tree;
};
};
export function cleanEmptyExpressions(modulePath: string, isStandalone: boolean): Rule {
return (host: Tree) => {
const buffer = host.read(modulePath);
if (!buffer) throw new SchematicsException(`Cannot read ${modulePath}`);
const source = ts.createSourceFile(
modulePath,
buffer.toString('utf-8'),
ts.ScriptTarget.Latest,
true,
);
const recorder = host.beginUpdate(modulePath);
if (isStandalone) {
cleanEmptyExprFromProviders(source, recorder);
} else {
cleanEmptyExprFromModule(source, recorder);
}
host.commitUpdate(recorder);
return host;
};
}