Your first @angular/schematics

Jorge Cano
7 min readFeb 23, 2018

--

The Angular team did it again šŸ˜šŸ˜!!

https://mashable.com/2017/07/13/cult-of-the-party-parrot-slack-reddit-meme/#PBb.P.tmUSqV

šŸ¤  Angular Schematics itā€™s a new amazing function for the devs!!

Angular labs :)

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

src/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.

src/my-first-schema/schema.json

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ā€¦

/src/my-first-schema/schema.ts

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)

src/my-first-schema/index.ts

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ā€¦

src/my-first-schema/files/

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ā€¦

src/my-first-schema/files/my-first-schema.component.ts

HTML time šŸ‘Øā€šŸ’»

src/my-first-schema/files/my-first-schema.component.html

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ā€¦

ng g my-first-schema mfs ā€” service ā€” name=ā€Mfsā€ ā€” collection mfs

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 ā€

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.

Thanks for read šŸ‘Œ

JC

--

--

Jorge Cano

Angular Google Developer Expert ā€” Principal Engineer