| 16 | } |
| 17 | |
| 18 | export function migrate(options: Options): Rule { |
| 19 | return async (tree, context) => { |
| 20 | await runMigrationInDevkit({ |
| 21 | tree, |
| 22 | getMigration: (fs) => |
| 23 | new OutputMigration({ |
| 24 | shouldMigrate: (_, file) => { |
| 25 | return ( |
| 26 | file.rootRelativePath.startsWith(fs.normalize(options.path)) && |
| 27 | !/(^|\/)node_modules\//.test(file.rootRelativePath) |
| 28 | ); |
| 29 | }, |
| 30 | }), |
| 31 | beforeProgramCreation: (tsconfigPath, stage) => { |
| 32 | if (stage === MigrationStage.Analysis) { |
| 33 | context.logger.info(`Preparing analysis for: ${tsconfigPath}...`); |
| 34 | } else { |
| 35 | context.logger.info(`Running migration for: ${tsconfigPath}...`); |
| 36 | } |
| 37 | }, |
| 38 | afterProgramCreation: (info, fs) => { |
| 39 | const analysisPath = fs.resolve(options.analysisDir); |
| 40 | |
| 41 | // Support restricting the analysis to subfolders for larger projects. |
| 42 | if (analysisPath !== '/') { |
| 43 | info.sourceFiles = info.sourceFiles.filter((sf) => sf.fileName.startsWith(analysisPath)); |
| 44 | info.fullProgramSourceFiles = info.fullProgramSourceFiles.filter((sf) => |
| 45 | sf.fileName.startsWith(analysisPath), |
| 46 | ); |
| 47 | } |
| 48 | }, |
| 49 | beforeUnitAnalysis: (tsconfigPath) => { |
| 50 | context.logger.info(`Scanning for outputs: ${tsconfigPath}...`); |
| 51 | }, |
| 52 | afterAllAnalyzed: () => { |
| 53 | context.logger.info(``); |
| 54 | context.logger.info(`Processing analysis data between targets...`); |
| 55 | context.logger.info(``); |
| 56 | }, |
| 57 | afterAnalysisFailure: () => { |
| 58 | context.logger.error('Migration failed unexpectedly with no analysis data'); |
| 59 | }, |
| 60 | whenDone: ({detectedOutputs, problematicOutputs, successRate}) => { |
| 61 | const migratedOutputs = detectedOutputs - problematicOutputs; |
| 62 | const successRatePercent = (successRate * 100).toFixed(2); |
| 63 | |
| 64 | context.logger.info(''); |
| 65 | context.logger.info(`Successfully migrated to outputs as functions 🎉`); |
| 66 | context.logger.info( |
| 67 | ` -> Migrated ${migratedOutputs} out of ${detectedOutputs} detected outputs (${successRatePercent} %).`, |
| 68 | ); |
| 69 | }, |
| 70 | }); |
| 71 | }; |
| 72 | } |