MCPcopy Create free account
hub / github.com/angular/components / addDeclarationToNgModule

Function addDeclarationToNgModule

src/cdk/schematics/utils/build-component.ts:68–122  ·  view source on GitHub ↗
(options: ComponentOptions)

Source from the content-addressed store, hash-verified

66}
67
68function addDeclarationToNgModule(options: ComponentOptions): Rule {
69 return (host: Tree) => {
70 if (options.skipImport || options.standalone || !options.module) {
71 return host;
72 }
73
74 const modulePath = options.module;
75 let source = readIntoSourceFile(host, modulePath);
76
77 const componentPath =
78 `/${options.path}/` +
79 (options.flat ? '' : strings.dasherize(options.name) + '/') +
80 strings.dasherize(options.name) +
81 '.component';
82 const relativePath = buildRelativePath(modulePath, componentPath);
83 const classifiedName = strings.classify(`${options.name}Component`);
84
85 const declarationChanges = addDeclarationToModule(
86 source,
87 modulePath,
88 classifiedName,
89 relativePath,
90 );
91
92 const declarationRecorder = host.beginUpdate(modulePath);
93 for (const change of declarationChanges) {
94 if (change instanceof InsertChange) {
95 declarationRecorder.insertLeft(change.pos, change.toAdd);
96 }
97 }
98 host.commitUpdate(declarationRecorder);
99
100 if (options.export) {
101 // Need to refresh the AST because we overwrote the file in the host.
102 source = readIntoSourceFile(host, modulePath);
103
104 const exportRecorder = host.beginUpdate(modulePath);
105 const exportChanges = addExportToModule(
106 source,
107 modulePath,
108 strings.classify(`${options.name}Component`),
109 relativePath,
110 );
111
112 for (const change of exportChanges) {
113 if (change instanceof InsertChange) {
114 exportRecorder.insertLeft(change.pos, change.toAdd);
115 }
116 }
117 host.commitUpdate(exportRecorder);
118 }
119
120 return host;
121 };
122}
123
124function buildSelector(options: ComponentOptions, projectPrefix?: string) {
125 let selector = strings.dasherize(options.name);

Callers 1

buildComponentFunction · 0.85

Calls 2

readIntoSourceFileFunction · 0.85
insertLeftMethod · 0.80

Tested by

no test coverage detected