|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 {dirname, relative} from 'path'; |
| 11 | +import * as ts from 'typescript'; |
| 12 | + |
| 13 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 14 | +import {parseTsconfigFile} from '../../utils/typescript/parse_tsconfig'; |
| 15 | +import {COMMON_IMPORT, DOCUMENT_TOKEN_NAME, DocumentImportVisitor, ResolvedDocumentImport} from './document_import_visitor'; |
| 16 | +import {addToImport, createImport, removeFromImport} from './move-import'; |
| 17 | + |
| 18 | + |
| 19 | +/** Entry point for the V8 move-document migration. */ |
| 20 | +export default function(): Rule { |
| 21 | + return (tree: Tree) => { |
| 22 | + const projectTsConfigPaths = getProjectTsConfigPaths(tree); |
| 23 | + const basePath = process.cwd(); |
| 24 | + |
| 25 | + if (!projectTsConfigPaths.length) { |
| 26 | + throw new SchematicsException(`Could not find any tsconfig file. Cannot migrate DOCUMENT |
| 27 | + to new import source.`); |
| 28 | + } |
| 29 | + |
| 30 | + for (const tsconfigPath of projectTsConfigPaths) { |
| 31 | + runMoveDocumentMigration(tree, tsconfigPath, basePath); |
| 32 | + } |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Runs the DOCUMENT InjectionToken import migration for the given TypeScript project. The |
| 38 | + * schematic analyzes the imports within the project and moves the deprecated symbol to the |
| 39 | + * new import source. |
| 40 | + */ |
| 41 | +function runMoveDocumentMigration(tree: Tree, tsconfigPath: string, basePath: string) { |
| 42 | + const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath)); |
| 43 | + const host = ts.createCompilerHost(parsed.options, true); |
| 44 | + |
| 45 | + // We need to overwrite the host "readFile" method, as we want the TypeScript |
| 46 | + // program to be based on the file contents in the virtual file tree. Otherwise |
| 47 | + // if we run the migration for multiple tsconfig files which have intersecting |
| 48 | + // source files, it can end up updating query definitions multiple times. |
| 49 | + host.readFile = fileName => { |
| 50 | + const buffer = tree.read(relative(basePath, fileName)); |
| 51 | + return buffer ? buffer.toString() : undefined; |
| 52 | + }; |
| 53 | + |
| 54 | + const program = ts.createProgram(parsed.fileNames, parsed.options, host); |
| 55 | + const typeChecker = program.getTypeChecker(); |
| 56 | + const visitor = new DocumentImportVisitor(typeChecker); |
| 57 | + const rootSourceFiles = program.getRootFileNames().map(f => program.getSourceFile(f) !); |
| 58 | + |
| 59 | + // Analyze source files by finding imports. |
| 60 | + rootSourceFiles.forEach(sourceFile => visitor.visitNode(sourceFile)); |
| 61 | + |
| 62 | + const {importsMap} = visitor; |
| 63 | + |
| 64 | + // Walk through all source files that contain resolved queries and update |
| 65 | + // the source files if needed. Note that we need to update multiple queries |
| 66 | + // within a source file within the same recorder in order to not throw off |
| 67 | + // the TypeScript node offsets. |
| 68 | + importsMap.forEach((resolvedImport: ResolvedDocumentImport, sourceFile: ts.SourceFile) => { |
| 69 | + const {platformBrowserImport, commonImport, documentElement} = resolvedImport; |
| 70 | + if (!documentElement || !platformBrowserImport) { |
| 71 | + return; |
| 72 | + } |
| 73 | + const update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); |
| 74 | + |
| 75 | + const platformBrowserDeclaration = platformBrowserImport.parent.parent; |
| 76 | + const newPlatformBrowserText = |
| 77 | + removeFromImport(platformBrowserImport, sourceFile, DOCUMENT_TOKEN_NAME); |
| 78 | + const newCommonText = commonImport ? |
| 79 | + addToImport(commonImport, sourceFile, documentElement.name, documentElement.propertyName) : |
| 80 | + createImport(COMMON_IMPORT, sourceFile, documentElement.name, documentElement.propertyName); |
| 81 | + |
| 82 | + // Replace the existing query decorator call expression with the updated |
| 83 | + // call expression node. |
| 84 | + update.remove(platformBrowserDeclaration.getStart(), platformBrowserDeclaration.getWidth()); |
| 85 | + update.insertRight(platformBrowserDeclaration.getStart(), newPlatformBrowserText); |
| 86 | + |
| 87 | + if (commonImport) { |
| 88 | + const commonDeclaration = commonImport.parent.parent; |
| 89 | + update.remove(commonDeclaration.getStart(), commonDeclaration.getWidth()); |
| 90 | + update.insertRight(commonDeclaration.getStart(), newCommonText); |
| 91 | + } else { |
| 92 | + update.insertRight(platformBrowserDeclaration.getStart(), newCommonText); |
| 93 | + } |
| 94 | + |
| 95 | + tree.commitUpdate(update); |
| 96 | + }); |
| 97 | +} |
0 commit comments