Skip to content

Commit d2fa2ed

Browse files
clydinalexeagle
authored andcommitted
refactor(@schematics/angular): update class to use new workspace rules
1 parent 83f3c6c commit d2fa2ed

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

packages/schematics/angular/class/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
12-
SchematicsException,
1311
Tree,
1412
apply,
1513
applyTemplates,
@@ -22,19 +20,13 @@ import {
2220
} from '@angular-devkit/schematics';
2321
import { applyLintFix } from '../utility/lint-fix';
2422
import { parseName } from '../utility/parse-name';
25-
import { buildDefaultPath, getProject } from '../utility/project';
23+
import { createDefaultPath } from '../utility/workspace';
2624
import { Schema as ClassOptions } from './schema';
2725

2826
export default function (options: ClassOptions): Rule {
29-
return (host: Tree, context: SchematicContext) => {
30-
if (!options.project) {
31-
throw new SchematicsException('Option (project) is required.');
32-
}
33-
34-
const project = getProject(host, options.project);
35-
27+
return async (host: Tree) => {
3628
if (options.path === undefined) {
37-
options.path = buildDefaultPath(project);
29+
options.path = await createDefaultPath(host, options.project as string);
3830
}
3931

4032
options.type = !!options.type ? `.${options.type}` : '';

packages/schematics/angular/class/index_spec.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,61 @@ describe('Class Schematic', () => {
4444
appTree = schematicRunner.runSchematic('application', appOptions, appTree);
4545
});
4646

47-
it('should create just the class file', () => {
48-
const tree = schematicRunner.runSchematic('class', defaultOptions, appTree);
47+
it('should create just the class file', async () => {
48+
const tree = await schematicRunner.runSchematicAsync('class', defaultOptions, appTree)
49+
.toPromise();
4950
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
5051
expect(tree.files).not.toContain('/projects/bar/src/app/foo.spec.ts');
5152
});
5253

53-
it('should create the class and spec file', () => {
54+
it('should create the class and spec file', async () => {
5455
const options = {
5556
...defaultOptions,
5657
spec: true,
5758
};
58-
const tree = schematicRunner.runSchematic('class', options, appTree);
59+
const tree = await schematicRunner.runSchematicAsync('class', options, appTree)
60+
.toPromise();
5961
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
6062
expect(tree.files).toContain('/projects/bar/src/app/foo.spec.ts');
6163
});
6264

63-
it('should create an class named "Foo"', () => {
64-
const tree = schematicRunner.runSchematic('class', defaultOptions, appTree);
65+
it('should create an class named "Foo"', async () => {
66+
const tree = await schematicRunner.runSchematicAsync('class', defaultOptions, appTree)
67+
.toPromise();
6568
const fileContent = tree.readContent('/projects/bar/src/app/foo.ts');
6669
expect(fileContent).toMatch(/export class Foo/);
6770
});
6871

69-
it('should put type in the file name', () => {
72+
it('should put type in the file name', async () => {
7073
const options = { ...defaultOptions, type: 'model' };
7174

72-
const tree = schematicRunner.runSchematic('class', options, appTree);
75+
const tree = await schematicRunner.runSchematicAsync('class', options, appTree)
76+
.toPromise();
7377
expect(tree.files).toContain('/projects/bar/src/app/foo.model.ts');
7478
});
7579

76-
it('should split the name to name & type with split on "."', () => {
80+
it('should split the name to name & type with split on "."', async () => {
7781
const options = {...defaultOptions, name: 'foo.model' };
78-
const tree = schematicRunner.runSchematic('class', options, appTree);
82+
const tree = await schematicRunner.runSchematicAsync('class', options, appTree)
83+
.toPromise();
7984
const classPath = '/projects/bar/src/app/foo.model.ts';
8085
const content = tree.readContent(classPath);
8186
expect(content).toMatch(/export class Foo/);
8287
});
8388

84-
it('should respect the path option', () => {
89+
it('should respect the path option', async () => {
8590
const options = { ...defaultOptions, path: 'zzz' };
86-
const tree = schematicRunner.runSchematic('class', options, appTree);
91+
const tree = await schematicRunner.runSchematicAsync('class', options, appTree)
92+
.toPromise();
8793
expect(tree.files).toContain('/zzz/foo.ts');
8894
});
8995

90-
it('should respect the sourceRoot value', () => {
96+
it('should respect the sourceRoot value', async () => {
9197
const config = JSON.parse(appTree.readContent('/angular.json'));
9298
config.projects.bar.sourceRoot = 'projects/bar/custom';
9399
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
94-
appTree = schematicRunner.runSchematic('class', defaultOptions, appTree);
100+
appTree = await schematicRunner.runSchematicAsync('class', defaultOptions, appTree)
101+
.toPromise();
95102
expect(appTree.files).toContain('/projects/bar/custom/app/foo.ts');
96103
});
97104
});

packages/schematics/angular/class/schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
}
5353
},
5454
"required": [
55-
"name",
56-
"project"
55+
"name"
5756
]
5857
}

0 commit comments

Comments
 (0)