How can I modify the default CLI templates for creating page or provider with the 'generate' option?

I always divide my code using comments for every class I write, this helps me a lot with the readability of my own code. It is a pain to have to add these sections to all classes, pages and providers I write and since using the generator is a great way to save time, I thought it would be awesome if I could add my comment divisions to the templates that are generated by the Ionic 2 CLI generate option. (Please notice that I am talking about the code generated with the ‘generate’ option of the CLI for Pages and Providers, not the starter projects for which Ionic is highly configurable by allowing to use CodePen, Github, etc, for a starter. In this case I have not been able to find where these templates are.

Where are these templates? Is it possible to edit them to add the comments I always use?

Thanks in advance.

Could you explain a bit more about what you want?

sure, we would love that when the CLI creates an angular provider, it could add some custom code (comments) that we use in all our code. Otherwise when the CLI finish generating the provider we have to manually add the code divisions we use. For example for a component we add some comments to divide the different sections of the source file like,

//------------------------------------------------------------
// Imports Section:
//------------------------------------------------------------
import {Component, Input, Output, EventEmitter, OnInit} from ‘angular2/core’;
import {IONIC_DIRECTIVES} from ‘ionic-angular’;

//------------------------------------------------------------
// Component Declaration Section:
//------------------------------------------------------------
@Component({
selector : ‘x-component’,
templateUrl : ‘build/components/x-component/x-component.html’,
directives : [IONIC_DIRECTIVES]
})
//------------------------------------------------------------
// Component Class Section:
//------------------------------------------------------------
etc…

Modifying the template for the CLI code generation option would allow us also to automatically have a provider generated with an observable instead of a promise (the current default), etc. in a uniform way by using the CLI.

I have used CodeSmith in the past and I just thought that probably the Ionic CLI uses templates like CodeSmith to generate its providers and Pages. If that is the case, if I knew where the templates are, I could just modify them by adding our own code preferences and comments and therefore customize the end result of the generation. I almost fee guilty for asking this, since it is not a blocker for our work, but it would help us to make things more automatic and consistent.

All the generate commands are actually part of the ionic-angualr package.

https://github.com/driftyco/ionic/tree/2.0/tooling

1 Like

I think the link was moved.

https://github.com/driftyco/ionic/tree/master/tooling

Thanks :slight_smile: What would be the best way to implement slight changes to the templates? I mean, by changing and overriding ionic-angular in node_modules, would pollute the original code in Ionic CLI, otherwise by creating a branch apart would take us out of the official channel and therefore the product updates from npm, Is there a way to implement our generators as an add-in to the ionic-cli? something perhaps like the yeoman generators where you can create your own scaffolds? I know this issue is not critical at all and there are other ways to do this, so I feel a little bit hesitant to bother you with this, but it is just a gut feeling that tells me to push this a little bit because it would be fantastic to be able to generate lots of services, directives, view scaffolds, etc. with added custom code for managing all kinds of use cases. Adding lots of templates would be a form to reuse code and handling our kb in a way that we would have as a team a single place where to go from problem to implementation in a single step. A way to streamline the process of sending emails to the team asking same question over and over and adapting code from the same stack overflow article once and again, instead of that, just to have our wiki with a simple instruction to call the ionic CLI generator with a specific template to get the problem solved. I hope I’m making sense. In any case I thank you for taking the time to read this post and consider my question.

I think this is still very relevant! I want to import my SharedModule in every ionic g page name-here for example… Any news on this?

Noting that with ionic-v3 we can use angular schematics, which make it possible to make such templates and use them with ng generate