@@ -4,10 +4,20 @@ import { DefaultTreeDocument, DefaultTreeElement, parse as parseHtml } from 'par
44import { getWorkspace } from '@schematics/angular/utility/config' ;
55import { getFileContent } from '@schematics/angular/utility/test' ;
66import { NodeDependency } from '@schematics/angular/utility/dependencies' ;
7+ import * as ts from 'typescript' ;
78
89
910
1011
12+
13+ export function createOrOverwriteFile ( tree : Tree , filePath : string , fileContent : string ) : void {
14+ if ( ! tree . exists ( filePath ) ) {
15+ tree . create ( filePath , '' ) ;
16+ }
17+ tree . overwrite ( filePath , fileContent ) ;
18+ }
19+
20+
1121/** Appends fragment the specified file. */
1222export function appendToStartFile ( host : Tree , filePath : string , styleRule : string ) {
1323 const fileBuffer = host . read ( filePath ) ;
@@ -26,6 +36,18 @@ export function appendToStartFile(host: Tree, filePath: string, styleRule: strin
2636 host . commitUpdate ( recordedChange ) ;
2737}
2838
39+
40+ /** Appends the given element HTML fragment to the specified HTML file. */
41+ export function appendHtmlElementToTag ( host : Tree , htmlFilePath : string , elementHtml : string , side : string = 'right' ) {
42+ const htmlFileBuffer = host . read ( htmlFilePath ) ;
43+ if ( ! htmlFileBuffer ) {
44+ throw new SchematicsException ( `Could not read file for path: ${ htmlFilePath } ` ) ;
45+ }
46+ const htmlContent = htmlFileBuffer . toString ( ) ;
47+ host . overwrite ( `${ htmlFilePath } ` , ( side === 'right' ) ? htmlContent + elementHtml : elementHtml + htmlContent ) ;
48+ }
49+
50+
2951/** Appends the given element HTML fragment to the `<body>` element of the specified HTML file. */
3052export function appendHtmlElementToBody ( host : Tree , htmlFilePath : string , elementHtml : string , side : string = 'right' ) {
3153 const htmlFileBuffer = host . read ( htmlFilePath ) ;
@@ -68,6 +90,8 @@ export function appendHtmlElementToBody(host: Tree, htmlFilePath: string, elemen
6890 }
6991}
7092
93+
94+
7195/** Adds a class to the body of the document. */
7296export function addBodyClass ( host : Tree , htmlFilePath : string , className : string ) : void {
7397 const htmlFileBuffer = host . read ( htmlFilePath ) ;
@@ -165,3 +189,11 @@ export function getSourceRoot(tree: Tree, options: any): string {
165189 const workspace = getWorkspace ( tree ) ;
166190 return `/${ workspace . projects [ options . clientProject ] . sourceRoot } ` ;
167191}
192+
193+ export function readIntoSourceFile ( host : Tree , modulePath : string ) {
194+ const text = host . read ( modulePath ) ;
195+ if ( text === null ) {
196+ throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
197+ }
198+ return ts . createSourceFile ( modulePath , text . toString ( 'utf-8' ) , ts . ScriptTarget . Latest , true ) ;
199+ }
0 commit comments