-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
index.ts
100 lines (89 loc) · 3.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {Schema} from './schema';
import {DevkitFileSystem, UpdateProject, findStylesheetFiles} from '@angular/cdk/schematics';
import {ThemingStylesMigration} from './rules/theming-styles';
import {TemplateMigration} from './rules/template-migration';
import {ComponentMigrator, MIGRATORS} from './rules';
import {dirname} from 'path';
/** Groups of components that must be migrated together. */
const migrationGroups = [
['autocomplete', 'form-field', 'input', 'select'],
['button'],
['card'],
['checkbox'],
['chips'],
['dialog'],
['list'],
['menu'],
['paginator'],
['progress-bar'],
['progress-spinner'],
['radio'],
['slide-toggle'],
['slider'],
['snack-bar'],
['table'],
['tabs'],
['tooltip'],
];
function getComponentsToMigrate(requested: string[]): Set<string> {
const componentsToMigrate = new Set<string>(requested);
if (componentsToMigrate.has('all')) {
componentsToMigrate.clear();
migrationGroups.forEach(group =>
group.forEach(component => componentsToMigrate.add(component)),
);
} else {
for (const group of migrationGroups) {
if (group.some(component => componentsToMigrate.has(component))) {
group.forEach(component => componentsToMigrate.add(component));
}
}
}
return componentsToMigrate;
}
export default function (options: Schema): Rule {
const componentsToMigrate = getComponentsToMigrate(options.components);
const tsconfigPath = options.tsconfig;
const migrationDir = options.directory ?? dirname(tsconfigPath);
console.log('Migrating:', [...componentsToMigrate]);
console.log('Directory:', migrationDir);
const migrators: ComponentMigrator[] = [];
for (let i = 0; i < MIGRATORS.length; i++) {
if (componentsToMigrate.has(MIGRATORS[i].component)) {
migrators.push(MIGRATORS[i]);
}
}
return (tree: Tree, context: SchematicContext) => {
const fileSystem = new DevkitFileSystem(tree);
const program = UpdateProject.createProgramFromTsconfig(
fileSystem.resolve(tsconfigPath),
fileSystem,
);
const additionalStylesheetPaths = findStylesheetFiles(tree, migrationDir);
const project = new UpdateProject(context, program, fileSystem, new Set(), context.logger);
const {hasFailures} = project.migrate(
[ThemingStylesMigration, TemplateMigration],
null,
migrators,
additionalStylesheetPaths,
);
// Commit all recorded edits in the update recorder. We apply the edits after all
// migrations ran because otherwise offsets in the TypeScript program would be
// shifted and individual migrations could no longer update the same source file.
fileSystem.commitEdits();
if (hasFailures) {
context.logger.error('Unable to migrate project. See errors above.');
} else {
context.logger.info('Successfully migrated the project.');
}
return tree;
};
}