Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
* Public API Surface of <%= dasherize(name) %>
*/

export * from './lib/<%= dasherize(name) %>.service';
export * from './lib/<%= dasherize(name) %>';<% if (!standalone) { %>
export * from './lib/<%= dasherize(name) %>.module';<% } %>
6 changes: 0 additions & 6 deletions packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ export default function (options: LibraryOptions): Rule {
standalone: options.standalone,
project: packageName,
}),
schematic('service', {
name: options.name,
flat: true,
path: sourceDir,
project: packageName,
}),
(_tree: Tree, context: SchematicContext) => {
if (!options.skipPackageJson && !options.skipInstall) {
context.addTask(new NodePackageInstallTask());
Expand Down
13 changes: 2 additions & 11 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ describe('Library Schematic', () => {
'/projects/foo/src/my-index.ts',
'/projects/foo/src/lib/foo.spec.ts',
'/projects/foo/src/lib/foo.ts',
'/projects/foo/src/lib/foo.service.spec.ts',
'/projects/foo/src/lib/foo.service.ts',
]),
);
});
Expand Down Expand Up @@ -102,8 +100,6 @@ describe('Library Schematic', () => {
'/some/other/directory/bar/src/my-index.ts',
'/some/other/directory/bar/src/lib/foo.spec.ts',
'/some/other/directory/bar/src/lib/foo.ts',
'/some/other/directory/bar/src/lib/foo.service.spec.ts',
'/some/other/directory/bar/src/lib/foo.service.ts',
]),
);
});
Expand Down Expand Up @@ -207,10 +203,8 @@ describe('Library Schematic', () => {
const project = config.projects.pascalCasedName;
expect(project).toBeDefined();
expect(project.root).toEqual('projects/pascal-cased-name');
const svcContent = tree.readContent(
'/projects/pascal-cased-name/src/lib/pascal-cased-name.service.ts',
);
expect(svcContent).toMatch(/providedIn: 'root'/);
const svcContent = tree.readContent('/projects/pascal-cased-name/src/lib/pascal-cased-name.ts');
expect(svcContent).toContain('@Component');
});

describe(`update package.json`, () => {
Expand Down Expand Up @@ -320,7 +314,6 @@ describe('Library Schematic', () => {

const pkgJsonPath = '/projects/myscope/mylib/package.json';
expect(tree.files).toContain(pkgJsonPath);
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.service.ts');
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.ts');

const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
Expand Down Expand Up @@ -431,8 +424,6 @@ describe('Library Schematic', () => {
'/projects/foo/src/lib/foo.module.ts',
'/projects/foo/src/lib/foo.spec.ts',
'/projects/foo/src/lib/foo.ts',
'/projects/foo/src/lib/foo.service.spec.ts',
'/projects/foo/src/lib/foo.service.ts',
]),
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { <%= classify(name) %><%= classify(type) %> } from './<%= dasherize(name) %><%= type ? '.' + dasherize(type) : '' %>';

describe('<%= classify(name) %><%= classify(type) %>', () => {
let service: <%= classify(name) %><%= classify(type) %>;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(<%= classify(name) %><%= classify(type) %>);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class <%= classify(name) %>Service {
export class <%= classify(name) %><%= classify(type) %> {

constructor() { }
}

This file was deleted.

3 changes: 3 additions & 0 deletions packages/schematics/angular/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default function (options: ServiceOptions): Rule {
const flat = options.flat;
options.flat = true;

// Schematic templates require a defined type value
options.type ??= '';

return generateFromFiles(options, {
'if-flat': (s: string) => (flat ? '' : s),
});
Expand Down
30 changes: 24 additions & 6 deletions packages/schematics/angular/service/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe('Service Schematic', () => {

const tree = await schematicRunner.runSchematic('service', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo/foo.service.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo.service.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo.ts');
});

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

const tree = await schematicRunner.runSchematic('service', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts');
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
expect(content).toMatch(/providedIn: 'root'/);
});

Expand All @@ -63,15 +63,33 @@ describe('Service Schematic', () => {

const tree = await schematicRunner.runSchematic('service', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo/foo.service.ts');
expect(files).not.toContain('/projects/bar/src/app/foo/foo.service.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo.ts');
expect(files).not.toContain('/projects/bar/src/app/foo/foo.spec.ts');
});

it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.bar.sourceRoot = 'projects/bar/custom';
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
appTree = await schematicRunner.runSchematic('service', defaultOptions, appTree);
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.service.ts');
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.ts');
});

it('should respect the type option', async () => {
const options = { ...defaultOptions, type: 'Service' };
const tree = await schematicRunner.runSchematic('service', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.service.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.service.spec.ts');
expect(content).toContain('export class FooService');
expect(testContent).toContain("describe('FooService'");
});

it('should allow empty string in the type option', async () => {
const options = { ...defaultOptions, type: '' };
const tree = await schematicRunner.runSchematic('service', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
expect(content).toContain('export class Foo');
expect(testContent).toContain("describe('Foo'");
});
});
4 changes: 4 additions & 0 deletions packages/schematics/angular/service/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"type": "boolean",
"description": "Skip the generation of a unit test file `spec.ts` for the service.",
"default": false
},
"type": {
"type": "string",
"description": "Append a custom type to the service's filename. For example, if you set the type to `service`, the file will be named `my-service.service.ts`."
}
},
"required": ["name", "project"]
Expand Down
13 changes: 13 additions & 0 deletions packages/schematics/angular/utility/generate-from-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
*/

import {
FileOperator,
Rule,
Tree,
apply,
applyTemplates,
chain,
filter,
forEach,
mergeWith,
move,
noop,
Expand All @@ -31,6 +33,7 @@ export interface GenerateFromFilesOptions {
project: string;
skipTests?: boolean;
templateFilesDirectory?: string;
type?: string;
}

export function generateFromFiles(
Expand All @@ -56,6 +59,16 @@ export function generateFromFiles(
...options,
...extraTemplateValues,
}),
!options.type
? forEach(((file) => {
return file.path.includes('..')
? {
content: file.content,
path: file.path.replace('..', '.'),
}
: file;
}) as FileOperator)
: noop(),
move(parsedPath.path + (options.flat ? '' : '/' + strings.dasherize(options.name))),
]);

Expand Down
5 changes: 2 additions & 3 deletions tests/legacy-cli/e2e/tests/build/library/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function libraryConsumptionSetup(): Promise<void> {
export class MyLibComponent {}`,
'./src/app/app.ts': `
import { Component } from '@angular/core';
import { MyLibService, MyLibComponent } from 'my-lib';
import { MyLibComponent } from 'my-lib';

@Component({
standalone: true,
Expand All @@ -28,8 +28,7 @@ export async function libraryConsumptionSetup(): Promise<void> {
export class App {
title = 'test-project';

constructor(myLibService: MyLibService) {
console.log(myLibService);
constructor() {
}
}
`,
Expand Down
4 changes: 2 additions & 2 deletions tests/legacy-cli/e2e/tests/generate/service/service-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default function () {
return (
ng('generate', 'service', 'test-service')
.then(() => expectFileToExist(serviceDir))
.then(() => expectFileToExist(join(serviceDir, 'test-service.service.ts')))
.then(() => expectFileToExist(join(serviceDir, 'test-service.service.spec.ts')))
.then(() => expectFileToExist(join(serviceDir, 'test-service.ts')))
.then(() => expectFileToExist(join(serviceDir, 'test-service.spec.ts')))

// Try to run the unit tests.
.then(() => ng('test', '--watch=false'))
Expand Down
4 changes: 2 additions & 2 deletions tests/legacy-cli/e2e/tests/misc/es2015-nometa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { prependToFile, replaceInFile } from '../../utils/fs';
import { ng } from '../../utils/process';

export default async function () {
await ng('generate', 'service', 'user');
await ng('generate', 'service', 'user-service');

// Update the application to use the new service
await prependToFile('src/app/app.ts', "import { UserService } from './user.service';");
await prependToFile('src/app/app.ts', "import { UserService } from './user-service';");

await replaceInFile(
'src/app/app.ts',
Expand Down