Skip to content

Commit a0e3de2

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 33f9f3d commit a0e3de2

File tree

12 files changed

+28
-95
lines changed

12 files changed

+28
-95
lines changed

packages/angular_devkit/schematics_cli/blank/factory.ts

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

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

@@ -55,16 +51,13 @@ export default function (options: Schema): Rule {
5551

5652
let collectionPath: Path | undefined;
5753
try {
58-
const packageJsonContent = tree.read('/package.json');
59-
if (packageJsonContent) {
60-
const packageJson = JSON.parse(packageJsonContent.toString()) as {
61-
schematics: unknown;
62-
};
63-
if (typeof packageJson.schematics === 'string') {
64-
const p = normalize(packageJson.schematics);
65-
if (tree.exists(p)) {
66-
collectionPath = p;
67-
}
54+
const packageJson = tree.readJson('/package.json') as {
55+
schematics: unknown;
56+
};
57+
if (typeof packageJson.schematics === 'string') {
58+
const p = normalize(packageJson.schematics);
59+
if (tree.exists(p)) {
60+
collectionPath = p;
6861
}
6962
}
7063
} catch {}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ 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) {
40-
throw new SchematicsException(`Could not find ${path}.`);
41-
}
42-
const content = buffer.toString();
38+
const content = host.readText(path);
4339
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
4440

4541
return source;
@@ -82,10 +78,9 @@ function getComponentTemplate(host: Tree, compPath: string, tmplInfo: TemplateIn
8278
const templateUrl = (tmplInfo.templateUrlProp.initializer as ts.StringLiteral).text;
8379
const dir = dirname(normalize(compPath));
8480
const templatePath = join(dir, templateUrl);
85-
const buffer = host.read(templatePath);
86-
if (buffer) {
87-
template = buffer.toString();
88-
}
81+
try {
82+
template = host.readText(templatePath);
83+
} catch {}
8984
}
9085

9186
return template;

packages/schematics/angular/component/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ 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) {
37-
throw new SchematicsException(`File ${modulePath} does not exist.`);
38-
}
39-
const sourceText = text.toString('utf-8');
35+
const sourceText = host.readText(modulePath);
4036

4137
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
4238
}

packages/schematics/angular/directive/index.ts

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

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

4642
const directivePath =
@@ -66,11 +62,7 @@ function addDeclarationToNgModule(options: DirectiveOptions): Rule {
6662

6763
if (options.export) {
6864
// 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');
65+
const sourceText = host.readText(modulePath);
7466
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
7567

7668
const exportRecorder = host.beginUpdate(modulePath);

packages/schematics/angular/module/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule {
5555

5656
const modulePath = options.module;
5757

58-
const text = host.read(modulePath);
59-
if (text === null) {
60-
throw new SchematicsException(`File ${modulePath} does not exist.`);
61-
}
62-
const sourceText = text.toString();
58+
const sourceText = host.readText(modulePath);
6359
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
6460

6561
const relativePath = buildRelativeModulePath(options, modulePath);
@@ -101,12 +97,8 @@ function addRouteDeclarationToNgModule(
10197
path = options.module;
10298
}
10399

104-
const text = host.read(path);
105-
if (!text) {
106-
throw new Error(`Couldn't find the module nor its routing module.`);
107-
}
100+
const sourceText = host.readText(path);
108101

109-
const sourceText = text.toString();
110102
const addDeclaration = addRouteDeclarationToModule(
111103
ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true),
112104
path,

packages/schematics/angular/pipe/index.ts

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

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

4541
const pipePath =
@@ -63,11 +59,7 @@ function addDeclarationToNgModule(options: PipeOptions): Rule {
6359
host.commitUpdate(recorder);
6460

6561
if (options.export) {
66-
const text = host.read(modulePath);
67-
if (text === null) {
68-
throw new SchematicsException(`File ${modulePath} does not exist.`);
69-
}
70-
const sourceText = text.toString('utf-8');
62+
const sourceText = host.readText(modulePath);
7163
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
7264

7365
const exportRecorder = host.beginUpdate(modulePath);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ 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) {
127-
throw new SchematicsException(`Could not read file (${path}).`);
128-
}
129-
const content = buffer.toString();
125+
const content = host.readText(path);
130126
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
131127

132128
return source;

packages/schematics/angular/universal/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ function updateConfigFile(options: UniversalOptions, tsConfigDirectory: Path): R
9999
}
100100

101101
function findBrowserModuleImport(host: Tree, modulePath: string): ts.Node {
102-
const moduleBuffer = host.read(modulePath);
103-
if (!moduleBuffer) {
104-
throw new SchematicsException(`Module file (${modulePath}) not found`);
105-
}
106-
const moduleFileText = moduleBuffer.toString('utf-8');
107-
102+
const moduleFileText = host.readText(modulePath);
108103
const source = ts.createSourceFile(modulePath, moduleFileText, ts.ScriptTarget.Latest, true);
109104

110105
const decoratorMetadata = getDecoratorMetadata(source, 'NgModule', '@angular/core')[0];

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ 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();
33-
} else {
34-
throw new Error(`Could not read '${path}'.`);
35-
}
30+
this.content = this.host.readText(this.path);
3631
}
3732

3833
private _jsonAst: Node | undefined;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ 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) {
18-
throw new SchematicsException(`Main file (${mainPath}) not found`);
19-
}
20-
const mainText = mainBuffer.toString('utf-8');
16+
const mainText = host.readText(mainPath);
2117
const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
2218

2319
const allNodes = getSourceNodes(source);
@@ -58,11 +54,7 @@ export function findBootstrapModulePath(host: Tree, mainPath: string): string {
5854

5955
const bootstrapModule = bootstrapCall.arguments[0];
6056

61-
const mainBuffer = host.read(mainPath);
62-
if (!mainBuffer) {
63-
throw new SchematicsException(`Client application main file (${mainPath}) not found`);
64-
}
65-
const mainText = mainBuffer.toString('utf-8');
57+
const mainText = host.readText(mainPath);
6658
const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
6759
const allNodes = getSourceNodes(source);
6860
const bootstrapModuleRelativePath = allNodes

packages/schematics/angular/utility/workspace.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { json, virtualFs, workspaces } from '@angular-devkit/core';
9+
import { json, workspaces } from '@angular-devkit/core';
1010
import { Rule, Tree, noop } from '@angular-devkit/schematics';
1111
import { ProjectType } from './workspace-models';
1212

1313
function createHost(tree: Tree): workspaces.WorkspaceHost {
1414
return {
1515
async readFile(path: string): Promise<string> {
16-
const data = tree.read(path);
17-
if (!data) {
18-
throw new Error('File not found.');
19-
}
20-
21-
return virtualFs.fileBufferToString(data);
16+
return tree.readText(path);
2217
},
2318
async writeFile(path: string, data: string): Promise<void> {
2419
return tree.overwrite(path, data);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function addSnippet(options: WebWorkerOptions): Rule {
6565
`;
6666

6767
// Append the worker creation snippet.
68-
const originalContent = host.read(siblingModulePath);
68+
const originalContent = host.readText(siblingModulePath);
6969
host.overwrite(siblingModulePath, originalContent + '\n' + workerCreationSnippet);
7070

7171
return host;

0 commit comments

Comments
 (0)