MCPcopy Index your code
hub / github.com/angular/components / analyzeExamples

Function analyzeExamples

tools/example-module/generate-example-module.ts:82–158  ·  view source on GitHub ↗

* Analyzes the examples by parsing the given TypeScript files in order to find * individual example modules and example metadata.

(sourceFiles: string[], baseDir: string)

Source from the content-addressed store, hash-verified

80 * individual example modules and example metadata.
81 */
82function 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 }

Callers 1

generateExampleModuleFunction · 0.85

Calls 4

parseExampleFileFunction · 0.90
convertToDashCaseFunction · 0.85
pushMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…