Your first @angular/schematics
The Angular team did it again šš!!
š¤ Angular Schematics itās a new amazing function for the devs!!
Butā¦ What is @angular schematics?
This article itās a little introduction byš§ Hansš§
Okey, sound good š¤ā¦ .
But, Iām a Angular š§Gandalfš§ Developer, Why I need create a file into my āAngular projectā?
I can think of a lot of possibilitiesā¦
When you work team is big (or notš¤·ā), you need a guide lines for your code, and not ever you better option is āng g c my-componentā or āng g s my-serviceā etc etc etc ā¦
When you create a library you can add a āstoriesā like a Angular CLI style https://github.com/angular/angular-cli/wiki/stories
When your team use ngrx or other library for manager states (or other type of library) you need create a LOT of files with the same format
AND Angular Schematics is your solution
Itās code time
At this moment you need have install the angular/schematics
how??
npm install -g @angular-devkit/schematics-cli
Okey itās time to create your first schematics :)
schematics blank --name=my-first-schema
In this moment you have a little schematics for create a simple file š
at this moment we will teleport to another developer site šÆ
hands on the keyboard
Itās time to write a magic codeā¦
The first file you need change is the collection.json
$schema => is the url to the definition for the collection-schema.
schematics => Is the options for your schematics š .
my-first-schema => Is the schematics cli name :).
aliases => the alias.
factory => is the url for your folder code ( when you write, the schematic ).
Description => a simple description.
Schema => the url to options for your schema.
With this changes you have the configuration for your collection.
In this file you have a options for your schema ā¦ itās like the ā ā change-detectionā
ng g c component-name --change-detection
In this options you can add the type, default value, description, aliasā¦
After create the json with the options you need create the interface for thisā¦
Itās index time
The first and more important:
If you have plugins in your text editor it is possible that you get a lot of errors, the style of not finding any import or some specific function.
this file is the one that creates the
Classify is for a little magic in the templates for the schematics.
filterTemplates is a filter for use or add more files.
option.path itās very important you use this option for create the folders and files in the angular app.
templateSource use the cli options and ābuildā the files into ā./filesā for create you final template (with the cli options changes)
Itās Angular time šš
Itās time to write angular code (or like angular code), into the files folder, have all files for your final templateā¦
The first file is the component (my-first-schema.component.ts), in this file you have your angular functionality ā¦
BUT, when you see the code have a strange tagsā¦ like PHP/ASP/other SSR languagesā¦
<%= classify(name) %>
This is strangeā¦ If you review the post a bit, let us know that we were going to teleport.
When write the options, one of this is the ānameā and is a stringā¦
The classify function is write the value of each options.
Classify have the power for read the options š¤Øā¦ If you do not believe me, you only need wait a little minutes moreā¦
HTML time šØāš»
A little difference is the ātagā for use ā¦ You donāt use the equals (if you use the equals, the app never build š®ā)ā¦ In this case I use the value (boolean), if you write in the cli ā ā serviceā this value is true, if not the value for default is false.
<% if (service) %>
Console time šµ
Yeah, you need build this project (itās in typescript), after this you need ābuildā your schematicsā¦ and create a new project for test your schematics š§ā
Only for test
copy all file (except node_modules & package-lock.json) into your angular cli project to test-schematics/node_modules/mfs ( the mfs is the config in src/collection.json file ).
I guess you know what time it isā¦
YES, itās time to implement you schematics to the projectā¦
Okey, its alive, but the template work š¤??
Letās do a quick review:
- we install schematics
- we create a project
- we modify the configuration to support certain options
- modify the index to generate a set of files that were inside the files folder
- inside the files folder we created a ātemplateā of what would be a component in angular
- within that template we use classify to decide what to show inside the html and the component
- We made a build of everything
- we create an application in angular with the cli
- add to the node_modules the schematics (only for test, you have to make a link or upload it)
- we execute the schematics command and we pass options
- it all worked, youāre already āāa Gandalf of the schematics ā
Code: https://github.com/jorgeucano/my-first-schematics-es
To write this article, learn a lot from the following articles:
After more than 9 years working in the tech industry, Jorge is a full stack developer with focus on JavaScript technologies. Today, Jorge is working at ByteDefault using Angular / RxJS / Firebase / Nativescript as well as other technologies. He delivered several courses and talks as well as write some tech articles on these subjects. Because of that, he was recognised as a Google Developer Expert in Angular & web technologies and Nativescript Developer Expert.
Before leaving, I leave all the places for which it is possible to see what Iām doing, or just to be greeted =).
Iām write other article in english for angular:
Thanks for read š
JC