( host: Tree, generateDir: string, moduleExt?: string, routingModuleExt?: string, )
| 82 | * Function to find the "closest" module to a generated file's path. |
| 83 | */ |
| 84 | export function findModule( |
| 85 | host: Tree, |
| 86 | generateDir: string, |
| 87 | moduleExt?: string, |
| 88 | routingModuleExt?: string, |
| 89 | ): Path { |
| 90 | let dir: DirEntry | null = host.getDir('/' + generateDir); |
| 91 | let foundRoutingModule = false; |
| 92 | |
| 93 | const moduleExtensions: string[] = moduleExt ? [moduleExt] : [MODULE_EXT, MODULE_EXT_LEGACY]; |
| 94 | const routingModuleExtensions: string[] = routingModuleExt |
| 95 | ? [routingModuleExt] |
| 96 | : [ROUTING_MODULE_EXT, ROUTING_MODULE_EXT_LEGACY]; |
| 97 | |
| 98 | while (dir) { |
| 99 | const allMatches = dir.subfiles.filter((p) => moduleExtensions.some((m) => p.endsWith(m))); |
| 100 | const filteredMatches = allMatches.filter( |
| 101 | (p) => !routingModuleExtensions.some((m) => p.endsWith(m)), |
| 102 | ); |
| 103 | |
| 104 | foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length; |
| 105 | |
| 106 | if (filteredMatches.length == 1) { |
| 107 | return join(dir.path, filteredMatches[0]); |
| 108 | } else if (filteredMatches.length > 1) { |
| 109 | throw new Error( |
| 110 | `More than one module matches. Use the '--skip-import' option to skip importing ` + |
| 111 | 'the component into the closest module or use the module option to specify a module.', |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | dir = dir.parent; |
| 116 | } |
| 117 | |
| 118 | const errorMsg = foundRoutingModule |
| 119 | ? 'Could not find a non Routing NgModule.' + |
| 120 | `\nModules with suffix '${routingModuleExt}' are strictly reserved for routing.` + |
| 121 | `\nUse the '--skip-import' option to skip importing in NgModule.` |
| 122 | : `Could not find an NgModule. Use the '--skip-import' option to skip importing in NgModule.`; |
| 123 | |
| 124 | throw new Error(errorMsg); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Build a relative path from one file path to another file path. |
no test coverage detected