Skip to content

Commit d77f224

Browse files
committed
feat(route): add route-config proof of concept
1 parent 6607ebb commit d77f224

File tree

6 files changed

+126
-16
lines changed

6 files changed

+126
-16
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"routes": []
3+
}

addon/ng2/blueprints/ng2/files/src/app/__name__.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from 'angular2/core';
22
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
3-
3+
import {CliRouteConfig} from './route-config'
44

55
@Component({
66
selector: '<%= htmlComponentName %>-app',
@@ -11,10 +11,10 @@ import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
1111
})
1212
@RouteConfig([
1313

14-
])
14+
].concat(CliRouteConfig))
1515
export class <%= jsComponentName %>App {
1616
defaultMeaning: number = 42;
17-
17+
1818
meaningOfLife(meaning?: number) {
1919
return `The meaning of life is ${meaning || this.defaultMeaning}`;
2020
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// DO NOT EDIT THIS FILE
2+
// IT IS AUTO GENERATED BY ANGULAR-CLI
3+
4+
export const CliRouteConfig = [
5+
6+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// DO NOT EDIT THIS FILE
2+
// IT IS AUTO GENERATED BY ANGULAR-CLI
3+
<%= imports %>
4+
5+
export const CliRouteConfig = [
6+
<%= routeDefinitions %>
7+
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var fs = require('fs-extra');
2+
var path = require('path');
3+
var chalk = require('chalk');
4+
5+
var imports, routeDefinitions;
6+
7+
module.exports = {
8+
description: 'Registers the route with the router.',
9+
10+
locals: function(options) {
11+
return generateLocals.call(this, options);
12+
},
13+
14+
beforeInstall: function(options) {
15+
var routeConfigPath = path.join(options.project.root, 'src', 'app', 'route-config.ts');
16+
try {
17+
fs.unlinkSync(routeConfigPath);
18+
} catch (e) {}
19+
}
20+
};
21+
22+
function generateLocals(options) {
23+
var ngCliConfigPath = path.join(options.project.root, 'angular-cli.json');
24+
var ngCliConfig = JSON.parse(fs.readFileSync(ngCliConfigPath, 'utf-8'));
25+
26+
imports = ngCliConfig.routes.map(route =>
27+
`import {${route.component}} from '${route.componentPath}';`)
28+
.join('\n');
29+
30+
routeDefinitions = ngCliConfig.routes.map(route =>
31+
`{path:'${route.routePath}', name: '${route.component}', component: ${route.component}},`
32+
)
33+
.join('\n');
34+
35+
return {
36+
imports,
37+
routeDefinitions
38+
}
39+
}

addon/ng2/blueprints/route/index.js

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,71 @@
1-
var stringUtils = require('ember-cli/lib/utilities/string');
1+
var fs = require('fs-extra');
2+
var path = require('path');
3+
var chalk = require('chalk');
4+
5+
var imports, routeDefinitions;
26

37
module.exports = {
4-
description: ''
5-
6-
//locals: function(options) {
7-
// // Return custom template variables here.
8-
// return {
9-
//
10-
// };
11-
//}
12-
13-
// afterInstall: function(options) {
14-
// // Perform extra work here.
15-
// }
8+
description: 'Generates a route and a template.',
9+
10+
// TODO these options are not being used yet
11+
availableOptions: [{
12+
name: 'skip-router',
13+
type: Boolean,
14+
default: false
15+
}, {
16+
name: 'default',
17+
type: Boolean,
18+
default: false
19+
}],
20+
21+
beforeInstall: function(options, locals) {
22+
updateRouteConfig.call(this, 'add', options, locals);
23+
},
24+
25+
afterInstall: function(options, locals) {
26+
// TODO use skip-router
27+
this.lookupBlueprint('route-config')
28+
.install(options);
29+
},
30+
31+
beforeUninstall: function(options, locals) {
32+
updateRouteConfig.call(this, 'remove', options, locals);
33+
},
34+
35+
afterUninstall: function(options, locals) {
36+
// TODO use skip-router
37+
this.lookupBlueprint('route-config')
38+
.install(options);
39+
}
1640
};
41+
42+
function updateRouteConfig(action, options, locals) {
43+
var entity = options.entity;
44+
var actionColorMap = {
45+
add: 'green',
46+
remove: 'red'
47+
};
48+
var color = actionColorMap[action] || 'gray';
49+
50+
this._writeStatusToUI(chalk[color], action + ' route', entity.name);
51+
52+
var ngCliConfigPath = path.join(options.project.root, 'angular-cli.json');
53+
54+
// TODO use default option
55+
var route = {
56+
routePath: `/${locals.dasherizedModuleName}/...`,
57+
component: `${locals.classifiedModuleName}Root`,
58+
componentPath: `./${locals.dasherizedModuleName}/${locals.dasherizedModuleName}-root.component`
59+
}
60+
61+
var ngCliConfig = JSON.parse(fs.readFileSync(ngCliConfigPath, 'utf-8'));
62+
63+
if (action === 'add') {
64+
ngCliConfig.routes.push(route)
65+
} else if (action === 'remove') {
66+
var idx = ngCliConfig.routes.findIndex(el => el.routePath === route.routePath);
67+
if (idx) ngCliConfig.routes.splice(idx, 1);
68+
}
69+
70+
fs.writeFileSync(ngCliConfigPath, JSON.stringify(ngCliConfig, null, 2));
71+
}

0 commit comments

Comments
 (0)