Search code examples
angularangular-schematics

Custom schematics in Angular/CLI 6.x builds without errors but I cannot use it to generate files


I've created a few schematics before, before Angular 6.0. The same steps I did in Angular 5.x now always leads to the error schematic 'my-schematic' not found in collection @schematics/angular when I try to generate some code on a hosting application (of Angular 6)?.

My setup (global Angular stuff):

Angular CLI: 6.0.0
Node: 8.11.1
OS: win32 x64

@angular-devkit/{architect, core, schematics} 0.6.0
@schematics/angular 0.6.0
rxjs 6.1.0
typescript 2.7.2

My steps:

  1. Ran schematics schematic --name=my-first-schematics. So it creates a new schematics project with three sample schemas in it. Then I navigated into that new folder cd my-first-schematics.
  2. Ran npm run build. It built without errors.
  3. Ran schematics .:my-first-schematics. Result: Error "my-first-schematics" not found in collection "." I saw in some blogs that they performed this step without problems, though. I never used to run this command, and I'm not sure what it actually does, so I'll proceed assuming that this command isn't necessary after all.
  4. Ran npm link so that I can use this new schematics collection in a new project.
  5. In another prompt window, I created a new application: ng new schematics-consumer, then after the npm installs, cd schematics-consumer.
  6. Ran npm link my-first-schematics so I now have access to that schematics library.
  7. I navigated to src/app as that's where I want to generate my file.
  8. Ran ng g my-schematic -c my-first-schematics. Resulted in error Schematic "my-schematic" not found in collection "@schematics/angular". I specified a collection. Why is it not seeing my new collection, and it's still thinking that every schematic is in @schematics/angular?

So, I cannot generate anything with schematics. I did not modify anything in the default code generated by schematics schematic name=my-first-schematics.

Is there a new set of commands, procedures, etc. to create our own custom schematics? What steps am I missing? What else do I need to install? I'll provide more information if necessary.

At this time, there are no new guides or blogs on schematics, as 6.0 is only a few weeks old, so I'm assuming the old way should still work.


Solution

  • It seems like the issue is ultimately with angular-cli, see discussion: https://github.com/angular/devkit/issues/528#issuecomment-387415646

    To get around this, you can set your default collection to your custom schematics:

    ng config cli.defaultCollection my-first-schematics
    

    Now you should be able to run ng g my-schematic.

    If this does not work can you post the file list of your my-first-schematics directory?