(tree: Tree, modulePath: string, className: string)
| 13 | * Whether the Angular module in the given path imports the specified module class name. |
| 14 | */ |
| 15 | export function hasNgModuleImport(tree: Tree, modulePath: string, className: string): boolean { |
| 16 | const moduleFileContent = tree.read(modulePath); |
| 17 | |
| 18 | if (!moduleFileContent) { |
| 19 | throw new SchematicsException(`Could not read Angular module file: ${modulePath}`); |
| 20 | } |
| 21 | |
| 22 | const parsedFile = ts.createSourceFile( |
| 23 | modulePath, |
| 24 | moduleFileContent.toString(), |
| 25 | ts.ScriptTarget.Latest, |
| 26 | true, |
| 27 | ); |
| 28 | const ngModuleMetadata = findNgModuleMetadata(parsedFile); |
| 29 | |
| 30 | if (!ngModuleMetadata) { |
| 31 | throw new SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`); |
| 32 | } |
| 33 | |
| 34 | for (let property of ngModuleMetadata!.properties) { |
| 35 | if ( |
| 36 | !ts.isPropertyAssignment(property) || |
| 37 | property.name.getText() !== 'imports' || |
| 38 | !ts.isArrayLiteralExpression(property.initializer) |
| 39 | ) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | if (property.initializer.elements.some(element => element.getText() === className)) { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Resolves the last identifier that is part of the given expression. This helps resolving |
no test coverage detected