Skip to content

Commit d0355bc

Browse files
authored
feat: ask whether to add routing in ng-add schematic (#975)
1 parent b028e11 commit d0355bc

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

projects/ngx-meta/schematics/ng-add/index.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { beforeEach, describe } from '@jest/globals'
44
import { join } from 'path'
55
import { Schema as NgAddSchema } from './schema'
66
import { ProviderTestCase } from './testing/provider-test-case'
7-
import { shouldAddRootProvider } from './testing/should-add-root-provider'
87
import { createTestApp } from '../testing/create-test-app'
8+
import { shouldAddRootProvider } from './testing/should-add-root-provider'
9+
import { shouldNotAddRootProvider } from './testing/should-not-add-root-provider'
910

1011
// https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/ng-add/index.spec.ts
1112
// 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', () => {
1718

1819
const defaultOptions: NgAddSchema = {
1920
project: 'test',
21+
routing: false,
2022
}
2123

2224
beforeEach(async () => {
@@ -30,6 +32,10 @@ describe('ng-add schematic', () => {
3032
name: 'core',
3133
symbol: 'provideNgxMetaCore',
3234
})
35+
const ROUTING_PROVIDER = new ProviderTestCase({
36+
name: 'routing',
37+
symbol: 'provideNgxMetaRouting',
38+
})
3339

3440
;([true, false] as const).forEach((standalone) => {
3541
const appKind = standalone ? 'standalone' : 'module-based'
@@ -53,6 +59,22 @@ describe('ng-add schematic', () => {
5359
})
5460

5561
shouldAddRootProvider(CORE_PROVIDER, () => tree, standalone)
62+
shouldNotAddRootProvider(ROUTING_PROVIDER, () => tree, standalone)
63+
})
64+
65+
describe('when routing option is true', () => {
66+
const routing = true
67+
let tree: Tree
68+
69+
beforeEach(async () => {
70+
tree = await runner.runSchematic<NgAddSchema>(
71+
SCHEMATIC_NAME,
72+
{ ...defaultOptions, routing },
73+
appTree,
74+
)
75+
})
76+
77+
shouldAddRootProvider(ROUTING_PROVIDER, () => tree, standalone)
5678
})
5779
})
5880
})
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
import { Rule } from '@angular-devkit/schematics'
1+
import { chain, noop, Rule } from '@angular-devkit/schematics'
22
import { addRootProvider } from '@schematics/angular/utility'
33
import { Schema } from './schema'
4+
import { classify } from '@angular-devkit/core/src/utils/strings'
45

56
// noinspection JSUnusedGlobalSymbols (actually used in `collection.json`)
67
export function ngAdd(options: Schema): Rule {
7-
return addRootProvider(
8-
options.project,
9-
({ code, external }) =>
10-
code`${external('provideNgxMetaCore', '@davidlj95/ngx-meta/core')}()`,
11-
)
8+
const maybeAddNgxMetaRootProvider = (name?: string): Rule => {
9+
if (!name) {
10+
return noop()
11+
}
12+
return addRootProvider(
13+
options.project,
14+
({ code, external }) =>
15+
code`${external(`provideNgxMeta${classify(name)}`, `@davidlj95/ngx-meta/${name}`)}()`,
16+
)
17+
}
18+
19+
return chain([
20+
maybeAddNgxMetaRootProvider('core'),
21+
maybeAddNgxMetaRootProvider(options.routing ? 'routing' : undefined),
22+
])
1223
}

projects/ngx-meta/schematics/ng-add/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
"$default": {
1111
"$source": "projectName"
1212
}
13+
},
14+
"routing": {
15+
"type": "boolean",
16+
"description": "Enables routing module to provide metadata in Angular routes' data",
17+
"default": false,
18+
"//todo": "Enable x-prompts for this and metadata modules to include when both are ready",
19+
"//x-prompt": "Would you like to provide metadata in Angular routes' data?"
1320
}
1421
},
1522
"required": []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export interface Schema {
22
project: string
3+
routing: boolean
34
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ProviderTestCase } from './provider-test-case'
2+
import { Tree } from '@angular-devkit/schematics'
3+
import { expect, it } from '@jest/globals'
4+
import { getAppConfigOrAppModuleContent } from './get-app-config-or-app-module-content'
5+
6+
export const shouldNotAddRootProvider = (
7+
providerTestCase: ProviderTestCase,
8+
treeFactory: () => Tree,
9+
standalone: boolean,
10+
) => {
11+
it(`should not add ${providerTestCase.name} provider`, () => {
12+
const appConfigOrAppModuleContents = getAppConfigOrAppModuleContent(
13+
treeFactory(),
14+
standalone,
15+
)
16+
expect(appConfigOrAppModuleContents).not.toContain(providerTestCase.symbol)
17+
})
18+
}

0 commit comments

Comments
 (0)