-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (89 loc) · 2.85 KB
/
Copy pathindex.js
File metadata and controls
113 lines (89 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var ancestralBlueprint = require('../../lib/utilities/ancestral-blueprint');
var fs = require('fs-extra');
var path = require('path');
var EOL = require('os').EOL;
var chalk = require('chalk');
module.exports = {
description: 'Generates a route and registers it with the router.',
availableOptions: [
{
name: 'path',
type: String,
default: ''
},
{
name: 'skip-router',
type: Boolean,
default: false
}
],
fileMapTokens: function() {
var blueprint = ancestralBlueprint('route', this.project)
return blueprint.fileMapTokens.apply(blueprint, arguments);
},
locals: function() {
var blueprint = ancestralBlueprint('route', this.project);
return blueprint.locals.apply(blueprint, arguments);
},
shouldTouchRouter: function(name, options) {
return ancestralBlueprint('route', this.project).shouldTouchRouter(name, options);
},
afterInstall: function(options) {
updateRouter.call(this, 'add', options);
},
afterUninstall: function(options) {
updateRouter.call(this, 'remove', options);
}
};
function updateRouter(action, options) {
var entity = options.entity;
var actionColorMap = {
add: 'green',
remove: 'red'
};
var color = actionColorMap[action] || 'gray';
if (this.shouldTouchRouter(entity.name, options)) {
writeRoute(action, entity.name, options);
this.ui.writeLine('updating router');
this._writeStatusToUI(chalk[color], action + ' route', entity.name);
}
}
function findRouter(options) {
var routerPathParts = [options.project.root];
if (options.dummy && options.project.isEmberCLIAddon()) {
routerPathParts = routerPathParts.concat(['tests', 'dummy', 'app', 'router.coffee']);
} else {
routerPathParts = routerPathParts.concat(['app', 'router.coffee']);
}
return routerPathParts;
}
function writeRoute(action, name, options) {
var routerPath = path.join.apply(null, findRouter(options));
var source = fs.readFileSync(routerPath, 'utf-8');
var newRoutes;
if (action === 'add') {
newRoutes = addRouteToRouter(name, source);
} else {
newRoutes = removeRouteFromRouter(name, source);
}
fs.writeFileSync(routerPath, newRoutes);
}
function removeRouteFromRouter(name, oldContent) {
var existence = new RegExp("(?:route|resource)\\s?\\(?\\s?(['\"])" + name + "\\1");
if (!existence.test(oldContent)) {
return oldContent;
}
var re = new RegExp('^\\s*@route\\s*\\(?(["\'])\\s*'+ name +'\\s*\\1\\)?', 'm');
return oldContent.replace(re, '');
}
function addRouteToRouter(name, oldContent) {
var existence = new RegExp("(?:route|resource)\\s?\\(?\\s?(['\"])" + name + "\\1");
if (existence.test(oldContent)) {
return oldContent;
}
var funcRegex = /(map\s*->[\s\S]+)(\n^\S+)/m;
return oldContent.replace(
funcRegex,
"$1 @route '" + name + "'" + EOL + "$2"
);
}