Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit c8fba38

Browse files
committed
refactor(@schematics/angular): add readIntoSourceFile
1 parent adc79f0 commit c8fba38

File tree

1 file changed

+11
-18
lines changed
  • packages/schematics/angular/component

1 file changed

+11
-18
lines changed

packages/schematics/angular/component/index.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ import { parseName } from '../utility/parse-name';
3030
import { validateHtmlSelector, validateName } from '../utility/validation';
3131
import { Schema as ComponentOptions } from './schema';
3232

33+
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
34+
const text = host.read(modulePath);
35+
if (text === null) {
36+
throw new SchematicsException(`File ${modulePath} does not exist.`);
37+
}
38+
const sourceText = text.toString('utf-8');
39+
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
40+
}
3341

3442
function addDeclarationToNgModule(options: ComponentOptions): Rule {
3543
return (host: Tree) => {
@@ -38,12 +46,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
3846
}
3947

4048
const modulePath = options.module;
41-
const text = host.read(modulePath);
42-
if (text === null) {
43-
throw new SchematicsException(`File ${modulePath} does not exist.`);
44-
}
45-
const sourceText = text.toString('utf-8');
46-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
49+
const source = readIntoSourceFile(host, modulePath);
4750

4851
const componentPath = `/${options.path}/`
4952
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
@@ -66,12 +69,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
6669

6770
if (options.export) {
6871
// Need to refresh the AST because we overwrote the file in the host.
69-
const text = host.read(modulePath);
70-
if (text === null) {
71-
throw new SchematicsException(`File ${modulePath} does not exist.`);
72-
}
73-
const sourceText = text.toString('utf-8');
74-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
72+
const source = readIntoSourceFile(host, modulePath);
7573

7674
const exportRecorder = host.beginUpdate(modulePath);
7775
const exportChanges = addExportToModule(source, modulePath,
@@ -88,12 +86,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
8886

8987
if (options.entryComponent) {
9088
// Need to refresh the AST because we overwrote the file in the host.
91-
const text = host.read(modulePath);
92-
if (text === null) {
93-
throw new SchematicsException(`File ${modulePath} does not exist.`);
94-
}
95-
const sourceText = text.toString('utf-8');
96-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
89+
const source = readIntoSourceFile(host, modulePath);
9790

9891
const entryComponentRecorder = host.beginUpdate(modulePath);
9992
const entryComponentChanges = addEntryComponentToModule(source, modulePath,

0 commit comments

Comments
 (0)