Angular Schematic Generator: Create a Simple Component
This challenge asks you to build a basic Angular schematic generator that creates a new component with a predefined structure. Schematic generators are a powerful way to automate repetitive tasks in Angular development, ensuring consistency and reducing boilerplate code. This exercise will familiarize you with the Angular CLI's schematic generation capabilities.
Problem Description
You need to create an Angular schematic generator named my-component that, when executed, generates a new Angular component with the following structure:
- A component file (
my-component.component.ts) containing:- A simple class with a constructor.
- A public property named
messageinitialized to "Hello from My Component!".
- An HTML template file (
my-component.component.html) containing:- A
<div>element displaying the value of themessageproperty using interpolation ({{ message }}).
- A
- A CSS stylesheet file (
my-component.component.css) containing:- A simple CSS rule setting the color of the
<div>to blue.
- A simple CSS rule setting the color of the
- A component declaration file (
my-component.module.ts) that declares the component.
The schematic should prompt the user for the component's name (e.g., "MyComponent") and create the files within a directory named after the provided component name. The schematic should also automatically import and declare the new component in the root module (assuming a root module named AppModule exists).
Key Requirements:
- The schematic must be written in TypeScript.
- The schematic must prompt the user for the component name.
- The generated files must adhere to the specified structure.
- The schematic must correctly import and declare the component in the root module (
AppModule). - Error handling: If the root module cannot be found, the schematic should log an error and exit gracefully.
Expected Behavior:
- When the schematic is run, it should prompt the user for the component name.
- Upon receiving the component name, it should create the necessary files in a directory named after the component name.
- The generated component should display "Hello from My Component!" in blue within a
<div>element. - The component should be correctly declared in the
AppModule. - If
AppModuleis not found, an error message should be displayed.
Edge Cases to Consider:
- What happens if the user provides an invalid component name (e.g., containing spaces or special characters)? (For simplicity, assume the name will be valid).
- What happens if a directory with the same name as the component already exists? (For simplicity, assume it doesn't).
- What happens if the
AppModuledoesn't exist?
Examples
Example 1:
Input: Schematic execution with user input "MyNewComponent"
Output: A directory named "my-new-component" is created containing:
- my-new-component.component.ts
- my-new-component.component.html
- my-new-component.component.css
- my-new-component.module.ts
- AppModule is updated to include the new component.
Explanation: The schematic successfully generated the component and declared it in the root module.
Example 2:
Input: Schematic execution with user input "Invalid Component Name" (assuming this is allowed)
Output: Error message indicating the component name is invalid (or the schematic handles it gracefully).
Explanation: The schematic handles invalid input appropriately.
Example 3:
Input: Schematic execution when AppModule does not exist.
Output: Error message indicating that AppModule was not found.
Explanation: The schematic gracefully handles the absence of the root module.
Constraints
- The schematic must be compatible with Angular 14 or higher.
- The schematic should be written in a modular and maintainable way.
- The schematic should use the
@angular/clilibrary for file system operations and schematic generation. - The root module is assumed to be named
AppModule. - The schematic should not modify any other files besides those explicitly mentioned.
Notes
- You'll need to create an Angular project to test your schematic.
- Consider using the
Treeobject provided by the@angular/clilibrary to manipulate the file system. - The
SchematicTestRunnercan be used for testing your schematic. - Focus on the core functionality of generating the component files and declaring it in the root module. Advanced features like dependency injection or unit testing setup are beyond the scope of this challenge.
- Remember to use the
transformfunction to modify existing files (likeAppModule). - Use
chainto execute multiple schematic operations. - Use
ruleto define a transformation rule.