diff --git a/projects/ngx-meta/schematics/ng-add/index.spec.ts b/projects/ngx-meta/schematics/ng-add/index.spec.ts index 2e244336..33230f41 100644 --- a/projects/ngx-meta/schematics/ng-add/index.spec.ts +++ b/projects/ngx-meta/schematics/ng-add/index.spec.ts @@ -4,8 +4,9 @@ import { beforeEach, describe } from '@jest/globals' import { join } from 'path' import { Schema as NgAddSchema } from './schema' import { ProviderTestCase } from './testing/provider-test-case' -import { shouldAddRootProvider } from './testing/should-add-root-provider' import { createTestApp } from '../testing/create-test-app' +import { shouldAddRootProvider } from './testing/should-add-root-provider' +import { shouldNotAddRootProvider } from './testing/should-not-add-root-provider' // https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/ng-add/index.spec.ts // https://github.com/angular/components/blob/18.2.8/src/material/schematics/ng-add/index.spec.ts @@ -17,6 +18,7 @@ describe('ng-add schematic', () => { const defaultOptions: NgAddSchema = { project: 'test', + routing: false, } beforeEach(async () => { @@ -30,6 +32,10 @@ describe('ng-add schematic', () => { name: 'core', symbol: 'provideNgxMetaCore', }) + const ROUTING_PROVIDER = new ProviderTestCase({ + name: 'routing', + symbol: 'provideNgxMetaRouting', + }) ;([true, false] as const).forEach((standalone) => { const appKind = standalone ? 'standalone' : 'module-based' @@ -53,6 +59,22 @@ describe('ng-add schematic', () => { }) shouldAddRootProvider(CORE_PROVIDER, () => tree, standalone) + shouldNotAddRootProvider(ROUTING_PROVIDER, () => tree, standalone) + }) + + describe('when routing option is true', () => { + const routing = true + let tree: Tree + + beforeEach(async () => { + tree = await runner.runSchematic( + SCHEMATIC_NAME, + { ...defaultOptions, routing }, + appTree, + ) + }) + + shouldAddRootProvider(ROUTING_PROVIDER, () => tree, standalone) }) }) }) diff --git a/projects/ngx-meta/schematics/ng-add/index.ts b/projects/ngx-meta/schematics/ng-add/index.ts index c59304c6..b2255ea3 100644 --- a/projects/ngx-meta/schematics/ng-add/index.ts +++ b/projects/ngx-meta/schematics/ng-add/index.ts @@ -1,12 +1,23 @@ -import { Rule } from '@angular-devkit/schematics' +import { chain, noop, Rule } from '@angular-devkit/schematics' import { addRootProvider } from '@schematics/angular/utility' import { Schema } from './schema' +import { classify } from '@angular-devkit/core/src/utils/strings' // noinspection JSUnusedGlobalSymbols (actually used in `collection.json`) export function ngAdd(options: Schema): Rule { - return addRootProvider( - options.project, - ({ code, external }) => - code`${external('provideNgxMetaCore', '@davidlj95/ngx-meta/core')}()`, - ) + const maybeAddNgxMetaRootProvider = (name?: string): Rule => { + if (!name) { + return noop() + } + return addRootProvider( + options.project, + ({ code, external }) => + code`${external(`provideNgxMeta${classify(name)}`, `@davidlj95/ngx-meta/${name}`)}()`, + ) + } + + return chain([ + maybeAddNgxMetaRootProvider('core'), + maybeAddNgxMetaRootProvider(options.routing ? 'routing' : undefined), + ]) } diff --git a/projects/ngx-meta/schematics/ng-add/schema.json b/projects/ngx-meta/schematics/ng-add/schema.json index bf1cbcfa..7d3f4f76 100644 --- a/projects/ngx-meta/schematics/ng-add/schema.json +++ b/projects/ngx-meta/schematics/ng-add/schema.json @@ -10,6 +10,13 @@ "$default": { "$source": "projectName" } + }, + "routing": { + "type": "boolean", + "description": "Enables routing module to provide metadata in Angular routes' data", + "default": false, + "//todo": "Enable x-prompts for this and metadata modules to include when both are ready", + "//x-prompt": "Would you like to provide metadata in Angular routes' data?" } }, "required": [] diff --git a/projects/ngx-meta/schematics/ng-add/schema.ts b/projects/ngx-meta/schematics/ng-add/schema.ts index 748d4572..a3da68f4 100644 --- a/projects/ngx-meta/schematics/ng-add/schema.ts +++ b/projects/ngx-meta/schematics/ng-add/schema.ts @@ -1,3 +1,4 @@ export interface Schema { project: string + routing: boolean } diff --git a/projects/ngx-meta/schematics/ng-add/testing/should-not-add-root-provider.ts b/projects/ngx-meta/schematics/ng-add/testing/should-not-add-root-provider.ts new file mode 100644 index 00000000..f6f09eee --- /dev/null +++ b/projects/ngx-meta/schematics/ng-add/testing/should-not-add-root-provider.ts @@ -0,0 +1,18 @@ +import { ProviderTestCase } from './provider-test-case' +import { Tree } from '@angular-devkit/schematics' +import { expect, it } from '@jest/globals' +import { getAppConfigOrAppModuleContent } from './get-app-config-or-app-module-content' + +export const shouldNotAddRootProvider = ( + providerTestCase: ProviderTestCase, + treeFactory: () => Tree, + standalone: boolean, +) => { + it(`should not add ${providerTestCase.name} provider`, () => { + const appConfigOrAppModuleContents = getAppConfigOrAppModuleContent( + treeFactory(), + standalone, + ) + expect(appConfigOrAppModuleContents).not.toContain(providerTestCase.symbol) + }) +}