Skip to content

Commit 0ea756f

Browse files
committed
feat: add utility functions for route generation
'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
1 parent 0cfc2bf commit 0ea756f

File tree

5 files changed

+1055
-1
lines changed

5 files changed

+1055
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [];
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const Blueprint = require('ember-cli/lib/models/blueprint');
2+
const getFiles = Blueprint.prototype.files;
3+
const path = require('path');
4+
const fs = require('fs');
5+
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
6+
const util = require('../../utilities/route-utilities');
7+
const SilentError = require('silent-error');
8+
9+
module.exports = {
10+
description: 'Generates a route and template',
11+
12+
availableOptions: [
13+
{ name: 'is-default', type: Boolean, default: false, aliase: ['d'] },
14+
{ name: 'route', type: String },
15+
{ name: 'parent', type: String, default: '' },
16+
{ name: 'outlet', type: Boolean, default: false }
17+
],
18+
19+
beforeInstall: function(options){
20+
21+
if (process.env.PWD.indexOf('src/app') === -1) {
22+
throw new SilentError('New route must be within app');
23+
}
24+
this._locals(options)
25+
.then(names => {
26+
var route = options.route || this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '')
27+
+ `/+${names.dasherizedModuleName}`;
28+
// setup options needed for adding path to routes.ts
29+
this.pathOptions = {
30+
isDefault: options.default,
31+
route: route,
32+
parent: options.parent,
33+
outlet: options.outlet,
34+
component: `${names.classifiedModuleName}Component`,
35+
dasherizedName: names.dasherizedModuleName,
36+
mainFile: path.join(this.project.root, 'src/main.ts'),
37+
routesFile: path.join(this.project.root, 'src/routes.ts')
38+
};
39+
});
40+
// confirm that there is an export of the component in componentFile
41+
var file = util.resolveComponentPath(this.project.root, process.env.PWD, this.newRoutePath);
42+
var component = this.pathOptions.component;
43+
if (!util.confirmComponentExport(file, component)) {
44+
throw new SilentError(`Please add export for '${component}' to '${file}'`);
45+
}
46+
},
47+
48+
files: function() {
49+
var fileList = getFiles.call(this);
50+
if (this.project && fs.existsSync(path.join(this.project.root, 'src/routes.ts'))) {
51+
return [];
52+
}
53+
return fileList;
54+
},
55+
56+
fileMapTokens: function() {
57+
return {
58+
__path__: () => 'src'
59+
};
60+
},
61+
62+
normalizeEntityName: function(entityName) {
63+
if (!entityName) {
64+
throw new SilentError('Please provide new route\'s name');
65+
}
66+
this.dynamicPath = dynamicPathParser(this.project, entityName);
67+
this.newRoutePath = entityName;
68+
return entityName;
69+
},
70+
71+
afterInstall: function() {
72+
return util.configureMain(this.pathOptions.mainFile, 'routes', './routes').then(() => {
73+
this.pathOptions.component = util.resolveImportName(this.pathOptions.component, this.pathOptions.routesFile);
74+
return util.addPathToRoutes(this.pathOptions.routesFile, this.pathOptions);
75+
}).catch(e => {
76+
throw new SilentError(e.message);
77+
})
78+
}
79+
}

addon/ng2/utilities/dynamic-path-parser.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@ module.exports = function dynamicPathParser(project, entityName) {
5656

5757
return parsedPath;
5858
};
59-

0 commit comments

Comments
 (0)