Skip to content

feat(route): add route-config proof of concept #292

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addon/ng2/blueprints/ng2/files/angular-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"routes": []
}
6 changes: 3 additions & 3 deletions addon/ng2/blueprints/ng2/files/src/app/__name__.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';

import {CliRouteConfig} from './route-config'

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

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

meaningOfLife(meaning?: number) {
return `The meaning of life is ${meaning || this.defaultMeaning}`;
}
Expand Down
6 changes: 6 additions & 0 deletions addon/ng2/blueprints/ng2/files/src/app/route-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// DO NOT EDIT THIS FILE
// IT IS AUTO GENERATED BY ANGULAR-CLI

export const CliRouteConfig = [

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// DO NOT EDIT THIS FILE
// IT IS AUTO GENERATED BY ANGULAR-CLI
<%= imports %>

export const CliRouteConfig = [
<%= routeDefinitions %>
];
39 changes: 39 additions & 0 deletions addon/ng2/blueprints/route-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require('fs-extra');
var path = require('path');
var chalk = require('chalk');

var imports, routeDefinitions;

module.exports = {
description: 'Registers the route with the router.',

locals: function(options) {
return generateLocals.call(this, options);
},

beforeInstall: function(options) {
var routeConfigPath = path.join(options.project.root, 'src', 'app', 'route-config.ts');
try {
fs.unlinkSync(routeConfigPath);
} catch (e) {}
}
};

function generateLocals(options) {
var ngCliConfigPath = path.join(options.project.root, 'angular-cli.json');
var ngCliConfig = JSON.parse(fs.readFileSync(ngCliConfigPath, 'utf-8'));

imports = ngCliConfig.routes.map(route =>
`import {${route.component}} from '${route.componentPath}';`)
.join('\n');

routeDefinitions = ngCliConfig.routes.map(route =>
`{path:'${route.routePath}', name: '${route.component}', component: ${route.component}},`
)
.join('\n');

return {
imports,
routeDefinitions
}
}
81 changes: 68 additions & 13 deletions addon/ng2/blueprints/route/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,71 @@
var stringUtils = require('ember-cli/lib/utilities/string');
var fs = require('fs-extra');
var path = require('path');
var chalk = require('chalk');

var imports, routeDefinitions;

module.exports = {
description: ''

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

// afterInstall: function(options) {
// // Perform extra work here.
// }
description: 'Generates a route and a template.',

// TODO these options are not being used yet
availableOptions: [{
name: 'skip-router',
type: Boolean,
default: false
}, {
name: 'default',
type: Boolean,
default: false
}],

beforeInstall: function(options, locals) {
updateRouteConfig.call(this, 'add', options, locals);
},

afterInstall: function(options, locals) {
// TODO use skip-router
return this.lookupBlueprint('route-config')
.install(options);
},

beforeUninstall: function(options, locals) {
updateRouteConfig.call(this, 'remove', options, locals);
},

afterUninstall: function(options, locals) {
// TODO use skip-router
return this.lookupBlueprint('route-config')
.install(options);
}
};

function updateRouteConfig(action, options, locals) {
var entity = options.entity;
var actionColorMap = {
add: 'green',
remove: 'red'
};
var color = actionColorMap[action] || 'gray';

this._writeStatusToUI(chalk[color], action + ' route', entity.name);

var ngCliConfigPath = path.join(options.project.root, 'angular-cli.json');

// TODO use default option
var route = {
routePath: `/${locals.dasherizedModuleName}/...`,
component: `${locals.classifiedModuleName}Root`,
componentPath: `./${locals.dasherizedModuleName}/${locals.dasherizedModuleName}-root.component`
}

var ngCliConfig = JSON.parse(fs.readFileSync(ngCliConfigPath, 'utf-8'));

if (action === 'add') {
ngCliConfig.routes.push(route)
} else if (action === 'remove') {
var idx = ngCliConfig.routes.findIndex(el => el.routePath === route.routePath);
if (idx) ngCliConfig.routes.splice(idx, 1);
}

fs.writeFileSync(ngCliConfigPath, JSON.stringify(ngCliConfig, null, 2));
}