Skip to content

Commit

Permalink
fix(angular): use collections on generators (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Oct 23, 2020
1 parent d486408 commit 769985c
Show file tree
Hide file tree
Showing 29 changed files with 274 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { NgModule } from '@angular/core';
// xplat
import { UIModule } from '@<%= npmScope %>/<%= xplatFolderName %>';

const MODULES = [UIModule];

@NgModule({
imports: [UIModule],
exports: [UIModule]
imports: [...MODULES],
exports: [...MODULES]
})
export class SharedModule {}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { HomeComponent } from './home.component';

export const HOME_COMPONENTS = [HomeComponent];

export * from './home.component';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { SharedModule } from '../shared/shared.module';
import { HomeComponent } from './components';
import { HOME_COMPONENTS, HomeComponent } from './components';

export const routes: Routes = [
{
Expand All @@ -13,7 +13,7 @@ export const routes: Routes = [

@NgModule({
imports: [SharedModule, RouterModule.forChild(routes)],
declarations: [HomeComponent],
exports: [HomeComponent]
declarations: [...HOME_COMPONENTS],
exports: [...HOME_COMPONENTS]
})
export class HomeModule {}
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import { <%= utils.classify(name) %>Component } from './<%= name %>/<%= name %>.component';

export const <%= subFolder ? utils.sanitize(subFolder).toUpperCase() : utils.sanitize(name).toUpperCase() %>_COMPONENTS = [
<%= utils.classify(name) %>Component
];

export * from './<%= name %>/<%= name %>.component';
28 changes: 11 additions & 17 deletions packages/angular/src/schematics/component/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ describe('component schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(
barrelIndex.indexOf(`export * from './signup/signup.component';`)
).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`SignupComponent];`)).toBeGreaterThanOrEqual(0);
expect(
barrelIndex.indexOf(`./signup/signup.component`)
).toBeGreaterThanOrEqual(0);
Expand All @@ -87,9 +85,7 @@ describe('component schematic', () => {
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(
barrelIndex.indexOf(`export * from './signup/signup.component';`)
).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`SignupComponent];`)).toBeGreaterThanOrEqual(0);
});

it('should create component for specified platforms with framework name when no default is set', async () => {
Expand Down Expand Up @@ -199,9 +195,7 @@ describe('component schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(
barrelIndex.indexOf(`./signup/signup.component`)
).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`SignupComponent];`)).toBeGreaterThanOrEqual(0);

barrelPath = '/xplat/web/features/foo/components/registration/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
Expand All @@ -217,13 +211,17 @@ describe('component schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(barrelIndex.indexOf(`./registration`)).toBeGreaterThanOrEqual(0);
expect(
barrelIndex.indexOf(`REGISTRATION_COMPONENTS`)
).toBeGreaterThanOrEqual(0);

barrelPath = '/xplat/web/features/foo/components/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(barrelIndex.indexOf(`./registration`)).toBeGreaterThanOrEqual(0);
expect(
barrelIndex.indexOf(`REGISTRATION_COMPONENTS`)
).toBeGreaterThanOrEqual(0);
});

it('should create component for specified projects only', async () => {
Expand Down Expand Up @@ -300,9 +298,7 @@ describe('component schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(
barrelIndex.indexOf(`./signup/signup.component`)
).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`SignupComponent];`)).toBeGreaterThanOrEqual(0);

// let modulePath = '/apps/nativescript-viewer/src/features/foo/foo.module.ts';
// let content = getFileContent(tree, modulePath);
Expand All @@ -315,9 +311,7 @@ describe('component schematic', () => {
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(
barrelIndex.indexOf(`./signup/signup.component`)
).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`SignupComponent];`)).toBeGreaterThanOrEqual(0);
});

it('should THROW if feature module does not exist in projects', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
import { <%= utils.classify(name) %>Directive } from './<%= name %>.directive';

<% if (feature) { %>
export const <%= utils.sanitize(feature).toUpperCase() %>_DIRECTIVES = [
<% } else { %>
export const UI_DIRECTIVES = [
<% } %>
<%= utils.classify(name) %>Directive
];

export * from './<%= name %>.directive';
22 changes: 8 additions & 14 deletions packages/angular/src/schematics/directive/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ describe('directive schematic', () => {

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
0
);
expect(moduleContent.indexOf(`...UI_DIRECTIVES`)).toBeGreaterThanOrEqual(0);

// Directives added to the foo-feature
options = { ...defaultOptions, feature: 'foo' };
Expand Down Expand Up @@ -85,7 +83,7 @@ describe('directive schematic', () => {

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
expect(moduleContent.indexOf(`...FOO_DIRECTIVES`)).toBeGreaterThanOrEqual(
0
);
});
Expand Down Expand Up @@ -145,15 +143,11 @@ describe('directive schematic', () => {
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
0
);
expect(moduleContent.indexOf(`...UI_DIRECTIVES`)).toBeGreaterThanOrEqual(0);

modulePath = '/xplat/nativescript/features/ui/ui.module.ts';
moduleContent = getFileContent(tree, modulePath);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
0
);
expect(moduleContent.indexOf(`...UI_DIRECTIVES`)).toBeGreaterThanOrEqual(0);

// Directives added to the foo-feature
options = { ...defaultOptions, feature: 'foo' };
Expand Down Expand Up @@ -196,15 +190,15 @@ describe('directive schematic', () => {

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
expect(moduleContent.indexOf(`...FOO_DIRECTIVES`)).toBeGreaterThanOrEqual(
0
);
modulePath = '/xplat/nativescript/features/foo/foo.module.ts';
moduleContent = getFileContent(tree, modulePath);

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(
expect(moduleContent.indexOf(`...FOO_DIRECTIVES`)).toBeGreaterThanOrEqual(
0
);
});
Expand Down Expand Up @@ -264,12 +258,12 @@ describe('directive schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// symbol should be at end of collection
expect(index.indexOf(`./active-link.directive`)).toBeGreaterThanOrEqual(0);
expect(index.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(0);

indexPath = '/apps/web-viewer/src/app/features/foo/directives/index.ts';
index = getFileContent(tree, indexPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(index.indexOf(`./active-link.directive`)).toBeGreaterThanOrEqual(0);
expect(index.indexOf(`ActiveLinkDirective`)).toBeGreaterThanOrEqual(0);
});
});
4 changes: 2 additions & 2 deletions packages/angular/src/schematics/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ describe('feature schematic', () => {
expect(featureModule).toMatch(
`import { UIModule } from \'../ui/ui.module\'`
);
expect(featureModule.indexOf('FooComponent')).toBeGreaterThanOrEqual(0);
expect(featureModule.indexOf('FOO_COMPONENTS')).toBeGreaterThanOrEqual(0);
expect(featureModule.indexOf('declarations')).toBeGreaterThanOrEqual(0);

let compPath = '/xplat/web/features/foo/components/foo/foo.component.ts';
Expand Down Expand Up @@ -659,7 +659,7 @@ describe('feature schematic', () => {
featureModule = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(featureModule);
expect(featureModule).toMatch(`./foo-with-dash/foo-with-dash.component`);
expect(featureModule).toMatch(`export const FOOWITHDASH_COMPONENTS`);
});

it('should create feature module for specified project WITH Routing and adjustSandbox', async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/angular/src/schematics/pipe/_index_files/index.ts__tmpl__
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
import { <%= utils.classify(name) %>Pipe } from './<%= name %>.pipe';

<% if (feature) { %>
export const <%= utils.sanitize(feature).toUpperCase() %>_PIPES = [
<% } else { %>
export const UI_PIPES = [
<% } %>
<%= utils.classify(name) %>Pipe
];

export * from './<%= name %>.pipe';
12 changes: 6 additions & 6 deletions packages/angular/src/schematics/pipe/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('pipe schematic', () => {

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`...UI_PIPES`)).toBeGreaterThanOrEqual(0);

options = { ...defaultOptions, feature: 'foo' };
tree = await runSchematic('pipe', options, tree);
Expand All @@ -76,7 +76,7 @@ describe('pipe schematic', () => {

// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`...FOO_PIPES`)).toBeGreaterThanOrEqual(0);
});

it('should create pipe in libs and handle camel case properly', async () => {
Expand Down Expand Up @@ -170,25 +170,25 @@ describe('pipe schematic', () => {
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(pipeIndex.indexOf(`./truncate.pipe`)).toBeGreaterThanOrEqual(0);
expect(pipeIndex.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);

let modulePath = '/apps/nativescript-viewer/src/features/foo/foo.module.ts';
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`...FOO_PIPES`)).toBeGreaterThanOrEqual(0);

pipeIndexPath = '/apps/web-viewer/src/app/features/foo/pipes/index.ts';
pipeIndex = getFileContent(tree, pipeIndexPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(pipeIndex.indexOf(`./truncate.pipe`)).toBeGreaterThanOrEqual(0);
expect(pipeIndex.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);

modulePath = '/apps/web-viewer/src/app/features/foo/foo.module.ts';
moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`TruncatePipe`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`...FOO_PIPES`)).toBeGreaterThanOrEqual(0);
});

it('should THROW if feature module does not exist in shared code', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/angular/src/schematics/service/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('service schematic', () => {
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`AuthService`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`AuthService`)).toBe(-1);
});

it('should create service for specified projects only', async () => {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('service schematic', () => {
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`./services`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`./services`)).toBe(-1);

indexPath = '/apps/web-viewer/src/app/features/foo/services/index.ts';
index = getFileContent(tree, indexPath);
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('service schematic', () => {
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`AuthService`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`AuthService`)).toBe(-1);
});

it('should create service for specified platform only and by default add to core for that platform', async () => {
Expand Down Expand Up @@ -203,6 +203,6 @@ describe('service schematic', () => {
let moduleContent = getFileContent(tree, modulePath);
// console.log(modulePath + ':');
// console.log(moduleContent);
expect(moduleContent.indexOf(`AuthService`)).toBeGreaterThanOrEqual(0);
expect(moduleContent.indexOf(`AuthService`)).toBe(-1);
});
});
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { DateOrderPipe } from './date-order.pipe';

export const UI_PIPES = [DateOrderPipe];

export * from './date-order.pipe';
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { DateOrderPipe } from './pipes';
import { UI_PIPES } from './pipes';

const MODULES = [TranslateModule];

@NgModule({
imports: [TranslateModule],
declarations: [DateOrderPipe],
exports: [TranslateModule, DateOrderPipe]
imports: [...MODULES],
declarations: [...UI_PIPES],
exports: [...MODULES, ...UI_PIPES]
})
export class UISharedModule {}
Loading

0 comments on commit 769985c

Please sign in to comment.