|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Rule, SchematicsException, Tree} from '@angular-devkit/schematics'; |
| 10 | +import {relative} from 'path'; |
| 11 | +import ts from 'typescript'; |
| 12 | + |
| 13 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 14 | +import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host'; |
| 15 | + |
| 16 | +import {migrateEntryComponentsUsages} from './util'; |
| 17 | + |
| 18 | + |
| 19 | +/** Migration that removes `entryComponents` usages. */ |
| 20 | +export default function(): Rule { |
| 21 | + return async (tree: Tree) => { |
| 22 | + const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); |
| 23 | + const basePath = process.cwd(); |
| 24 | + const allPaths = [...buildPaths, ...testPaths]; |
| 25 | + |
| 26 | + if (!allPaths.length) { |
| 27 | + throw new SchematicsException( |
| 28 | + 'Could not find any tsconfig file. Cannot remove `entryComponents`.'); |
| 29 | + } |
| 30 | + |
| 31 | + for (const tsconfigPath of allPaths) { |
| 32 | + runEntryComponentsMigration(tree, tsconfigPath, basePath); |
| 33 | + } |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function runEntryComponentsMigration(tree: Tree, tsconfigPath: string, basePath: string) { |
| 38 | + const {program} = createMigrationProgram(tree, tsconfigPath, basePath); |
| 39 | + const typeChecker = program.getTypeChecker(); |
| 40 | + const printer = ts.createPrinter(); |
| 41 | + |
| 42 | + program.getSourceFiles() |
| 43 | + .filter(sourceFile => canMigrateFile(basePath, sourceFile, program)) |
| 44 | + .forEach(sourceFile => { |
| 45 | + const usages = migrateEntryComponentsUsages(typeChecker, printer, sourceFile); |
| 46 | + |
| 47 | + if (usages.length > 0) { |
| 48 | + const update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); |
| 49 | + usages.forEach(usage => { |
| 50 | + update.remove(usage.start, usage.length); |
| 51 | + update.insertRight(usage.start, usage.replacement); |
| 52 | + }); |
| 53 | + tree.commitUpdate(update); |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
0 commit comments