-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
ng-add
option to provide metadata modules (#978)
- Loading branch information
Showing
5 changed files
with
112 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
import { chain, noop, Rule } from '@angular-devkit/schematics' | ||
import { addRootProvider } from '@schematics/angular/utility' | ||
import { Schema } from './schema' | ||
import { MetadataModules, Schema } from './schema' | ||
import { classify } from '@angular-devkit/core/src/utils/strings' | ||
|
||
const ENTRYPOINTS = new Set<MetadataModules>([ | ||
'json-ld', | ||
'open-graph', | ||
'standard', | ||
'twitter-card', | ||
]) | ||
// noinspection JSUnusedGlobalSymbols (actually used in `collection.json`) | ||
export function ngAdd(options: Schema): Rule { | ||
const maybeAddNgxMetaRootProvider = (name?: string): Rule => { | ||
if (!name) { | ||
return noop() | ||
} | ||
const addNgxMetaRootProvider = (name: string): Rule => { | ||
const entrypoint = | ||
[...ENTRYPOINTS].find((entrypoint) => name.startsWith(entrypoint)) ?? name | ||
return addRootProvider( | ||
options.project, | ||
({ code, external }) => | ||
code`${external(`provideNgxMeta${classify(name)}`, `@davidlj95/ngx-meta/${name}`)}()`, | ||
code`${external(`provideNgxMeta${classify(name)}`, `@davidlj95/ngx-meta/${entrypoint}`)}()`, | ||
) | ||
} | ||
|
||
return chain([ | ||
maybeAddNgxMetaRootProvider('core'), | ||
maybeAddNgxMetaRootProvider(options.routing ? 'routing' : undefined), | ||
addNgxMetaRootProvider('core'), | ||
options.routing ? addNgxMetaRootProvider('routing') : noop(), | ||
...options.metadataModules.map((metadataModule) => | ||
addNgxMetaRootProvider(metadataModule), | ||
), | ||
]) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
export interface Schema { | ||
project: string | ||
routing: boolean | ||
metadataModules: ReadonlyArray<MetadataModules> | ||
} | ||
|
||
export type MetadataModules = | ||
| 'json-ld' | ||
| 'open-graph' | ||
| 'open-graph-profile' | ||
| 'standard' | ||
| 'twitter-card' |
This file contains 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