Skip to content

Commit

Permalink
feat: ask whether to add routing in ng-add schematic (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Oct 21, 2024
1 parent b028e11 commit d0355bc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
24 changes: 23 additions & 1 deletion projects/ngx-meta/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,7 @@ describe('ng-add schematic', () => {

const defaultOptions: NgAddSchema = {
project: 'test',
routing: false,
}

beforeEach(async () => {
Expand All @@ -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'
Expand All @@ -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<NgAddSchema>(
SCHEMATIC_NAME,
{ ...defaultOptions, routing },
appTree,
)
})

shouldAddRootProvider(ROUTING_PROVIDER, () => tree, standalone)
})
})
})
Expand Down
23 changes: 17 additions & 6 deletions projects/ngx-meta/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -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),
])
}
7 changes: 7 additions & 0 deletions projects/ngx-meta/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-meta/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Schema {
project: string
routing: boolean
}
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit d0355bc

Please sign in to comment.