diff --git a/src/generators/util.ts b/src/generators/util.ts index ac8f0af4..9348b474 100644 --- a/src/generators/util.ts +++ b/src/generators/util.ts @@ -159,8 +159,12 @@ export function nonPageFileManipulation(context: BuildContext, name: string, ngM fileContent = content; return generateTemplates(context, hydratedRequest); }).then(() => { - fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, relative(dirname(ngModulePath), hydratedRequest.dirToWrite)); - fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className); + fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, `${relative(dirname(ngModulePath), hydratedRequest.dirToWrite)}/${hydratedRequest.fileName}`); + if (type === 'provider') { + fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className, type); + } else { + fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className); + } return writeFileAsync(ngModulePath, fileContent); }); } diff --git a/src/util/typescript-utils.spec.ts b/src/util/typescript-utils.spec.ts index 2d817c18..113cb8e4 100644 --- a/src/util/typescript-utils.spec.ts +++ b/src/util/typescript-utils.spec.ts @@ -262,5 +262,111 @@ export class AppModule {} const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolComponent'); expect(result).toEqual(expectedContent); }); + + it('should return a modified file content for providers', () => { + const knownContent = ` +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { IonicApp, IonicModule } from '../../../../..'; + +import { AppComponent } from './app.component'; +import { RootPageModule } from '../pages/root-page/root-page.module'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + IonicModule.forRoot(AppComponent), + RootPageModule + ], + bootstrap: [IonicApp], + providers: [] +}) +export class AppModule {} +`; + + const knownPath = '/some/fake/path'; + + const expectedContent = ` +import { NgModule } from \'@angular/core\'; +import { BrowserModule } from \'@angular/platform-browser\'; +import { IonicApp, IonicModule } from \'../../../../..\'; + +import { AppComponent } from \'./app.component\'; +import { RootPageModule } from \'../pages/root-page/root-page.module\'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + IonicModule.forRoot(AppComponent), + RootPageModule + ], + bootstrap: [IonicApp], + providers: [CoolProvider] +}) +export class AppModule {} +`; + + const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolProvider', 'provider'); + expect(result).toEqual(expectedContent); +}); + + it('should return a modified file content for providers that already has one provider', () => { + const knownContent = ` +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { IonicApp, IonicModule } from '../../../../..'; + +import { AppComponent } from './app.component'; +import { RootPageModule } from '../pages/root-page/root-page.module'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + IonicModule.forRoot(AppComponent), + RootPageModule + ], + bootstrap: [IonicApp], + providers: [AwesomeProvider] +}) +export class AppModule {} +`; + + const knownPath = '/some/fake/path'; + + const expectedContent = ` +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { IonicApp, IonicModule } from '../../../../..'; + +import { AppComponent } from './app.component'; +import { RootPageModule } from '../pages/root-page/root-page.module'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + IonicModule.forRoot(AppComponent), + RootPageModule + ], + bootstrap: [IonicApp], + providers: [AwesomeProvider, CoolProvider] +}) +export class AppModule {} +`; + + const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolProvider', 'provider'); + expect(result).toEqual(expectedContent); + }); }); diff --git a/src/util/typescript-utils.ts b/src/util/typescript-utils.ts index 89c0bbed..e626cf55 100644 --- a/src/util/typescript-utils.ts +++ b/src/util/typescript-utils.ts @@ -74,6 +74,10 @@ export function appendAfter(source: string, node: Node, toAppend: string): strin return stringSplice(source, node.getEnd(), 0, toAppend); } +export function appendEmpty(source: string, position: number, toAppend: string): string { + return stringSplice(source, position, 0, toAppend); +} + export function appendBefore(filePath: string, fileContent: string, node: Node, toAppend: string): string { const sourceFile = getTypescriptSourceFile(filePath, fileContent, ScriptTarget.Latest, false); return stringSplice(fileContent, node.getStart(sourceFile), 0, toAppend); @@ -229,13 +233,23 @@ export function findObjectLiteralElementByName(properties: NodeArray