Skip to content

Commit

Permalink
feat: add utility functions for route generation
Browse files Browse the repository at this point in the history
'route-utils.ts' provides utility functions to be used in generating routes
'blueprints/routes/*' creates a 'routes.ts' file when the newroutes command is
run and 'route.ts' doesn't exit
  • Loading branch information
EmmanuelAzuh committed Jul 28, 2016
1 parent 0cfc2bf commit 2b05c22
Show file tree
Hide file tree
Showing 5 changed files with 1,115 additions and 1 deletion.
1 change: 1 addition & 0 deletions addon/ng2/blueprints/routes/files/__path__/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [];
83 changes: 83 additions & 0 deletions addon/ng2/blueprints/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const Blueprint = require('ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;
const path = require('path');
const fs = require('fs');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const util = require('../../utilities/route-utils');
const SilentError = require('silent-error');

module.exports = {
description: 'Generates a route and template',

availableOptions: [
{ name: 'default', type: Boolean, default: false },
{ name: 'route', type: String },
{ name: 'parent', type: String, default: '' },
{ name: 'outlet', type: Boolean, default: false }
],

beforeInstall: function(options){
if (process.env.PWD.indexOf('src/app') === -1) {
throw new SilentError('New route must be within app');
}
this._locals(options)
.then(names => {
var route = options.route || this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '')
+ `/+${names.dasherizedModuleName}`;
// setup options needed for adding path to routes.ts
this.pathOptions = {
isDefault: options.default,
route: route,
parent: options.parent,
outlet: options.outlet,
component: `${names.classifiedModuleName}Component`,
dasherizedName: names.dasherizedModuleName,
mainFile: path.join(this.project.root, 'src/main.ts'),
routesFile: path.join(this.project.root, 'src/routes.ts')
};

var component = this.pathOptions.component;
if (!util.confirmComponentExport(file, component)) {
throw new SilentError(`Please add export for '${component}' to '${file}'`);
}
});
// confirm that there is an export of the component in componentFile
try {
var file = util.resolveComponentPath(this.project.root, process.env.PWD, this.newRoutePath);
} catch (e) {
throw new SilentError(e);
}
},

files: function() {
var fileList = getFiles.call(this);
if (this.project && fs.existsSync(path.join(this.project.root, 'src/routes.ts'))) {
return [];
}
return fileList;
},

fileMapTokens: function() {
return {
__path__: () => 'src'
};
},

normalizeEntityName: function(entityName) {
if (!entityName) {
throw new SilentError('Please provide new route\'s name');
}
this.dynamicPath = dynamicPathParser(this.project, entityName);
this.newRoutePath = entityName;
return entityName;
},

afterInstall: function() {
return util.applyChanges(util.configureMain(this.pathOptions.mainFile, 'routes', './routes'))
.then(() => {
return util.applyChanges(util.addPathToRoutes(this.pathOptions.routesFile, this.pathOptions));
}).catch(e => {
throw new SilentError(e.message);
})
}
}
1 change: 0 additions & 1 deletion addon/ng2/utilities/dynamic-path-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ module.exports = function dynamicPathParser(project, entityName) {

return parsedPath;
};

Loading

0 comments on commit 2b05c22

Please sign in to comment.