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

Commit e82d5ff

Browse files
committed
fix(generators): use correct path and handle providers correctly
1 parent 44e8bd9 commit e82d5ff

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

src/generators/util.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ export function nonPageFileManipulation(context: BuildContext, name: string, ngM
159159
fileContent = content;
160160
return generateTemplates(context, hydratedRequest);
161161
}).then(() => {
162-
fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, relative(dirname(ngModulePath), hydratedRequest.dirToWrite));
163-
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className);
162+
fileContent = insertNamedImportIfNeeded(ngModulePath, fileContent, hydratedRequest.className, `${relative(dirname(ngModulePath), hydratedRequest.dirToWrite)}/${hydratedRequest.fileName}`);
163+
if (type === 'provider') {
164+
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className, type);
165+
} else {
166+
fileContent = appendNgModuleDeclaration(ngModulePath, fileContent, hydratedRequest.className);
167+
}
164168
return writeFileAsync(ngModulePath, fileContent);
165169
});
166170
}

src/util/typescript-utils.spec.ts

+106
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,111 @@ export class AppModule {}
262262
const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolComponent');
263263
expect(result).toEqual(expectedContent);
264264
});
265+
266+
it('should return a modified file content for providers', () => {
267+
const knownContent = `
268+
import { NgModule } from '@angular/core';
269+
import { BrowserModule } from '@angular/platform-browser';
270+
import { IonicApp, IonicModule } from '../../../../..';
271+
272+
import { AppComponent } from './app.component';
273+
import { RootPageModule } from '../pages/root-page/root-page.module';
274+
275+
@NgModule({
276+
declarations: [
277+
AppComponent
278+
],
279+
imports: [
280+
BrowserModule,
281+
IonicModule.forRoot(AppComponent),
282+
RootPageModule
283+
],
284+
bootstrap: [IonicApp],
285+
providers: []
286+
})
287+
export class AppModule {}
288+
`;
289+
290+
const knownPath = '/some/fake/path';
291+
292+
const expectedContent = `
293+
import { NgModule } from \'@angular/core\';
294+
import { BrowserModule } from \'@angular/platform-browser\';
295+
import { IonicApp, IonicModule } from \'../../../../..\';
296+
297+
import { AppComponent } from \'./app.component\';
298+
import { RootPageModule } from \'../pages/root-page/root-page.module\';
299+
300+
@NgModule({
301+
declarations: [
302+
AppComponent
303+
],
304+
imports: [
305+
BrowserModule,
306+
IonicModule.forRoot(AppComponent),
307+
RootPageModule
308+
],
309+
bootstrap: [IonicApp],
310+
providers: [CoolProvider]
311+
})
312+
export class AppModule {}
313+
`;
314+
315+
const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolProvider', 'provider');
316+
expect(result).toEqual(expectedContent);
317+
});
318+
319+
it('should return a modified file content for providers that already has one provider', () => {
320+
const knownContent = `
321+
import { NgModule } from '@angular/core';
322+
import { BrowserModule } from '@angular/platform-browser';
323+
import { IonicApp, IonicModule } from '../../../../..';
324+
325+
import { AppComponent } from './app.component';
326+
import { RootPageModule } from '../pages/root-page/root-page.module';
327+
328+
@NgModule({
329+
declarations: [
330+
AppComponent
331+
],
332+
imports: [
333+
BrowserModule,
334+
IonicModule.forRoot(AppComponent),
335+
RootPageModule
336+
],
337+
bootstrap: [IonicApp],
338+
providers: [AwesomeProvider]
339+
})
340+
export class AppModule {}
341+
`;
342+
343+
const knownPath = '/some/fake/path';
344+
345+
const expectedContent = `
346+
import { NgModule } from '@angular/core';
347+
import { BrowserModule } from '@angular/platform-browser';
348+
import { IonicApp, IonicModule } from '../../../../..';
349+
350+
import { AppComponent } from './app.component';
351+
import { RootPageModule } from '../pages/root-page/root-page.module';
352+
353+
@NgModule({
354+
declarations: [
355+
AppComponent
356+
],
357+
imports: [
358+
BrowserModule,
359+
IonicModule.forRoot(AppComponent),
360+
RootPageModule
361+
],
362+
bootstrap: [IonicApp],
363+
providers: [AwesomeProvider, CoolProvider]
364+
})
365+
export class AppModule {}
366+
`;
367+
368+
const result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolProvider', 'provider');
369+
expect(result).toEqual(expectedContent);
370+
});
265371
});
266372

src/util/typescript-utils.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export function appendAfter(source: string, node: Node, toAppend: string): strin
7474
return stringSplice(source, node.getEnd(), 0, toAppend);
7575
}
7676

77+
export function appendEmpty(source: string, position: number, toAppend: string): string {
78+
return stringSplice(source, position, 0, toAppend);
79+
}
80+
7781
export function appendBefore(filePath: string, fileContent: string, node: Node, toAppend: string): string {
7882
const sourceFile = getTypescriptSourceFile(filePath, fileContent, ScriptTarget.Latest, false);
7983
return stringSplice(fileContent, node.getStart(sourceFile), 0, toAppend);
@@ -229,13 +233,23 @@ export function findObjectLiteralElementByName(properties: NodeArray<ObjectLiter
229233
})[0];
230234
}
231235

232-
export function appendNgModuleDeclaration(filePath: string, fileContent: string, declaration: string): string {
236+
export function appendNgModuleDeclaration(filePath: string, fileContent: string, declaration: string, type?: string): string {
233237
const sourceFile = getTypescriptSourceFile(filePath, fileContent, ScriptTarget.Latest, false);
234238
const decorator = getNgModuleDecorator(path.basename(filePath), sourceFile);
235239
const obj = getNgModuleObjectLiteralArg(decorator);
236-
const properties = (findObjectLiteralElementByName(obj.properties, 'declarations') as PropertyAssignment);
237-
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
238-
return appendAfter(fileContent, declarations[declarations.length - 1], `,\n ${declaration}`);
240+
if (type === 'provider') {
241+
const properties = (findObjectLiteralElementByName(obj.properties, 'providers') as PropertyAssignment);
242+
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
243+
if (declarations.length === 0) {
244+
return appendEmpty(fileContent, declarations['end'], declaration);
245+
} else {
246+
return appendAfter(fileContent, declarations[declarations.length - 1], `, ${declaration}`);
247+
}
248+
} else {
249+
const properties = (findObjectLiteralElementByName(obj.properties, 'declarations') as PropertyAssignment);
250+
const declarations = (properties.initializer as ArrayLiteralExpression).elements;
251+
return appendAfter(fileContent, declarations[declarations.length - 1], `,\n ${declaration}`);
252+
}
239253
}
240254

241255
const NG_MODULE_DECORATOR_TEXT = 'NgModule';

0 commit comments

Comments
 (0)