Advertisement
Advanced Angular, Angular Schematics & Custom Builders

How would you create a custom schematic to generate a component following your own team's specific conventions?

Short answer

You create a new schematic project using the Angular schematics CLI tools, define a rule function that describes what files to create and how to transform them, and package it as an npm library that your team can install and run using ng generate.

Save Angular mock interview
Advertisement
AModel answer
You create a new schematic project using the Angular schematics CLI tools, define a rule function that describes what files to create and how to transform them, and package it as an npm library that your team can install and run using ng generate.
// A simplified custom schematic rule function
export function myComponent(options: any): Rule {
  return (tree: Tree, context: SchematicContext) => {
    const templateSource = apply(url('./files'), [template({ ...options })]);
    return chain([mergeWith(templateSource)])(tree, context);
  };
}
Real-world example
A large company builds a custom schematic that generates a new component pre wired with their specific logging setup and testing patterns, ensuring every new component across dozens of teams starts with the same consistent foundation.
Likely follow-ups
  • How do you test a custom schematic before publishing it to your team?
  • How would you add custom command line options to your schematic, similar to how ng generate component accepts flags?
Related topics