Skip to content

feat: add ng add schematic support - #6

Merged
geromegrignon merged 1 commit into
openng-org:mainfrom
valeriocomo:feat/ng-add-schematic
Jul 21, 2026
Merged

geromegrignon merged 1 commit into
openng-org:mainfrom
valeriocomo:feat/ng-add-schematic

Conversation

@valeriocomo

Copy link
Copy Markdown
Contributor

Closes #3

What

Adds an ng-add schematic so cashew can be installed and configured with a single command:

ng add @openng/cashew

The CLI installs the package (saved to dependencies) and the schematic automatically registers provideHttpCache() and the cache interceptor in the application config — no manual wiring needed.

How it works

The schematic covers the following scenarios:

  • Fresh app (no provideHttpClient) — adds provideHttpClient(withInterceptors([withHttpCacheInterceptor()])) and provideHttpCache() via addRootProvider from @schematics/angular/utility, which transparently supports both standalone (app.config.ts) and NgModule-based apps.
  • Existing provideHttpClient(...) — merges via TypeScript AST: appends a withInterceptors([withHttpCacheInterceptor()]) feature argument, or appends withHttpCacheInterceptor() to an already existing withInterceptors([...]) array, merging import statements.
  • Legacy HttpClientModule / unparseable code — never makes an ambiguous edit: still adds provideHttpCache() and logs a warning with the manual setup snippet.
  • Idempotent — running it twice produces no duplicates.

Only public, stable APIs are used (@angular-devkit/schematics, addRootProvider/readWorkspace, typescript), available since @schematics/angular 16.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 own tsconfig.json (CommonJS) into dist/openng/cashew/schematics via the new build:schematics script, hooked into postbuild:lib; collection.json/schema.json shipped through ng-packagr assets.
  • Library package.json now declares "schematics" and "ng-add": { "save": "dependencies" }.
  • New test:schematics script (Jest + SchematicTestRunner, Node environment, 9 tests) added as a CI step.
  • README now documents ng add @openng/cashew as the recommended installation path.

Testing

  • ✅ 9/9 schematic tests, 46/46 existing library tests.
  • ✅ End-to-end: packed the built library (npm pack), generated a fresh app with Angular CLI 20, ran ng add from the tarball — verified both a clean app and an app with a pre-existing provideHttpClient(withFetch(), withInterceptors([authInterceptor])); the resulting apps build successfully.

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>
@valeriocomo

Copy link
Copy Markdown
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
geromegrignon self-requested a review July 21, 2026 09:24
@geromegrignon
geromegrignon merged commit 5ecf407 into openng-org:main Jul 21, 2026
1 check passed
@geromegrignon

Copy link
Copy Markdown
Member

Thanks for your contribution!

@valeriocomo
valeriocomo deleted the feat/ng-add-schematic branch July 21, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Add ng add support

2 participants