Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 78ccdd3

Browse files
committed
chore(generators): move ngmodule utils to typescript utils
1 parent 3c26f29 commit 78ccdd3

File tree

5 files changed

+195
-190
lines changed

5 files changed

+195
-190
lines changed

src/deep-linking/util.spec.ts

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -549,127 +549,6 @@ export function removeDecorators(fileName: string, source: string): string {
549549
});
550550
});
551551

552-
describe('getNgModuleClassName', () => {
553-
it('should return the NgModule class name', () => {
554-
const knownContent = `
555-
import { NgModule } from '@angular/core';
556-
import { DeepLinkModule } from 'ionic-angular';
557-
558-
import { HomePage } from './home';
559-
560-
@NgModule({
561-
declarations: [
562-
HomePage,
563-
],
564-
imports: [
565-
DeepLinkModule.forChild(HomePage),
566-
]
567-
})
568-
export class HomePageModule {}
569-
`;
570-
571-
const knownPath = '/Users/noone/idk/some-path.module.ts';
572-
573-
const result = util.getNgModuleClassName(knownPath, knownContent);
574-
expect(result).toEqual('HomePageModule');
575-
});
576-
577-
it('should return the NgModule class name when there are multiple class declarations but only one is decorated', () => {
578-
const knownContent = `
579-
import { NgModule } from '@angular/core';
580-
import { DeepLinkModule } from 'ionic-angular';
581-
582-
import { HomePage } from './home';
583-
584-
@NgModule({
585-
declarations: [
586-
HomePage,
587-
],
588-
imports: [
589-
DeepLinkModule.forChild(HomePage),
590-
]
591-
})
592-
export class HomePageModule {}
593-
594-
export class TacoBell {
595-
constructor() {
596-
}
597-
598-
ionViewDidEnter() {
599-
console.log('tacos yo');
600-
}
601-
}
602-
`;
603-
604-
const knownPath = '/Users/noone/idk/some-path.module.ts';
605-
606-
const result = util.getNgModuleClassName(knownPath, knownContent);
607-
expect(result).toEqual('HomePageModule');
608-
});
609-
610-
it('should throw an error an NgModule isn\'t found', () => {
611-
const knownContent = `
612-
import { NgModule } from '@angular/core';
613-
import { DeepLinkModule } from 'ionic-angular';
614-
615-
import { HomePage } from './home';
616-
617-
export class HomePageModule {}
618-
619-
`;
620-
621-
const knownPath = '/Users/noone/idk/some-path.module.ts';
622-
623-
const knownError = 'Should never happen';
624-
try {
625-
util.getNgModuleClassName(knownPath, knownContent);
626-
throw new Error(knownError);
627-
} catch (ex) {
628-
expect(ex.message).not.toEqual(knownError);
629-
}
630-
});
631-
632-
it('should throw an error an multiple NgModules are found', () => {
633-
const knownContent = `
634-
import { NgModule } from '@angular/core';
635-
import { DeepLinkModule } from 'ionic-angular';
636-
637-
import { HomePage } from './home';
638-
639-
@NgModule({
640-
declarations: [
641-
HomePage,
642-
],
643-
imports: [
644-
DeepLinkModule.forChild(HomePage),
645-
]
646-
})
647-
export class HomePageModule {}
648-
649-
@NgModule({
650-
declarations: [
651-
HomePage,
652-
],
653-
imports: [
654-
DeepLinkModule.forChild(HomePage),
655-
]
656-
})
657-
export class TacoBellModule {}
658-
659-
`;
660-
661-
const knownPath = '/Users/noone/idk/some-path.module.ts';
662-
663-
const knownError = 'Should never happen';
664-
try {
665-
util.getNgModuleClassName(knownPath, knownContent);
666-
throw new Error(knownError);
667-
} catch (ex) {
668-
expect(ex.message).not.toEqual(knownError);
669-
}
670-
});
671-
});
672-
673552
describe('getRelativePathToPageNgModuleFromAppNgModule', () => {
674553
const prefix = join('Users', 'noone', 'myApp', 'src');
675554
const appNgModulePath = join(prefix, 'app', 'app.module.ts');

src/deep-linking/util.ts

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { dirname, extname, relative } from 'path';
22

33
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
1515
} from 'typescript';
1616

1717
import { Logger } from '../logger/logger';
1818
import * as Constants from '../util/constants';
1919
import { FileCache } from '../util/file-cache';
2020
import { changeExtension, getStringPropertyValue, replaceAll } from '../util/helpers';
2121
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';
2323

2424
import { transpileTsString } from '../transpile';
2525

@@ -77,33 +77,6 @@ export function getNgModuleDataFromPage(appNgModuleFilePath: string, filePath: s
7777
};
7878
}
7979

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-
10780
export function getDeepLinkDecoratorContentForSourceFile(sourceFile: SourceFile): DeepLinkDecoratorAndClass {
10881
const classDeclarations = getClassDeclarations(sourceFile);
10982

@@ -204,7 +177,7 @@ function getArrayValueFromDeepLinkDecorator(sourceFile: SourceFile, propertyNode
204177

205178
export function hasExistingDeepLinkConfig(appNgModuleFilePath: string, appNgModuleFileContent: string) {
206179
const sourceFile = getTypescriptSourceFile(appNgModuleFilePath, appNgModuleFileContent);
207-
const decorator = getAppNgModuleDecorator(appNgModuleFilePath, sourceFile);
180+
const decorator = getNgModuleDecorator(appNgModuleFilePath, sourceFile);
208181
const functionCall = getIonicModuleForRootCall(decorator);
209182

210183
if (functionCall.arguments.length <= 2) {
@@ -215,30 +188,6 @@ export function hasExistingDeepLinkConfig(appNgModuleFilePath: string, appNgModu
215188
return deepLinkConfigArg.kind === SyntaxKind.ObjectLiteralExpression;
216189
}
217190

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-
242191
function getNgModuleObjectLiteralArg(decorator: Decorator) {
243192
const ngModuleArgs = (decorator.expression as CallExpression).arguments;
244193
if (!ngModuleArgs || ngModuleArgs.length === 0 || ngModuleArgs.length > 1) {
@@ -358,13 +307,13 @@ export function updateAppNgModuleAndFactoryWithDeepLinkConfig(context: BuildCont
358307

359308
export function getUpdatedAppNgModuleContentWithDeepLinkConfig(appNgModuleFilePath: string, appNgModuleFileContent: string, deepLinkStringContent: string) {
360309
let sourceFile = getTypescriptSourceFile(appNgModuleFilePath, appNgModuleFileContent);
361-
let decorator = getAppNgModuleDecorator(appNgModuleFilePath, sourceFile);
310+
let decorator = getNgModuleDecorator(appNgModuleFilePath, sourceFile);
362311
let functionCall = getIonicModuleForRootCall(decorator);
363312

364313
if (functionCall.arguments.length === 1) {
365314
appNgModuleFileContent = addDefaultSecondArgumentToAppNgModule(appNgModuleFileContent, functionCall);
366315
sourceFile = getTypescriptSourceFile(appNgModuleFilePath, appNgModuleFileContent);
367-
decorator = getAppNgModuleDecorator(appNgModuleFilePath, sourceFile);
316+
decorator = getNgModuleDecorator(appNgModuleFilePath, sourceFile);
368317
functionCall = getIonicModuleForRootCall(decorator);
369318
}
370319

@@ -405,7 +354,6 @@ export function addDeepLinkArgumentToAppNgModule(appNgModuleFileContent: string,
405354

406355

407356
const DEEPLINK_DECORATOR_TEXT = 'DeepLink';
408-
const NG_MODULE_DECORATOR_TEXT = 'NgModule';
409357
const DEEPLINK_DECORATOR_NAME_ATTRIBUTE = 'name';
410358
const DEEPLINK_DECORATOR_SEGMENT_ATTRIBUTE = 'segment';
411359
const DEEPLINK_DECORATOR_PRIORITY_ATTRIBUTE = 'priority';

src/generators/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export function getNgModules(context: BuildContext, type: string): Promise<GlobR
9696
export function getDirToWriteToByType(context: BuildContext, type: string) {
9797
if (type === Constants.COMPONENT) {
9898
return context.componentsDir;
99-
} else if ( type === Constants.DIRECTIVE) {
99+
} else if (type === Constants.DIRECTIVE) {
100100
return context.directivesDir;
101101
} else if (type === Constants.PAGE) {
102102
return context.pagesDir;
103-
} else if ( type === Constants.PIPE) {
103+
} else if (type === Constants.PIPE) {
104104
return context.pipesDir;
105105
} else if (type === Constants.PROVIDER) {
106106
return context.providersDir;

0 commit comments

Comments
 (0)