* Analyzes the examples by parsing the given TypeScript files in order to find * individual example modules and example metadata.
(sourceFiles: string[], baseDir: string)
| 80 | * individual example modules and example metadata. |
| 81 | */ |
| 82 | function analyzeExamples(sourceFiles: string[], baseDir: string): AnalyzedExamples { |
| 83 | const exampleMetadata: ExampleMetadata[] = []; |
| 84 | |
| 85 | for (const sourceFile of sourceFiles) { |
| 86 | const relativePath = path.relative(baseDir, sourceFile).replace(/\\/g, '/'); |
| 87 | const packagePath = path.dirname(relativePath); |
| 88 | const importPath = path.dirname(packagePath); |
| 89 | |
| 90 | // Avoid parsing non-example files. |
| 91 | if (!path.basename(sourceFile, path.extname(sourceFile)).endsWith('-example')) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | const sourceContent = fs.readFileSync(sourceFile, 'utf-8'); |
| 96 | const {primaryComponent, secondaryComponents} = parseExampleFile(sourceFile, sourceContent); |
| 97 | |
| 98 | if (primaryComponent) { |
| 99 | // Generate a unique id for the component by converting the class name to dash-case. |
| 100 | const exampleId = convertToDashCase(primaryComponent.componentName.replace('Example', '')); |
| 101 | const example: ExampleMetadata = { |
| 102 | sourcePath: relativePath, |
| 103 | packagePath, |
| 104 | id: exampleId, |
| 105 | selector: primaryComponent.selector, |
| 106 | componentName: primaryComponent.componentName, |
| 107 | title: primaryComponent.title.trim(), |
| 108 | additionalComponents: [], |
| 109 | files: [], |
| 110 | importPath, |
| 111 | }; |
| 112 | |
| 113 | // For consistency, we expect the example component selector to match |
| 114 | // the id of the example. |
| 115 | const expectedSelector = `${exampleId}-example`; |
| 116 | if (primaryComponent.selector !== expectedSelector) { |
| 117 | throw Error( |
| 118 | `Example ${exampleId} uses selector: ${primaryComponent.selector}, ` + |
| 119 | `but expected: ${expectedSelector}`, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | example.files.push(path.basename(relativePath)); |
| 124 | if (primaryComponent.templateUrl) { |
| 125 | example.files.push(primaryComponent.templateUrl); |
| 126 | } |
| 127 | if (primaryComponent.styleUrls) { |
| 128 | example.files.push(...primaryComponent.styleUrls); |
| 129 | } |
| 130 | if (primaryComponent.componentName.includes('Harness')) { |
| 131 | example.files.push(primaryComponent.selector + '.spec.ts'); |
| 132 | } |
| 133 | |
| 134 | if (secondaryComponents.length) { |
| 135 | for (const meta of secondaryComponents) { |
| 136 | example.additionalComponents.push(meta.componentName); |
| 137 | if (meta.templateUrl) { |
| 138 | example.files.push(meta.templateUrl); |
| 139 | } |
no test coverage detected
searching dependent graphs…