@@ -6,81 +6,74 @@ import {
66} from '@angular-devkit/schematics' ;
77import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
88import { applyToUpdateRecorder } from '@schematics/angular/utility/change' ;
9- import { addSymbolToNgModuleMetadata } from '../utility/ast-utils' ;
109import * as ts from 'typescript' ;
10+ import { addSymbolToNgModuleMetadata } from '../utility/ast-utils' ;
1111
1212export function ngAdd ( ) : Rule {
1313 return ( tree : Tree , context : SchematicContext ) => {
14- addModuleAndInjectionTokens ( tree , context ) ;
15- installDependencies ( context ) ;
14+ context . logger . info ( 'Installing dependencies...' ) ;
15+ context . addTask ( new NodePackageInstallTask ( ) ) ;
1616
17- return tree ;
18- } ;
19- }
17+ context . logger . info ( 'Adding ApiTokenInterceptorModule to the app...' ) ;
18+ const appModulePath = './src/app/app.module.ts' ;
2019
21- function addModuleAndInjectionTokens (
22- tree : Tree ,
23- context : SchematicContext
24- ) : void {
25- context . logger . info ( 'Adding ApiTokenInterceptorModule to the app...' ) ;
26- const appModulePath = '/src/app/app.module.ts' ;
20+ if ( ! tree . exists ( appModulePath ) ) {
21+ throw new SchematicsException (
22+ `The file ${ appModulePath } doesn't exist...`
23+ ) ;
24+ }
2725
28- if ( ! tree . exists ( appModulePath ) ) {
29- throw new SchematicsException ( `The file ${ appModulePath } doesn't exist...` ) ;
30- }
26+ const recorder = tree . beginUpdate ( appModulePath ) ;
27+ const appModuleFileContent = tree . read ( appModulePath ) ;
3128
32- const recorder = tree . beginUpdate ( appModulePath ) ;
33- const appModuleFileContent = tree . read ( appModulePath ) ;
29+ if ( appModuleFileContent === null ) {
30+ throw new SchematicsException (
31+ `The content of ${ appModulePath } couldn't be read...`
32+ ) ;
33+ }
3434
35- if ( appModuleFileContent === null ) {
36- throw new SchematicsException (
37- `The content of ${ appModulePath } couldn't be read...`
35+ const appModuleFileText = appModuleFileContent . toString ( 'utf-8' ) ;
36+ const sourceFile = ts . createSourceFile (
37+ appModulePath ,
38+ appModuleFileText ,
39+ ts . ScriptTarget . Latest ,
40+ true
3841 ) ;
39- }
42+ const importPath = '@ngx-toolset/api-token-interceptor' ;
4043
41- const sourceFile = ts . createSourceFile (
42- appModulePath ,
43- appModuleFileContent . toString ( ) ,
44- ts . ScriptTarget . ES2020
45- ) ;
46- const importPath = '@ngx-toolset/api-token-interceptor' ;
44+ for ( const symbol of [
45+ {
46+ metadataField : 'imports' ,
47+ symbolName : 'ApiTokenInterceptorModule' ,
48+ importText : 'ApiTokenInterceptorModule.forRoot()' ,
49+ } ,
50+ {
51+ metadataField : 'providers' ,
52+ symbolName : 'API_URL_REGEX' ,
53+ importText : '{ provide: API_URL_REGEX, useValue: /^/ },' ,
54+ } ,
55+ {
56+ metadataField : 'providers' ,
57+ symbolName : 'BEARER_TOKEN_CALLBACK_FN' ,
58+ importText :
59+ "{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', }," ,
60+ } ,
61+ ] ) {
62+ applyToUpdateRecorder (
63+ recorder ,
64+ addSymbolToNgModuleMetadata (
65+ sourceFile ,
66+ appModulePath ,
67+ symbol . metadataField ,
68+ symbol . symbolName ,
69+ importPath ,
70+ symbol . importText
71+ )
72+ ) ;
73+ }
4774
48- for ( const symbol of [
49- {
50- metadataField : 'imports' ,
51- symbolName : 'ApiTokenInterceptorModule' ,
52- importText : 'ApiTokenInterceptorModule.forRoot()' ,
53- } ,
54- {
55- metadataField : 'providers' ,
56- symbolName : 'API_URL_REGEX' ,
57- importText :
58- '{ provide: API_URL_REGEX, useValue: /^https://sample-regex.com/ }' ,
59- } ,
60- {
61- metadataField : 'providers' ,
62- symbolName : 'BEARER_TOKEN_CALLBACK_FN' ,
63- importText :
64- "{ provide: BEARER_TOKEN_CALLBACK_FN, useValue: (): string => 'sampleToken', }" ,
65- } ,
66- ] ) {
67- applyToUpdateRecorder (
68- recorder ,
69- addSymbolToNgModuleMetadata (
70- sourceFile ,
71- appModulePath ,
72- symbol . metadataField ,
73- symbol . symbolName ,
74- importPath ,
75- symbol . importText
76- )
77- ) ;
78- }
79-
80- tree . commitUpdate ( recorder ) ;
81- }
75+ tree . commitUpdate ( recorder ) ;
8276
83- function installDependencies ( context : SchematicContext ) : void {
84- context . logger . info ( 'Installing dependencies...' ) ;
85- context . addTask ( new NodePackageInstallTask ( ) ) ;
77+ return tree ;
78+ } ;
8679}
0 commit comments