Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add route generation #139

Merged
merged 1 commit into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Dependency Status][david-badge]][david-badge-url]
[![devDependency Status][david-dev-badge]][david-dev-badge-url]
[![npm][npm-badge]][npm-badge-url]

Prototype of a CLI for Angular 2 applications based on the [ember-cli](http://www.ember-cli.com/) project.

## Note
Expand Down Expand Up @@ -59,6 +59,50 @@ Directive | `ng g directive my-new-directive`
Pipe | `ng g pipe my-new-pipe`
Service | `ng g service my-new-service`

### Generating a route

You can generate a new route by with the following command (note the singular
used in `hero`):

```bash
ng generate route hero
```

This will create a folder with a routable component (`hero-root.component.ts`)
with two sub-routes. The file structure will be as follows:

```
...
|-- app
| |-- hero
| | |-- hero-detail.component.html
| | |-- hero-detail.component.css
| | |-- hero-detail.component.spec.ts
| | |-- hero-detail.component.ts
| | |-- hero-list.component.html
| | |-- hero-list.component.css
| | |-- hero-list.component.spec.ts
| | |-- hero-list.component.ts
| | |-- hero-root.component.spec.ts
| | |-- hero-root.component.ts
| | |-- hero.service.spec.ts
| | |-- hero.service.ts
| |-- ...
|-- app.ts
...
```

Afterwards to use the new route open your main app component, import
`hero-root.component.ts` and add it in the route config:

```
@RouteConfig([
{path:'/hero/...', name: 'HeroRoot', component: HeroRoot}
])
```

Visiting `http://localhost:4200/hero` will show the hero list.


### Creating a build

Expand Down
2 changes: 1 addition & 1 deletion addon/ng2/blueprints/ng2/files/karma-test-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ System.import('angular2/platform/browser').then(function(browser_adapter) {
});

function onlyAppFiles(filePath) {
return /^\/base\/dist\/app\/(?!spec)([a-z0-9-_\/]+)\.js$/.test(filePath);
return /^\/base\/dist\/app\/(?!.*\.spec\.js$)([a-z0-9-_\.\/]+)\.js$/.test(filePath);
}

function onlySpecFiles(path) {
Expand Down
2 changes: 2 additions & 0 deletions addon/ng2/blueprints/ng2/files/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = function(config) {
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
{pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/router.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},

{pattern: 'karma-test-shim.js', included: true, watched: true},
Expand Down
6 changes: 4 additions & 2 deletions addon/ng2/blueprints/ng2/files/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {bootstrap} from 'angular2/platform/browser';
import {<%= jsComponentName %>App} from './app/<%= htmlComponentName %>';
import {ROUTER_PROVIDERS} from 'angular2/router';


bootstrap(<%= jsComponentName %>App);
bootstrap(<%= jsComponentName %>App, [
ROUTER_PROVIDERS
]);
4 changes: 3 additions & 1 deletion addon/ng2/blueprints/ng2/files/src/app/__name__.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<p>
<%= htmlComponentName %> Works!
</p>
</p>

<router-outlet></router-outlet>
8 changes: 6 additions & 2 deletions addon/ng2/blueprints/ng2/files/src/app/__name__.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';


@Component({
selector: '<%= htmlComponentName %>-app',
providers: [],
templateUrl: 'app/<%= htmlComponentName %>.html',
directives: [],
directives: [ROUTER_DIRECTIVES],
pipes: []
})
@RouteConfig([

])
export class <%= jsComponentName %>App {
defaultMeaning: number = 42;

meaningOfLife(meaning?: number) {
return `The meaning of life is ${meaning || this.defaultMeaning}`;
}
Expand Down
2 changes: 1 addition & 1 deletion addon/ng2/blueprints/ng2/files/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title><%= jsComponentName %></title>
<base href=".">
<base href="/">
{{content-for 'head'}}
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div *ngIf="<%= camelizedModuleName %>">
<h3>"{{editName}}"</h3>
<div>
<label>Id:</label>
{{<%= camelizedModuleName %>.id}}
</div>
<div>
<label>Name:</label>
<input [(ngModel)]="editName" placeholder="name"/>
</div>
<button (click)="save()">Save</button>
<button (click)="cancel()">Cancel</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
it,
iit,
describe,
ddescribe,
expect,
inject,
injectAsync,
TestComponentBuilder,
beforeEachProviders
} from 'angular2/testing';
import {provide} from 'angular2/core';
import {<%= classifiedModuleName %>DetailComponent} from './<%= dasherizedModuleName %>-detail.component';

describe('<%= classifiedModuleName %>DetailComponent', () => {

beforeEachProviders(() => []);

it('should ...', injectAsync([TestComponentBuilder], (tcb:TestComponentBuilder) => {
return tcb.createAsync(<%= classifiedModuleName %>DetailComponent).then((fixture) => {
fixture.detectChanges();
});
}));

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {Component, OnInit} from 'angular2/core';
import {<%= classifiedModuleName %>, <%= classifiedModuleName %>Service} from './<%= dasherizedModuleName %>.service';
import {RouteParams, Router} from 'angular2/router';
import {CanDeactivate, ComponentInstruction} from 'angular2/router';

@Component({
templateUrl: 'app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-detail.component.html',
styleUrls: ['app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-detail.component.css']
})
export class <%= classifiedModuleName %>DetailComponent implements OnInit, CanDeactivate {

<%= camelizedModuleName %>: <%= classifiedModuleName %>;
editName: string;

constructor(
private _service: <%= classifiedModuleName %>Service,
private _router: Router,
private _routeParams: RouteParams
) { }

ngOnInit() {
let id = +this._routeParams.get('id');
this._service.get(id).then(<%= camelizedModuleName %> => {
if (<%= camelizedModuleName %>) {
this.editName = <%= camelizedModuleName %>.name;
this.<%= camelizedModuleName %> = <%= camelizedModuleName %>;
} else {
this.gotoList();
}
});
}

routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction): any {
if (!this.<%= camelizedModuleName %> || this.<%= camelizedModuleName %>.name === this.editName) {
return true;
}

return new Promise<boolean>((resolve, reject) => resolve(window.confirm('Discard changes?')));
}

cancel() {
this.editName = this.<%= camelizedModuleName %>.name;
this.gotoList();
}

save() {
this.<%= camelizedModuleName %>.name = this.editName;
this.gotoList();
}

gotoList() {
this._router.navigate(['<%= classifiedModuleName %>List']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2><%= classifiedModuleName %> List</h2>

<ul>
<li *ngFor="#<%= camelizedModuleName %> of <%= camelizedModuleName %>s">
<a
[routerLink]="['<%= classifiedModuleName %>Detail', { id: <%= camelizedModuleName %>.id }]">
<strong>{{<%= camelizedModuleName %>.id}}</strong>
-
{{<%= camelizedModuleName %>.name}}
</a>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
it,
iit,
describe,
ddescribe,
expect,
inject,
injectAsync,
TestComponentBuilder,
beforeEachProviders
} from 'angular2/testing';
import {provide} from 'angular2/core';
import {<%= classifiedModuleName %>ListComponent} from './<%= dasherizedModuleName %>-list.component';

describe('<%= classifiedModuleName %>ListComponent', () => {

beforeEachProviders(() => []);

it('should ...', injectAsync([TestComponentBuilder], (tcb:TestComponentBuilder) => {
return tcb.createAsync(<%= classifiedModuleName %>ListComponent).then((fixture) => {
fixture.detectChanges();
});
}));

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Component, OnInit} from 'angular2/core';
import {<%= classifiedModuleName %>, <%= classifiedModuleName %>Service} from './<%= dasherizedModuleName %>.service';
import {ROUTER_DIRECTIVES} from 'angular2/router';

@Component({
templateUrl: 'app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-list.component.html',
styleUrls: ['app/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>-list.component.css'],
directives: [ROUTER_DIRECTIVES]
})
export class <%= classifiedModuleName %>ListComponent implements OnInit {
<%= camelizedModuleName %>s: <%= classifiedModuleName %>[];
constructor(
private _service: <%= classifiedModuleName %>Service) {}
ngOnInit() {
this._service.getAll().then(<%= camelizedModuleName %>s => this.<%= camelizedModuleName %>s = <%= camelizedModuleName %>s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Component} from 'angular2/core';
import {RouteConfig, RouterOutlet} from 'angular2/router';

import {<%= classifiedModuleName %>ListComponent} from './<%= dasherizedModuleName %>-list.component';
import {<%= classifiedModuleName %>DetailComponent} from './<%= dasherizedModuleName %>-detail.component';
import {<%= classifiedModuleName %>Service} from './<%= dasherizedModuleName %>.service';

@Component({
template: '<router-outlet></router-outlet>',
providers: [<%= classifiedModuleName %>Service],
directives: [RouterOutlet]
})
@RouteConfig([
{path:'/', name: '<%= classifiedModuleName %>List', component: <%= classifiedModuleName %>ListComponent, useAsDefault: true},
{path:'/:id', name: '<%= classifiedModuleName %>Detail', component: <%= classifiedModuleName %>DetailComponent}
])
export class <%= classifiedModuleName %>Root {
constructor() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {provide} from 'angular2/core';
import {<%= classifiedModuleName %>Service} from './<%= dasherizedModuleName %>.service';

describe('<%= classifiedModuleName %>Service', () => {

beforeEachProviders(() => [<%= classifiedModuleName %>Service]);

it('should get all <%= camelizedModuleName %>s', inject([<%= classifiedModuleName %>Service], (<%= camelizedModuleName %>Service:<%= classifiedModuleName %>Service) => {
<%= camelizedModuleName %>Service.getAll().then(<%= camelizedModuleName %>s => expect(<%= camelizedModuleName %>s.length).toBe(3));
}));

it('should get one <%= camelizedModuleName %>', inject([<%= classifiedModuleName %>Service], (<%= camelizedModuleName %>Service:<%= classifiedModuleName %>Service) => {
<%= camelizedModuleName %>Service.get(1).then(<%= camelizedModuleName %> => expect(<%= camelizedModuleName %>.id).toBe(1));
}));

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Injectable} from 'angular2/core';

export class <%= classifiedModuleName %> {
constructor(public id: number, public name: string) { }
}

@Injectable()
export class <%= classifiedModuleName %>Service {
getAll() { return promise; }
get(id: number) {
return promise.then(all => all.find(e => e.id === id));
}
}

let mock = [
new <%= classifiedModuleName %>(1, 'one'),
new <%= classifiedModuleName %>(2, 'two'),
new <%= classifiedModuleName %>(3, 'three')
];

let promise = Promise.resolve(mock);
16 changes: 16 additions & 0 deletions addon/ng2/blueprints/route/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var stringUtils = require('ember-cli/lib/utilities/string');

module.exports = {
description: ''

//locals: function(options) {
// // Return custom template variables here.
// return {
//
// };
//}

// afterInstall: function(options) {
// // Perform extra work here.
// }
};