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

Commit 3bf5ec6

Browse files
Broccohansl
authored andcommitted
fix(@schematics/angular): Remove module option from service schematic
fixes angular/angular-cli#10170
1 parent 33930a4 commit 3bf5ec6

File tree

5 files changed

+3
-86
lines changed

5 files changed

+3
-86
lines changed

packages/schematics/angular/service/files/__name@dasherize@if-flat__/__name@dasherize__.service.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Injectable } from '@angular/core';<% if (providedIn) { %>
2-
import { <%= providedIn %> } from '<%= providedInPath %>';<% } %>
1+
import { Injectable } from '@angular/core';
32

43
@Injectable({
5-
providedIn: <%= providedIn || "'root'" %>
4+
providedIn: 'root'
65
})
76
export class <%= classify(name) %>Service {
87

packages/schematics/angular/service/index.ts

+1-52
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, strings } from '@angular-devkit/core';
8+
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicContext,
12-
SchematicsException,
1312
Tree,
1413
apply,
1514
filter,
@@ -19,41 +18,12 @@ import {
1918
template,
2019
url,
2120
} from '@angular-devkit/schematics';
22-
import * as ts from 'typescript';
23-
import { getFirstNgModuleName } from '../utility/ast-utils';
2421
import { getWorkspace } from '../utility/config';
25-
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
2622
import { parseName } from '../utility/parse-name';
2723
import { Schema as ServiceOptions } from './schema';
2824

29-
function getModuleNameFromPath(host: Tree, modulePath: Path) {
30-
if (!host.exists(modulePath)) {
31-
throw new SchematicsException(`File ${modulePath} does not exist.`);
32-
}
33-
34-
const text = host.read(modulePath);
35-
if (text === null) {
36-
throw new SchematicsException(`File ${modulePath} cannot be read.`);
37-
}
38-
const sourceText = text.toString('utf-8');
39-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
40-
41-
return getFirstNgModuleName(source);
42-
}
43-
44-
function stripTsExtension(path: string): string {
45-
if (!path.endsWith('.ts')) {
46-
throw new SchematicsException(`File ${path} is not a Typescript file.`);
47-
}
48-
49-
return path.substr(0, path.length - 3);
50-
}
51-
5225
export default function (options: ServiceOptions): Rule {
5326
return (host: Tree, context: SchematicContext) => {
54-
let providedByModule = '';
55-
let providedInPath = '';
56-
5727
const workspace = getWorkspace(host);
5828
if (!options.project) {
5929
options.project = Object.keys(workspace.projects)[0];
@@ -64,25 +34,6 @@ export default function (options: ServiceOptions): Rule {
6434
options.path = `/${project.root}/src/app`;
6535
}
6636

67-
if (options.module) {
68-
const modulePath = findModuleFromOptions(host, options);
69-
if (!modulePath || !host.exists(modulePath)) {
70-
throw new Error('Specified module does not exist');
71-
}
72-
providedByModule = getModuleNameFromPath(host, modulePath) || '';
73-
74-
if (!providedByModule) {
75-
throw new SchematicsException(`module option did not point to an @NgModule.`);
76-
}
77-
78-
const servicePath = `/${options.path}/`
79-
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
80-
+ strings.dasherize(options.name)
81-
+ '.service';
82-
83-
providedInPath = stripTsExtension(buildRelativePath(servicePath, modulePath));
84-
}
85-
8637
const parsedPath = parseName(options.path, options.name);
8738
options.name = parsedPath.name;
8839
options.path = parsedPath.path;
@@ -93,8 +44,6 @@ export default function (options: ServiceOptions): Rule {
9344
...strings,
9445
'if-flat': (s: string) => options.flat ? '' : s,
9546
...options,
96-
providedIn: providedByModule,
97-
providedInPath: providedInPath,
9847
}),
9948
move(parsedPath.path),
10049
]);

packages/schematics/angular/service/index_spec.ts

-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('Service Schematic', () => {
2020
const defaultOptions: ServiceOptions = {
2121
name: 'foo',
2222
spec: true,
23-
module: undefined,
2423
flat: false,
2524
};
2625

@@ -62,26 +61,6 @@ describe('Service Schematic', () => {
6261
expect(content).toMatch(/providedIn: 'root'/);
6362
});
6463

65-
it('should import a specified module', () => {
66-
const options = { ...defaultOptions, module: 'app.module.ts' };
67-
68-
const tree = schematicRunner.runSchematic('service', options, appTree);
69-
const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts');
70-
expect(content).toMatch(/import { AppModule } from '..\/app.module'/);
71-
expect(content).toMatch(/providedIn: AppModule/);
72-
});
73-
74-
it('should fail if specified module does not exist', () => {
75-
const options = { ...defaultOptions, module: '/projects/bar/src/app/app.moduleXXX.ts' };
76-
let thrownError: Error | null = null;
77-
try {
78-
schematicRunner.runSchematic('service', options, appTree);
79-
} catch (err) {
80-
thrownError = err;
81-
}
82-
expect(thrownError).toBeDefined();
83-
});
84-
8564
it('should respect the spec flag', () => {
8665
const options = { ...defaultOptions, spec: false };
8766

packages/schematics/angular/service/schema.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,4 @@ export interface Schema {
2727
* Specifies if a spec file is generated.
2828
*/
2929
spec?: boolean;
30-
/**
31-
* Allows specification of the providing module.
32-
*/
33-
module?: string;
3430
}

packages/schematics/angular/service/schema.json

-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
"type": "boolean",
3333
"default": true,
3434
"description": "Specifies if a spec file is generated."
35-
},
36-
"module": {
37-
"type": "string",
38-
"default": "",
39-
"description": "Allows specification of the providing module.",
40-
"alias": "m"
4135
}
4236
},
4337
"required": []

0 commit comments

Comments
 (0)