1
1
import { dirname , extname , relative } from 'path' ;
2
2
3
3
import {
4
- ArrayLiteralExpression ,
5
- CallExpression ,
6
- Decorator ,
7
- Expression ,
8
- Identifier ,
9
- Node ,
10
- ObjectLiteralExpression ,
11
- PropertyAccessExpression ,
12
- PropertyAssignment ,
13
- SourceFile ,
14
- SyntaxKind
4
+ ArrayLiteralExpression ,
5
+ CallExpression ,
6
+ Decorator ,
7
+ Expression ,
8
+ Identifier ,
9
+ Node ,
10
+ ObjectLiteralExpression ,
11
+ PropertyAccessExpression ,
12
+ PropertyAssignment ,
13
+ SourceFile ,
14
+ SyntaxKind
15
15
} from 'typescript' ;
16
16
17
17
import { Logger } from '../logger/logger' ;
18
18
import * as Constants from '../util/constants' ;
19
19
import { FileCache } from '../util/file-cache' ;
20
20
import { changeExtension , getStringPropertyValue , replaceAll } from '../util/helpers' ;
21
21
import { BuildContext , ChangedFile , DeepLinkConfigEntry , DeepLinkDecoratorAndClass , DeepLinkPathInfo , File } from '../util/interfaces' ;
22
- import { appendAfter , getClassDeclarations , getTypescriptSourceFile , getNodeStringContent , replaceNode } from '../util/typescript-utils' ;
22
+ import { appendAfter , getNgModuleClassName , getNgModuleDecorator , getClassDeclarations , getTypescriptSourceFile , getNodeStringContent , replaceNode } from '../util/typescript-utils' ;
23
23
24
24
import { transpileTsString } from '../transpile' ;
25
25
@@ -77,33 +77,6 @@ export function getNgModuleDataFromPage(appNgModuleFilePath: string, filePath: s
77
77
} ;
78
78
}
79
79
80
- export function getNgModuleClassName ( filePath : string , fileContent : string ) {
81
- const ngModuleSourceFile = getTypescriptSourceFile ( filePath , fileContent ) ;
82
- const classDeclarations = getClassDeclarations ( ngModuleSourceFile ) ;
83
- // find the class with NgModule decorator;
84
- const classNameList : string [ ] = [ ] ;
85
- classDeclarations . forEach ( classDeclaration => {
86
- if ( classDeclaration && classDeclaration . decorators ) {
87
- classDeclaration . decorators . forEach ( decorator => {
88
- if ( decorator . expression && ( decorator . expression as CallExpression ) . expression && ( ( decorator . expression as CallExpression ) . expression as Identifier ) . text === NG_MODULE_DECORATOR_TEXT ) {
89
- const className = ( classDeclaration . name as Identifier ) . text ;
90
- classNameList . push ( className ) ;
91
- }
92
- } ) ;
93
- }
94
- } ) ;
95
-
96
- if ( classNameList . length === 0 ) {
97
- throw new Error ( `Could not find a class declaration in ${ filePath } ` ) ;
98
- }
99
-
100
- if ( classNameList . length > 1 ) {
101
- throw new Error ( `Multiple class declarations with NgModule in ${ filePath } . The correct class to use could not be determined.` ) ;
102
- }
103
-
104
- return classNameList [ 0 ] ;
105
- }
106
-
107
80
export function getDeepLinkDecoratorContentForSourceFile ( sourceFile : SourceFile ) : DeepLinkDecoratorAndClass {
108
81
const classDeclarations = getClassDeclarations ( sourceFile ) ;
109
82
@@ -204,7 +177,7 @@ function getArrayValueFromDeepLinkDecorator(sourceFile: SourceFile, propertyNode
204
177
205
178
export function hasExistingDeepLinkConfig ( appNgModuleFilePath : string , appNgModuleFileContent : string ) {
206
179
const sourceFile = getTypescriptSourceFile ( appNgModuleFilePath , appNgModuleFileContent ) ;
207
- const decorator = getAppNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
180
+ const decorator = getNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
208
181
const functionCall = getIonicModuleForRootCall ( decorator ) ;
209
182
210
183
if ( functionCall . arguments . length <= 2 ) {
@@ -215,30 +188,6 @@ export function hasExistingDeepLinkConfig(appNgModuleFilePath: string, appNgModu
215
188
return deepLinkConfigArg . kind === SyntaxKind . ObjectLiteralExpression ;
216
189
}
217
190
218
- function getAppNgModuleDecorator ( appNgModuleFilePath : string , sourceFile : SourceFile ) {
219
- const ngModuleDecorators : Decorator [ ] = [ ] ;
220
- const classDeclarations = getClassDeclarations ( sourceFile ) ;
221
- classDeclarations . forEach ( classDeclaration => {
222
- if ( classDeclaration && classDeclaration . decorators ) {
223
- classDeclaration . decorators . forEach ( decorator => {
224
- if ( decorator . expression && ( decorator . expression as CallExpression ) . expression && ( ( decorator . expression as CallExpression ) . expression as Identifier ) . text === NG_MODULE_DECORATOR_TEXT ) {
225
- ngModuleDecorators . push ( decorator ) ;
226
- }
227
- } ) ;
228
- }
229
- } ) ;
230
-
231
- if ( ngModuleDecorators . length === 0 ) {
232
- throw new Error ( `Could not find an "NgModule" decorator in ${ appNgModuleFilePath } ` ) ;
233
- }
234
-
235
- if ( ngModuleDecorators . length > 1 ) {
236
- throw new Error ( `Multiple "NgModule" decorators found in ${ appNgModuleFilePath } . The correct one to use could not be determined` ) ;
237
- }
238
-
239
- return ngModuleDecorators [ 0 ] ;
240
- }
241
-
242
191
function getNgModuleObjectLiteralArg ( decorator : Decorator ) {
243
192
const ngModuleArgs = ( decorator . expression as CallExpression ) . arguments ;
244
193
if ( ! ngModuleArgs || ngModuleArgs . length === 0 || ngModuleArgs . length > 1 ) {
@@ -358,13 +307,13 @@ export function updateAppNgModuleAndFactoryWithDeepLinkConfig(context: BuildCont
358
307
359
308
export function getUpdatedAppNgModuleContentWithDeepLinkConfig ( appNgModuleFilePath : string , appNgModuleFileContent : string , deepLinkStringContent : string ) {
360
309
let sourceFile = getTypescriptSourceFile ( appNgModuleFilePath , appNgModuleFileContent ) ;
361
- let decorator = getAppNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
310
+ let decorator = getNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
362
311
let functionCall = getIonicModuleForRootCall ( decorator ) ;
363
312
364
313
if ( functionCall . arguments . length === 1 ) {
365
314
appNgModuleFileContent = addDefaultSecondArgumentToAppNgModule ( appNgModuleFileContent , functionCall ) ;
366
315
sourceFile = getTypescriptSourceFile ( appNgModuleFilePath , appNgModuleFileContent ) ;
367
- decorator = getAppNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
316
+ decorator = getNgModuleDecorator ( appNgModuleFilePath , sourceFile ) ;
368
317
functionCall = getIonicModuleForRootCall ( decorator ) ;
369
318
}
370
319
@@ -405,7 +354,6 @@ export function addDeepLinkArgumentToAppNgModule(appNgModuleFileContent: string,
405
354
406
355
407
356
const DEEPLINK_DECORATOR_TEXT = 'DeepLink' ;
408
- const NG_MODULE_DECORATOR_TEXT = 'NgModule' ;
409
357
const DEEPLINK_DECORATOR_NAME_ATTRIBUTE = 'name' ;
410
358
const DEEPLINK_DECORATOR_SEGMENT_ATTRIBUTE = 'segment' ;
411
359
const DEEPLINK_DECORATOR_PRIORITY_ATTRIBUTE = 'priority' ;
0 commit comments