8000 feat: add utility functions for route generation · angular/angular-cli@2b05c22 · GitHub
[go: up one dir, main page]

Skip to content
< 10000 h1 class="Box-sc-g0xbh4-0 bmcJak prc-PageHeader-Title-LKOsd f2 prc-Heading-Heading-6CmGO" data-component="PH_Title" data-hidden="false">Commit 2b05c22
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 2b05c22

File tree

5 files changed

+1115
-1
lines changed

5 files changed

+1115
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [];

addon/ng2/blueprints/routes/index.js

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

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)
0