nx/packages/angular/src/generators/application/lib/update-component-template.ts
Colum Ferry 96293b02cb
cleanup(angular): refactor angular application generator (#6036)
Refactor Angular Application Schematic to Generator using Nx Devkit
2021-06-18 15:39:05 +01:00

44 lines
1.2 KiB
TypeScript

import type { Tree } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';
import * as ts from 'typescript';
import { replaceNodeValue } from '@nrwl/workspace/src/utilities/ast-utils';
import { getDecoratorPropertyValueNode } from '../../../utils/nx-devkit/ast-utils';
import { nrwlHomeTemplate } from './nrwl-home-tpl';
export function updateComponentTemplate(host: Tree, options: NormalizedSchema) {
const content = options.routing
? `${nrwlHomeTemplate.html}\n<router-outlet></router-outlet>`
: nrwlHomeTemplate.html;
if (!options.inlineTemplate) {
host.write(`${options.appProjectRoot}/src/app/app.component.html`, content);
return;
}
// Inline component update
const componentPath = `${options.appProjectRoot}/src/app/app.component.ts`;
const templateNodeValue = getDecoratorPropertyValueNode(
host,
componentPath,
'Component',
'template',
'@angular/core'
);
replaceNodeValue(
host,
ts.createSourceFile(
componentPath,
host.read(componentPath, 'utf-8'),
ts.ScriptTarget.Latest,
true
),
componentPath,
templateNodeValue,
`\`\n${nrwlHomeTemplate.html}\n\`,\n`
);
}