diff --git a/addon/ng2/blueprints/ng2/files/angular-cli.json b/addon/ng2/blueprints/ng2/files/angular-cli.json new file mode 100644 index 000000000000..9ade02d5d3f7 --- /dev/null +++ b/addon/ng2/blueprints/ng2/files/angular-cli.json @@ -0,0 +1,3 @@ +{ + "routes": [] +} diff --git a/addon/ng2/blueprints/ng2/files/src/app/__name__.ts b/addon/ng2/blueprints/ng2/files/src/app/__name__.ts index 4426740f4e4c..b4b9251540e6 100644 --- a/addon/ng2/blueprints/ng2/files/src/app/__name__.ts +++ b/addon/ng2/blueprints/ng2/files/src/app/__name__.ts @@ -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', @@ -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}`; } diff --git a/addon/ng2/blueprints/ng2/files/src/app/route-config.ts b/addon/ng2/blueprints/ng2/files/src/app/route-config.ts new file mode 100644 index 000000000000..6f412a983e34 --- /dev/null +++ b/addon/ng2/blueprints/ng2/files/src/app/route-config.ts @@ -0,0 +1,6 @@ +// DO NOT EDIT THIS FILE +// IT IS AUTO GENERATED BY ANGULAR-CLI + +export const CliRouteConfig = [ + +]; diff --git a/addon/ng2/blueprints/route-config/files/src/app/route-config.ts b/addon/ng2/blueprints/route-config/files/src/app/route-config.ts new file mode 100644 index 000000000000..3d3f1a4d2322 --- /dev/null +++ b/addon/ng2/blueprints/route-config/files/src/app/route-config.ts @@ -0,0 +1,7 @@ +// DO NOT EDIT THIS FILE +// IT IS AUTO GENERATED BY ANGULAR-CLI +<%= imports %> + +export const CliRouteConfig = [ +<%= routeDefinitions %> +]; diff --git a/addon/ng2/blueprints/route-config/index.js b/addon/ng2/blueprints/route-config/index.js new file mode 100644 index 000000000000..53ea78932462 --- /dev/null +++ b/addon/ng2/blueprints/route-config/index.js @@ -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 + } +} diff --git a/addon/ng2/blueprints/route/index.js b/addon/ng2/blueprints/route/index.js index 8540d5f2f309..80fe3f2e14d8 100644 --- a/addon/ng2/blueprints/route/index.js +++ b/addon/ng2/blueprints/route/index.js @@ -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)); +}