Skip to content

Commit

Permalink
Fix module declaration import closes #38
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexivy committed Sep 8, 2018
1 parent c187b54 commit e35e2be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/angular-cli.ts
Expand Up @@ -61,18 +61,19 @@ export class AngularCli {


private parseNgModule(data: string) {
const startPattern = '@NgModule(';
const endPattern = ')';
const startPattern = '@NgModule({';
const endPattern = '})';
const startIndex = data.indexOf(startPattern) + startPattern.length;
const endIndex = data.indexOf(endPattern, startIndex);
const ngModuleStr = data.substring(startIndex, endIndex)
const ngModuleStr = data
.substring(startIndex, endIndex)
.replace('{', '')
.replace('}', '')
.split(' ')
.join('');

const before = data.substring(0, startIndex - startPattern.length);
const after = data.substring(endIndex + 1, data.length);
const after = data.substring(endIndex + endPattern.length, data.length);

const ngModuleTokens = ngModuleStr.split('],');

Expand Down
23 changes: 23 additions & 0 deletions test/extension.test.ts
Expand Up @@ -155,6 +155,29 @@ describe('Extension Tests:', () => {
const moduleFileContent = fs.readFileSync(path.join(moduleLocation.fullPath, moduleFilename), 'utf-8');
expect(moduleFileContent).to.contain('MyComponentComponent', 'No declaration has been added');
});

it('Should generate component with valid module declaration', async () => {
// tslint:disable-next-line:max-line-length
const beforeModuleContent = `import { BrowserModule } from \'@angular/platform-browser\';\nimport { NgModule } from \'@angular/core\';\nimport { RouterModule, Routes } from \'@angular/router\';\n\nimport { AppComponent } from \'./app.component\';\nimport { AdminSettingsComponent } from \'./admin-settings/admin-settings.component\';\nimport { AdminLandingComponent } from \'./admin-landing/admin-landing.component\';\nimport { AdminSettingsGradeMarksComponent } from \'./admin-settings-grade-marks/admin-settings-grade-marks.component\';\n\nconst appRoutes: Routes = [\n { path: \'settings/main\', component: AdminSettingsComponent },\n { path: \'settings/grade-marks\', component: AdminSettingsGradeMarksComponent },\n { path: \'\', component: AdminLandingComponent }\n];\n\n@NgModule({\n declarations: [\n AppComponent,\n AdminSettingsComponent,\n AdminLandingComponent,\n AdminSettingsGradeMarksComponent\n ],\n imports: [\n RouterModule.forRoot(appRoutes),\n BrowserModule\n ],\n providers: [\n RouterModule\n ],\n bootstrap: [\n AppComponent\n ]\n})\nexport class AppModule {}\n`;
// tslint:disable-next-line:max-line-length
const afterModuleContent = `import { BrowserModule } from \'@angular/platform-browser\';\nimport { NgModule } from \'@angular/core\';\nimport { RouterModule, Routes } from \'@angular/router\';\n\nimport { AppComponent } from \'./app.component\';\nimport { AdminSettingsComponent } from \'./admin-settings/admin-settings.component\';\nimport { AdminLandingComponent } from \'./admin-landing/admin-landing.component\';\nimport { AdminSettingsGradeMarksComponent } from \'./admin-settings-grade-marks/admin-settings-grade-marks.component\';\nimport { JeffTestComponent } from \'./JeffTest/JeffTest.component\';\n\nconst appRoutes: Routes = [\n { path: \'settings/main\', component: AdminSettingsComponent },\n { path: \'settings/grade-marks\', component: AdminSettingsGradeMarksComponent },\n { path: \'\', component: AdminLandingComponent }\n];\n\n@NgModule({\n declarations: [\n AppComponent,\n AdminSettingsComponent,\n AdminLandingComponent,\n AdminSettingsGradeMarksComponent,\n JeffTestComponent\n ],\n imports: [\n RouterModule.forRoot(appRoutes),\n BrowserModule\n ],\n providers: [\n RouterModule\n ],\n bootstrap: [\n AppComponent\n ]\n})\nexport class AppModule {}\n`;
const moduleLocation = Object.assign({}, { fullPath: path.join(testPath, 'my-module'), fileName: 'my-module', dirName: '', dirPath: testPath, rootPath: __dirname, params: [] });
config.defaults.component.module = 'my-module';
await angularCli.generateResources(ResourceType.Module, moduleLocation, config);
const moduleFilename = fs.readdirSync(moduleLocation.fullPath).find(f => f.endsWith('module.ts'));

expect(moduleFilename).not.to.be.empty;

// write test module content
fs.writeFileSync(path.join(moduleLocation.fullPath, moduleFilename), beforeModuleContent,'utf-8');

const componentLocation = Object.assign({}, moduleLocation, { fullPath: path.join(testPath, 'my-module', 'JeffTest'), fileName: 'JeffTest', dirPath: path.join(testPath, 'my-module') });
const result = await angularCli.generateResources(ResourceType.Component, componentLocation, config);

const realModuleFileContent = fs.readFileSync(path.join(moduleLocation.fullPath, moduleFilename), 'utf-8');

expect(realModuleFileContent).to.be.eql(afterModuleContent);
});
});

describe('Generate class tests', () => {
Expand Down

0 comments on commit e35e2be

Please sign in to comment.