Skip to content

Commit 5bb54d1

Browse files
committed
refactor(@schematics/angular): update service to use new workspace rules
1 parent 1584822 commit 5bb54d1

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

packages/schematics/angular/service/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicsException,
1211
Tree,
1312
apply,
1413
applyTemplates,
@@ -21,18 +20,13 @@ import {
2120
} from '@angular-devkit/schematics';
2221
import { applyLintFix } from '../utility/lint-fix';
2322
import { parseName } from '../utility/parse-name';
24-
import { buildDefaultPath, getProject } from '../utility/project';
23+
import { createDefaultPath } from '../utility/workspace';
2524
import { Schema as ServiceOptions } from './schema';
2625

2726
export default function (options: ServiceOptions): Rule {
28-
return (host: Tree) => {
29-
if (!options.project) {
30-
throw new SchematicsException('Option (project) is required.');
31-
}
32-
const project = getProject(host, options.project);
33-
27+
return async (host: Tree) => {
3428
if (options.path === undefined) {
35-
options.path = buildDefaultPath(project);
29+
options.path = await createDefaultPath(host, options.project as string);
3630
}
3731

3832
const parsedPath = parseName(options.path, options.name);

packages/schematics/angular/service/index_spec.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,41 @@ describe('Service Schematic', () => {
4141
appTree = schematicRunner.runSchematic('application', appOptions, appTree);
4242
});
4343

44-
it('should create a service', () => {
44+
it('should create a service', async () => {
4545
const options = { ...defaultOptions };
4646

47-
const tree = schematicRunner.runSchematic('service', options, appTree);
47+
const tree = await schematicRunner.runSchematicAsync('service', options, appTree)
48+
.toPromise();
4849
const files = tree.files;
4950
expect(files).toContain('/projects/bar/src/app/foo/foo.service.spec.ts');
5051
expect(files).toContain('/projects/bar/src/app/foo/foo.service.ts');
5152
});
5253

53-
it('service should be tree-shakeable', () => {
54+
it('service should be tree-shakeable', async () => {
5455
const options = { ...defaultOptions};
5556

56-
const tree = schematicRunner.runSchematic('service', options, appTree);
57+
const tree = await schematicRunner.runSchematicAsync('service', options, appTree)
58+
.toPromise();
5759
const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts');
5860
expect(content).toMatch(/providedIn: 'root'/);
5961
});
6062

61-
it('should respect the skipTests flag', () => {
63+
it('should respect the skipTests flag', async () => {
6264
const options = { ...defaultOptions, skipTests: true };
6365

64-
const tree = schematicRunner.runSchematic('service', options, appTree);
66+
const tree = await schematicRunner.runSchematicAsync('service', options, appTree)
67+
.toPromise();
6568
const files = tree.files;
6669
expect(files).toContain('/projects/bar/src/app/foo/foo.service.ts');
6770
expect(files).not.toContain('/projects/bar/src/app/foo/foo.service.spec.ts');
6871
});
6972

70-
it('should respect the sourceRoot value', () => {
73+
it('should respect the sourceRoot value', async () => {
7174
const config = JSON.parse(appTree.readContent('/angular.json'));
7275
config.projects.bar.sourceRoot = 'projects/bar/custom';
7376
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
74-
appTree = schematicRunner.runSchematic('service', defaultOptions, appTree);
77+
appTree = await schematicRunner.runSchematicAsync('service', defaultOptions, appTree)
78+
.toPromise();
7579
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.service.ts');
7680
});
7781
});

0 commit comments

Comments
 (0)