feat: add ng add schematic support - #6
Merged
Merged
Conversation
Adds an ng-add schematic so consumers can run `ng add @openng/cashew`: the CLI installs the package and the schematic registers provideHttpCache() and the cache interceptor in the application config. - registers provideHttpClient(withInterceptors([withHttpCacheInterceptor()])) in a fresh app via addRootProvider - merges withHttpCacheInterceptor() into an existing provideHttpClient call via TypeScript AST (adds or extends withInterceptors) - warns with a manual snippet for legacy HttpClientModule setups or unparseable code, and is idempotent on repeated runs - ships compiled schematics in dist via build:schematics (postbuild:lib) and ng-package assets - adds jest-based schematic tests (test:schematics) wired into CI Closes openng-org#3 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Author
|
hi @geromegrignon. I've already had experience in implementing this feature in the past. I've written some tests to verify the behaviour of this schematic. I'd be interested to know if there are other effective ways to test this feature. |
geromegrignon
self-requested a review
July 21, 2026 09:24
Member
|
Thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3
What
Adds an
ng-addschematic so cashew can be installed and configured with a single command:The CLI installs the package (saved to
dependencies) and the schematic automatically registersprovideHttpCache()and the cache interceptor in the application config — no manual wiring needed.How it works
The schematic covers the following scenarios:
provideHttpClient) — addsprovideHttpClient(withInterceptors([withHttpCacheInterceptor()]))andprovideHttpCache()viaaddRootProviderfrom@schematics/angular/utility, which transparently supports both standalone (app.config.ts) and NgModule-based apps.provideHttpClient(...)— merges via TypeScript AST: appends awithInterceptors([withHttpCacheInterceptor()])feature argument, or appendswithHttpCacheInterceptor()to an already existingwithInterceptors([...])array, merging import statements.HttpClientModule/ unparseable code — never makes an ambiguous edit: still addsprovideHttpCache()and logs a warning with the manual setup snippet.Only public, stable APIs are used (
@angular-devkit/schematics,addRootProvider/readWorkspace,typescript), available since@schematics/angular16.1 — safe across the whole supported peer range (@angular/core >=17). No private deep imports, following the Angular Material approach.Build & CI changes
schematics/folder compiled with its owntsconfig.json(CommonJS) intodist/openng/cashew/schematicsvia the newbuild:schematicsscript, hooked intopostbuild:lib;collection.json/schema.jsonshipped through ng-packagrassets.package.jsonnow declares"schematics"and"ng-add": { "save": "dependencies" }.test:schematicsscript (Jest +SchematicTestRunner, Node environment, 9 tests) added as a CI step.ng add @openng/cashewas the recommended installation path.Testing
npm pack), generated a fresh app with Angular CLI 20, ranng addfrom the tarball — verified both a clean app and an app with a pre-existingprovideHttpClient(withFetch(), withInterceptors([authInterceptor])); the resulting apps build successfully.