-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
get-component-selector.ts
32 lines (29 loc) · 1 KB
/
get-component-selector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { Tree } from '@nrwl/devkit';
import { findNodes } from 'nx/src/utils/typescript';
import type { PropertyAssignment } from 'typescript';
import { SyntaxKind } from 'typescript';
import {
getDecoratorMetadata,
getTsSourceFile,
} from '../../../utils/nx-devkit/ast-utils';
export function getComponentSelector(tree: Tree, path: string): string {
const file = getTsSourceFile(tree, path);
const componentDecorators = getDecoratorMetadata(
file,
'Component',
'@angular/core'
);
if (componentDecorators.length === 0) {
throw new Error(`No @Component decorator in ${path}.`);
}
const componentDecorator = componentDecorators[0];
const selectorNode = <PropertyAssignment>(
findNodes(componentDecorator, SyntaxKind.PropertyAssignment).find(
(node: PropertyAssignment) => node.name.getText() === 'selector'
)
);
if (!selectorNode) {
throw new Error(`No selector defined for the component in ${path}.`);
}
return selectorNode.initializer.getText().slice(1, -1);
}