mvp-schematic
v2.0.1
Published
A functional schematic for Angular CLI to generate MVP (Model-View-Presenter) structured components, supporting both classic (NgModule) and modern (standalone) Angular approaches.
Readme
MVP Schematic
A functional schematic for Angular CLI to generate MVP (Model-View-Presenter) structured components, supporting both classic (NgModule) and modern (standalone) Angular approaches.
Installation
Install the package in your Angular project:
npm install mvp-schematicSetup
After installing, run the setup command to copy the interactive generator script and add the npm script to your project:
npx mvp-schematic postinstallThis will:
- Create a
scriptsfolder (if it doesn't exist). - Copy the
generate-mvp.jsinteractive generator script intoscripts/. - Add the
generate:mvpscript to yourpackage.json.
Usage
Interactive Generator
Run the following command to launch the interactive MVP component generator:
npm run generate:mvpYou will be prompted for:
- Component name: Must not contain spaces or special characters.
- Target path: Where to generate the component (default:
src/app/features). - Use Angular standalone style? If yes, the schematic will generate standalone components and routing using the latest Angular patterns.
- Include presenter.ts file?
- Include module.ts file? (only if not standalone)
- Include routing.ts file?
- Include .scss file?
The generator will then run the schematic with your selected options.
Direct Schematic Usage
You can also use the schematic directly with Angular CLI:
ng generate mvp-schematic:mvp --name=example --standalone=true --with-presenter=true --with-module=false --with-routing=true --with-styles=true --target-path='src/app/features'Features
- Supports both classic (NgModule) and standalone Angular component generation.
- Modern routing: When using standalone, generates routes as
export default [] as Routes;. - Presenter pattern: Optionally generate a presenter service for the MVP pattern.
- Customizable: Choose whether to include styles, routing, presenter, and module files.
- Version-aware: Automatically adapts to Angular versions where standalone is the default (Angular 19+).
Development & Local Testing
To test your schematic locally before publishing to npm:
Build the schematic:
npm run buildLink the package globally:
npm linkIn your Angular project, link the schematic:
npm link mvp-schematicRun the generator as described above.
To unlink after testing:
npm unlink mvp-schematicPublishing
- Ensure the
distfolder and all necessary files are included (see thefilesfield inpackage.json). - Bump the version in
package.jsonas needed. - Run:
npm publish
Notes
- The schematic detects your Angular version and omits the
standalone: trueproperty if your project uses Angular 17+ (where standalone is the default). - The interactive generator validates the component name to avoid spaces or special characters.
- All templates are separated into
classicandstandalonefolders for maintainability.
License
MIT
