Hey there, schematic enthusiasts! If you’re new to schematics, you might be wondering how to extend existing ones without starting from scratch. You probably just want to add some new functionality without reinventing the wheel. Unfortunately, there’s not a lot of information out there on how to do this, so I’m here to help.
In this tutorial, we’ll dive into extending an existing schematic, specifically the component schematic from Angular. Let’s get started!
Problem to Solve
Here’s what we’ll be doing:
Keep all the questions/inputs that the original one has.
Implement the logic that this schematic does.
Add a Storybook file after this.
Tips
Be careful about where you will use this schematic.
Understand the behavior of the schematic you will extend.
Based on these tips, let’s define those:
Where will this Schematic be used?
We’ll be using it in Angular environments. Angular has a unique feature where ng new [app-name] not only creates an application but also sets up a workspace with a default app.
In Angular, you can configure where to create new sub-projects. This adds complexity because while we can create our extended schematic for our own project, it might not work with different configurations. So, we’ll read the angular.json file to know the base path.
Does the Schematic have important behavior?
Yes! For example, when you specify the component name, you can include a path, like /home/components/carousel. Executing ng g c home/components/carousel will create the carousel component in home/components.
Now that we know what we need, let’s dive in!
Step-by-Step Guide
1. Install the CLI
First, install the CLI tool globally:
2. Create a New Schematic Project
Next, create a new schematic project:
3. Create Your Component Schematic
Generate a new component schematic:
4. Extend Component Inputs
We need to add inputs/questions to get before the factory starts. These go into the schema.json file.
Here’s a typical schema.json:
But we can play with json-schemas using anyOf, allOf, oneOf, or not. For our scenario, we’ll use anyOf and allOf. These help mix more than one schema.
The component schematic needs a project input. Angular CLI provides this automatically, but we’ll handle it if the user doesn’t specify.
Here’s our modified schema.json:
$ref attribute will read an schema with the http protocol, I am using unpkg to read this schema. Please go to npm to the tab Code to see where are that schema that you want to extends.
5. Recreate Interfaces Based on the schema.json
Execute the command:
6. Modify the Factory to Extend the Schematic
Before you continues, please add this utils from this repo to your project:
Add a new file named __name@dasherize__.stories.ts.template in a folder called files.
8. Add the Storybook File
Here’s how to add the Storybook file:
9. Build It
Compile your project:
10. Test It
Locally
Test your schematic locally in an Angular application:
Verdaccio
To test it as if deployed to a package manager like npm, start Verdaccio. If you don’t have it, follow the instructions on the official page and configure a local npm user.