1

With early versions of the Angular Cli when I run ng g service services/MyService it created:

services/my-service/my-service.service.ts
services/my-service/my-service.service.spec.ts

But now it creates

services/my-service.service.ts
services/my-service.service.spec.ts

Is there a way to go back to the other behavior without write a verbose ng g service services/my-service/MyService ?I had not found anything related but maybe I am not using the correct keywords.

distante
  • 6,438
  • 6
  • 48
  • 90
  • Possible duplicate of [How do I add component defaults in angular cli 6+](https://stackoverflow.com/questions/50651977/how-do-i-add-component-defaults-in-angular-cli-6) – Alexander Staroselsky Jan 31 '19 at 20:12

2 Answers2

5

While you can pass --flat=false each time you execute ng generate so that a directory is created based on the service/pipe/directive name, you can actually override default schematics options such as flat at the project level in angular.json to avoid needing to pass the --flat=false option every time on the command line. For example, to set flat to false when executing ng g service services/MyService, you would add an additional property, @schematics/angular:service, in the schematics property of the respective project in angular.json:

...
"projects": {
    "sample-angular": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:service": {
          "flat": false
        }
     },
     ...
}

After adding this override, running the command ng g service services/MyService, generates the following output:

CREATE src/app/services/my-service/my-service.service.spec.ts (349 bytes)
CREATE src/app/services/my-service/my-service.service.ts (138 bytes)

You can override any specific schematics you need whether that is for Pipes, Services, Components, Modules, or Directives. You can see the default schematics options at /node_modules/@angular/cli/lib/config/schema.json. There are a number of options and you can fine tune exactly what you want generated and how to avoid needing to remember and pass options to the command line.

If you have multiple projects, you can create a property schematics at the same level as projects to override schematic options for all projects.

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Great answer -- while the Angular defaults _are_ there for a reason, if you want to follow your own convention in a given application this is the way to do it. – Nathan Beck Jan 31 '19 at 20:03
  • @NathanBeck when I have more than 5 services I see it a little bit clutter up. Because I have 10 files instead of 5 :( – distante Jan 31 '19 at 20:14
  • @alexander-staroselsky is there a way to apply the same property (`flat`) to all the schematics without the need to write it for each key? (component, pipe, service, etc, etc) – distante Jan 31 '19 at 21:03
  • @distante I don't think so, but I could be wrong. The issue is that not all schematics have the `flat` option including Guard, Enum, and Interface to name a few. You may want to post a new question either here or in the issues of the @angular/cli project as there could be some kind of glob pattern that could target all schematics somehow. – Alexander Staroselsky Jan 31 '19 at 21:13
2

The flat flag defaults to true when generating a service.

I suggest one of the following (haven't tested on Angular CLI 7 but both work with Angular CLI 6)

ng g service services/my-service --no-flat
ng g service services/my-service --flat=false
Nathan Beck
  • 1,152
  • 14
  • 23