|
| 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 {createProgram, NgtscProgram} from '@angular/compiler-cli'; |
| 11 | +import {existsSync, statSync} from 'fs'; |
| 12 | +import {join, relative} from 'path'; |
| 13 | +import ts from 'typescript'; |
| 14 | + |
| 15 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 16 | +import {canMigrateFile, createProgramOptions} from '../../utils/typescript/compiler_host'; |
| 17 | + |
| 18 | +import {toStandalone} from './to-standalone'; |
| 19 | +import {ChangesByFile} from './util'; |
| 20 | + |
| 21 | +enum MigrationMode { |
| 22 | + toStandalone = 'convert-to-standalone', |
| 23 | +} |
| 24 | + |
| 25 | +interface Options { |
| 26 | + path: string; |
| 27 | + mode: MigrationMode; |
| 28 | +} |
| 29 | + |
| 30 | +export default function(options: Options): Rule { |
| 31 | + return async (tree) => { |
| 32 | + const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); |
| 33 | + const basePath = process.cwd(); |
| 34 | + const allPaths = [...buildPaths, ...testPaths]; |
| 35 | + |
| 36 | + if (!allPaths.length) { |
| 37 | + throw new SchematicsException( |
| 38 | + 'Could not find any tsconfig file. Cannot run the standalone migration.'); |
| 39 | + } |
| 40 | + |
| 41 | + for (const tsconfigPath of allPaths) { |
| 42 | + standaloneMigration(tree, tsconfigPath, basePath, options); |
| 43 | + } |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string, options: Options) { |
| 48 | + if (options.path.startsWith('..')) { |
| 49 | + throw new SchematicsException( |
| 50 | + 'Cannot run standalone migration outside of the current project.'); |
| 51 | + } |
| 52 | + |
| 53 | + const {host, rootNames} = createProgramOptions(tree, tsconfigPath, basePath); |
| 54 | + const program = createProgram({ |
| 55 | + rootNames, |
| 56 | + host, |
| 57 | + options: {_enableTemplateTypeChecker: true, compileNonExportedClasses: true} |
| 58 | + }) as NgtscProgram; |
| 59 | + const printer = ts.createPrinter(); |
| 60 | + const pathToMigrate = join(basePath, options.path); |
| 61 | + |
| 62 | + if (existsSync(pathToMigrate) && !statSync(pathToMigrate).isDirectory()) { |
| 63 | + throw new SchematicsException(`Migration path ${ |
| 64 | + pathToMigrate} has to be a directory. Cannot run the standalone migration.`); |
| 65 | + } |
| 66 | + |
| 67 | + const sourceFiles = program.getTsProgram().getSourceFiles().filter(sourceFile => { |
| 68 | + return sourceFile.fileName.startsWith(pathToMigrate) && |
| 69 | + canMigrateFile(basePath, sourceFile, program.getTsProgram()); |
| 70 | + }); |
| 71 | + |
| 72 | + if (sourceFiles.length === 0) { |
| 73 | + throw new SchematicsException(`Could not find any files to migrate under the path ${ |
| 74 | + pathToMigrate}. Cannot run the standalone migration.`); |
| 75 | + } |
| 76 | + |
| 77 | + let pendingChanges: ChangesByFile; |
| 78 | + |
| 79 | + if (options.mode === MigrationMode.toStandalone) { |
| 80 | + pendingChanges = toStandalone(sourceFiles, program, printer); |
| 81 | + } else { |
| 82 | + throw new SchematicsException( |
| 83 | + `Unknown schematic mode ${options.mode}. Cannot run the standalone migration.`); |
| 84 | + } |
| 85 | + |
| 86 | + for (const [file, changes] of pendingChanges.entries()) { |
| 87 | + const update = tree.beginUpdate(relative(basePath, file.fileName)); |
| 88 | + |
| 89 | + changes.forEach(change => { |
| 90 | + if (change.removeLength != null) { |
| 91 | + update.remove(change.start, change.removeLength); |
| 92 | + } |
| 93 | + update.insertRight(change.start, change.text); |
| 94 | + }); |
| 95 | + |
| 96 | + tree.commitUpdate(update); |
| 97 | + } |
| 98 | +} |
0 commit comments