Update and insert auto-generated code to existing Typescript, HTML and JSON files with Angular schematics Pt.2

Aziz Haddad
Humanitec
Published in
3 min readOct 29, 2018

This is the second part of my “Modify and add auto-generated code to existing TypeScript, HTML and JSON files with angular schematics” series. If you didn’t read that yet, I recommend you to do as I will start from where I left off in the first part.

As promised, this part will be about how to auto-generate code into existing JSON and HTML files which is something that I struggled personally to figure out due to the lack of documentation and online content. I wanted to share with you because of the same reason. So, now enough talk, let’s get into the real deal.

How to add auto-generated code into an existing HTML file

Assuming that you already read the first part, you now have a schematic that is calling one rule to add an object to an array in a TypeScript file. Let’s say now we want to add another rule to add an HTML element to an existing one when running the same schematics.

In order to do this, we have to (I am sure you’ve guessed it already) create another rule.

But before that, let’s create the HTML file that we want to add the generated code into.

Now, we have to start on how to create our rule to generate another list element. In order to do this, we have to use an HTML parser which will make our lives a lot easier because we can access DOM elements, for this example, we will use cheerio. So, make sure you install it by running the following commands:

npm i cheerio

npm i @types/cheerio --save

Our rule should read the file as a string, parse it to HTML, append the new bullet to the list element with class “people-list” and then simply overwrite the file with the modified HTML

Now, we have to add this rule to the chain function that we already created in the previous part under src/tutorial-schematic/index.ts. The final tutorialSchematic function will look like this:

When running the schematic now, both objects should be added to the array in people.ts and the bullet point should be added to the <ul> element in people.html file.

How to add auto-generated code into an existing JSON file

To create a rule to update a JSON file is the easiest part, you just have to parse the JSON file -> do the changes -> overwrite the file with the stringified updated JSON -> BAM! Easy isn’t it?

Let’s say we want to update this JSON file and add another entry to the people array.

What makes our life easier is that there are a lot of utility functions that are already done by the angular team which you can find in the @angular/dev-kit package. One of them does exactly what we want — update a JSON file. So, I copied from there and call it in our rule which will end up to be like this:

The final step is as always to add the created rule to the index.ts file of the schematics. Our final index.ts file will look like this.

Now, just run your schematics and enjoy the power of auto-code generation.

In these articles, I tried to cover some of the common use cases of using schematics to update existing files, shared my learnings and tried to demystify a little bit more on what I think is an amazing tool which is unfortunately very poorly documented. Schematics can actually do a whole lot more than this and hopefully, we will get an official documentation soon for all of that.

--

--

Aziz Haddad
Humanitec

Senior Frontend Engineer at Humanitec GmbH and Angular enthusiast