2019-04-17 00:06:06 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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, SchematicsException, Tree} from '@angular-devkit/schematics';
|
|
|
|
import {dirname, relative} from 'path';
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
|
2019-09-24 06:19:23 -04:00
|
|
|
import {createMigrationCompilerHost} from '../../utils/typescript/compiler_host';
|
2019-04-17 00:06:06 -04:00
|
|
|
import {parseTsconfigFile} from '../../utils/typescript/parse_tsconfig';
|
2019-09-24 06:19:23 -04:00
|
|
|
|
2019-04-17 00:06:06 -04:00
|
|
|
import {COMMON_IMPORT, DOCUMENT_TOKEN_NAME, DocumentImportVisitor, ResolvedDocumentImport} from './document_import_visitor';
|
|
|
|
import {addToImport, createImport, removeFromImport} from './move-import';
|
|
|
|
|
|
|
|
|
2019-09-24 06:19:23 -04:00
|
|
|
|
2019-04-17 00:06:06 -04:00
|
|
|
/** Entry point for the V8 move-document migration. */
|
|
|
|
export default function(): Rule {
|
|
|
|
return (tree: Tree) => {
|
2019-04-22 15:11:29 -04:00
|
|
|
const {buildPaths, testPaths} = getProjectTsConfigPaths(tree);
|
2019-04-17 00:06:06 -04:00
|
|
|
const basePath = process.cwd();
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
if (!buildPaths.length && !testPaths.length) {
|
2019-04-17 00:06:06 -04:00
|
|
|
throw new SchematicsException(`Could not find any tsconfig file. Cannot migrate DOCUMENT
|
|
|
|
to new import source.`);
|
|
|
|
}
|
|
|
|
|
2019-04-22 15:11:29 -04:00
|
|
|
for (const tsconfigPath of [...buildPaths, ...testPaths]) {
|
2019-04-17 00:06:06 -04:00
|
|
|
runMoveDocumentMigration(tree, tsconfigPath, basePath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the DOCUMENT InjectionToken import migration for the given TypeScript project. The
|
|
|
|
* schematic analyzes the imports within the project and moves the deprecated symbol to the
|
|
|
|
* new import source.
|
|
|
|
*/
|
|
|
|
function runMoveDocumentMigration(tree: Tree, tsconfigPath: string, basePath: string) {
|
|
|
|
const parsed = parseTsconfigFile(tsconfigPath, dirname(tsconfigPath));
|
2019-09-24 06:19:23 -04:00
|
|
|
const host = createMigrationCompilerHost(tree, parsed.options, basePath);
|
2019-04-17 00:06:06 -04:00
|
|
|
|
|
|
|
const program = ts.createProgram(parsed.fileNames, parsed.options, host);
|
|
|
|
const typeChecker = program.getTypeChecker();
|
|
|
|
const visitor = new DocumentImportVisitor(typeChecker);
|
2019-05-05 06:02:35 -04:00
|
|
|
const sourceFiles = program.getSourceFiles().filter(
|
|
|
|
f => !f.isDeclarationFile && !program.isSourceFileFromExternalLibrary(f));
|
2019-04-17 00:06:06 -04:00
|
|
|
|
|
|
|
// Analyze source files by finding imports.
|
2019-05-05 06:02:35 -04:00
|
|
|
sourceFiles.forEach(sourceFile => visitor.visitNode(sourceFile));
|
2019-04-17 00:06:06 -04:00
|
|
|
|
|
|
|
const {importsMap} = visitor;
|
|
|
|
|
|
|
|
// Walk through all source files that contain resolved queries and update
|
|
|
|
// the source files if needed. Note that we need to update multiple queries
|
|
|
|
// within a source file within the same recorder in order to not throw off
|
|
|
|
// the TypeScript node offsets.
|
|
|
|
importsMap.forEach((resolvedImport: ResolvedDocumentImport, sourceFile: ts.SourceFile) => {
|
|
|
|
const {platformBrowserImport, commonImport, documentElement} = resolvedImport;
|
|
|
|
if (!documentElement || !platformBrowserImport) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const update = tree.beginUpdate(relative(basePath, sourceFile.fileName));
|
|
|
|
|
|
|
|
const platformBrowserDeclaration = platformBrowserImport.parent.parent;
|
|
|
|
const newPlatformBrowserText =
|
|
|
|
removeFromImport(platformBrowserImport, sourceFile, DOCUMENT_TOKEN_NAME);
|
|
|
|
const newCommonText = commonImport ?
|
|
|
|
addToImport(commonImport, sourceFile, documentElement.name, documentElement.propertyName) :
|
|
|
|
createImport(COMMON_IMPORT, sourceFile, documentElement.name, documentElement.propertyName);
|
|
|
|
|
|
|
|
// Replace the existing query decorator call expression with the updated
|
|
|
|
// call expression node.
|
|
|
|
update.remove(platformBrowserDeclaration.getStart(), platformBrowserDeclaration.getWidth());
|
|
|
|
update.insertRight(platformBrowserDeclaration.getStart(), newPlatformBrowserText);
|
|
|
|
|
|
|
|
if (commonImport) {
|
|
|
|
const commonDeclaration = commonImport.parent.parent;
|
|
|
|
update.remove(commonDeclaration.getStart(), commonDeclaration.getWidth());
|
|
|
|
update.insertRight(commonDeclaration.getStart(), newCommonText);
|
|
|
|
} else {
|
|
|
|
update.insertRight(platformBrowserDeclaration.getStart(), newCommonText);
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.commitUpdate(update);
|
|
|
|
});
|
|
|
|
}
|