Skip to content

Commit 691133f

Browse files
committed
refactor(@schematics/angular): use Tree's newly introduced readText and readJSON functionality
Code related to decoding buffers into strings and parsing content into JSON can now be removed by using the support provided directly from the Tree instance for the executing schematic.
1 parent 3a15a43 commit 691133f

File tree

12 files changed

+40
-52
lines changed

12 files changed

+40
-52
lines changed

packages/angular_devkit/schematics_cli/blank/factory.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ function addSchematicToCollectionJson(
2828
description: JsonObject,
2929
): Rule {
3030
return (tree: Tree) => {
31-
const collectionJsonContent = tree.read(collectionPath);
32-
if (!collectionJsonContent) {
31+
const collectionJson = tree.readJSON(collectionPath);
32+
if (!collectionJson) {
3333
throw new Error('Invalid collection path: ' + collectionPath);
3434
}
3535

36-
const collectionJson = JSON.parse(collectionJsonContent.toString());
37-
if (!isJsonObject(collectionJson.schematics)) {
36+
if (!isJsonObject(collectionJson) || !isJsonObject(collectionJson.schematics)) {
3837
throw new Error('Invalid collection.json; schematics needs to be an object.');
3938
}
4039

@@ -55,9 +54,9 @@ export default function (options: Schema): Rule {
5554

5655
let collectionPath: Path | undefined;
5756
try {
58-
const packageJsonContent = tree.read('/package.json');
57+
const packageJsonContent = tree.readJSON('/package.json');
5958
if (packageJsonContent) {
60-
const packageJson = JSON.parse(packageJsonContent.toString()) as {
59+
const packageJson = packageJsonContent as {
6160
schematics: unknown;
6261
};
6362
if (typeof packageJson.schematics === 'string') {

packages/schematics/angular/app-shell/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import { BrowserBuilderOptions, Builders, ServerBuilderOptions } from '../utilit
3535
import { Schema as AppShellOptions } from './schema';
3636

3737
function getSourceFile(host: Tree, path: string): ts.SourceFile {
38-
const buffer = host.read(path);
39-
if (!buffer) {
38+
const content = host.readText(path);
39+
if (!content) {
4040
throw new SchematicsException(`Could not find ${path}.`);
4141
}
42-
const content = buffer.toString();
42+
4343
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
4444

4545
return source;
@@ -82,9 +82,9 @@ function getComponentTemplate(host: Tree, compPath: string, tmplInfo: TemplateIn
8282
const templateUrl = (tmplInfo.templateUrlProp.initializer as ts.StringLiteral).text;
8383
const dir = dirname(normalize(compPath));
8484
const templatePath = join(dir, templateUrl);
85-
const buffer = host.read(templatePath);
86-
if (buffer) {
87-
template = buffer.toString();
85+
const text = host.readText(templatePath);
86+
if (text !== null) {
87+
template = text;
8888
}
8989
}
9090

packages/schematics/angular/component/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ import { buildDefaultPath, getWorkspace } from '../utility/workspace';
3232
import { Schema as ComponentOptions, Style } from './schema';
3333

3434
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
35-
const text = host.read(modulePath);
36-
if (text === null) {
35+
const sourceText = host.readText(modulePath);
36+
if (sourceText === null) {
3737
throw new SchematicsException(`File ${modulePath} does not exist.`);
3838
}
39-
const sourceText = text.toString('utf-8');
4039

4140
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
4241
}

packages/schematics/angular/directive/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ function addDeclarationToNgModule(options: DirectiveOptions): Rule {
3636
}
3737

3838
const modulePath = options.module;
39-
const text = host.read(modulePath);
40-
if (text === null) {
39+
const sourceText = host.readText(modulePath);
40+
if (sourceText === null) {
4141
throw new SchematicsException(`File ${modulePath} does not exist.`);
4242
}
43-
const sourceText = text.toString('utf-8');
4443
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
4544

4645
const directivePath =
@@ -66,11 +65,10 @@ function addDeclarationToNgModule(options: DirectiveOptions): Rule {
6665

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

7674
const exportRecorder = host.beginUpdate(modulePath);

packages/schematics/angular/module/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
5454

5555
const modulePath = options.module;
5656

57-
const text = host.read(modulePath);
58-
if (text === null) {
57+
const sourceText = host.readText(modulePath);
58+
if (sourceText === null) {
5959
throw new SchematicsException(`File ${modulePath} does not exist.`);
6060
}
61-
const sourceText = text.toString();
6261
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
6362

6463
const relativePath = buildRelativeModulePath(options, modulePath);
@@ -100,12 +99,11 @@ function addRouteDeclarationToNgModule(
10099
path = options.module;
101100
}
102101

103-
const text = host.read(path);
104-
if (!text) {
102+
const sourceText = host.readText(path);
103+
if (sourceText === null) {
105104
throw new Error(`Couldn't find the module nor its routing module.`);
106105
}
107106

108-
const sourceText = text.toString();
109107
const addDeclaration = addRouteDeclarationToModule(
110108
ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true),
111109
path,

packages/schematics/angular/pipe/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ function addDeclarationToNgModule(options: PipeOptions): Rule {
3535
}
3636

3737
const modulePath = options.module;
38-
const text = host.read(modulePath);
39-
if (text === null) {
38+
const sourceText = host.readText(modulePath);
39+
if (sourceText === null) {
4040
throw new SchematicsException(`File ${modulePath} does not exist.`);
4141
}
42-
const sourceText = text.toString('utf-8');
4342
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
4443

4544
const pipePath =
@@ -63,11 +62,10 @@ function addDeclarationToNgModule(options: PipeOptions): Rule {
6362
host.commitUpdate(recorder);
6463

6564
if (options.export) {
66-
const text = host.read(modulePath);
67-
if (text === null) {
65+
const sourceText = host.readText(modulePath);
66+
if (sourceText === null) {
6867
throw new SchematicsException(`File ${modulePath} does not exist.`);
6968
}
70-
const sourceText = text.toString('utf-8');
7169
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
7270

7371
const exportRecorder = host.beginUpdate(modulePath);

packages/schematics/angular/service-worker/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,10 @@ function updateAppModule(mainPath: string): Rule {
122122
}
123123

124124
function getTsSourceFile(host: Tree, path: string): ts.SourceFile {
125-
const buffer = host.read(path);
126-
if (!buffer) {
125+
const content = host.readText(path);
126+
if (content === null) {
127127
throw new SchematicsException(`Could not read file (${path}).`);
128128
}
129-
const content = buffer.toString();
130129
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
131130

132131
return source;

packages/schematics/angular/universal/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,10 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
9898
}
9999

100100
function findBrowserModuleImport(host: Tree, modulePath: string): ts.Node {
101-
const moduleBuffer = host.read(modulePath);
102-
if (!moduleBuffer) {
101+
const moduleFileText = host.readText(modulePath);
102+
if (moduleFileText === null) {
103103
throw new SchematicsException(`Module file (${modulePath}) not found`);
104104
}
105-
const moduleFileText = moduleBuffer.toString('utf-8');
106105

107106
const source = ts.createSourceFile(modulePath, moduleFileText, ts.ScriptTarget.Latest, true);
108107

packages/schematics/angular/utility/json-file.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export class JSONFile {
2727
content: string;
2828

2929
constructor(private readonly host: Tree, private readonly path: string) {
30-
const buffer = this.host.read(this.path);
31-
if (buffer) {
32-
this.content = buffer.toString();
30+
const content = this.host.readText(this.path);
31+
if (content !== null) {
32+
this.content = content;
3333
} else {
3434
throw new Error(`Could not read '${path}'.`);
3535
}

packages/schematics/angular/utility/ng-ast-utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescri
1313
import { findNode, getSourceNodes } from '../utility/ast-utils';
1414

1515
export function findBootstrapModuleCall(host: Tree, mainPath: string): ts.CallExpression | null {
16-
const mainBuffer = host.read(mainPath);
17-
if (!mainBuffer) {
16+
const mainText = host.readText(mainPath);
17+
if (mainText === null) {
1818
throw new SchematicsException(`Main file (${mainPath}) not found`);
1919
}
20-
const mainText = mainBuffer.toString('utf-8');
2120
const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
2221

2322
const allNodes = getSourceNodes(source);
@@ -58,11 +57,10 @@ export function findBootstrapModulePath(host: Tree, mainPath: string): string {
5857

5958
const bootstrapModule = bootstrapCall.arguments[0];
6059

61-
const mainBuffer = host.read(mainPath);
62-
if (!mainBuffer) {
60+
const mainText = host.readText(mainPath);
61+
if (mainText === null) {
6362
throw new SchematicsException(`Client application main file (${mainPath}) not found`);
6463
}
65-
const mainText = mainBuffer.toString('utf-8');
6664
const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
6765
const allNodes = getSourceNodes(source);
6866
const bootstrapModuleRelativePath = allNodes

0 commit comments

Comments
 (0)