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

Commit

Permalink
fix(generators): use correct path and handle providers correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jgw96 committed Mar 27, 2017
1 parent 44e8bd9 commit e82d5ff
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/generators/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
106 changes: 106 additions & 0 deletions src/util/typescript-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

22 changes: 18 additions & 4 deletions src/util/typescript-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -229,13 +233,23 @@ export function findObjectLiteralElementByName(properties: NodeArray<ObjectLiter
})[0];
}

export function appendNgModuleDeclaration(filePath: string, fileContent: string, declaration: string): string {
export function appendNgModuleDeclaration(filePath: string, fileContent: string, declaration: string, type?: string): string {
const sourceFile = getTypescriptSourceFile(filePath, fileContent, ScriptTarget.Latest, false);
const decorator = getNgModuleDecorator(path.basename(filePath), sourceFile);
const obj = getNgModuleObjectLiteralArg(decorator);
const properties = (findObjectLiteralElementByName(obj.properties, 'declarations') as PropertyAssignment);
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
return appendAfter(fileContent, declarations[declarations.length - 1], `,\n ${declaration}`);
if (type === 'provider') {
const properties = (findObjectLiteralElementByName(obj.properties, 'providers') as PropertyAssignment);
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
if (declarations.length === 0) {
return appendEmpty(fileContent, declarations['end'], declaration);
} else {
return appendAfter(fileContent, declarations[declarations.length - 1], `, ${declaration}`);
}
} else {
const properties = (findObjectLiteralElementByName(obj.properties, 'declarations') as PropertyAssignment);
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
return appendAfter(fileContent, declarations[declarations.length - 1], `,\n ${declaration}`);
}
}

const NG_MODULE_DECORATOR_TEXT = 'NgModule';

0 comments on commit e82d5ff

Please sign in to comment.