This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
/
stagecraft_stub_controller.js
153 lines (127 loc) · 3.97 KB
/
stagecraft_stub_controller.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var fs = require('graceful-fs'),
_ = require('lodash'),
glob = require('glob'),
async = require('async');
function parseParent(dashboardData, slug) {
var module = _.find(dashboardData.modules, function (module) {
return module.slug === slug;
});
if (!module) {
return false;
}
module = _.extend(module, {
'page-type': 'module',
'dashboard-title': dashboardData.title,
'dashboard-strapline': dashboardData.strapline,
'dashboard-slug': dashboardData.slug
});
if (dashboardData.department) {
module = _.extend(module, {
'department': dashboardData.department
});
}
if (dashboardData.agency) {
module = _.extend(module, {
'agency': dashboardData.agency
});
}
delete module.classes; // We don't care about the classes property on the page-per-thing pages
return module;
}
function send404(res, filePath) {
res.status(404);
res.send({error: "No such stub exists: " + filePath});
}
function loadRelated(basePath, callback) {
async.map([
basePath + '/departments.json',
basePath + '/business-models.json',
basePath + '/customer-types.json',
basePath + '/agencies.json'
], fs.readFile,
function(err, content) {
var json = content.map(JSON.parse.bind(JSON)),
related = { };
related.departments = json[0];
related.businessModels = json[1];
related.customerTypes = json[2];
related.agencies = json[3];
callback(null, related);
});
}
function enrichDashboard(related, dashboard) {
if (dashboard.department) {
dashboard.department =
related.departments[dashboard.department];
}
if (dashboard.agency) {
dashboard.agency = related.agencies[dashboard.agency];
}
if (dashboard['customer-type']) {
dashboard['customer-type'] =
related.customerTypes[dashboard['customer-type']];
}
if (dashboard['business-model']) {
dashboard['business-model'] =
related.businessModels[dashboard['business-model']];
}
return dashboard;
}
function removeDisabledModules(dashboard) {
if (dashboard.modules) {
dashboard.modules = dashboard.modules.filter(function(module) {
return !module.disabled;
});
}
return dashboard;
}
function loadDashboards(basePath, callback) {
loadRelated(basePath, function(err, related) {
if (err) callback(err);
else {
var dashboardGlob = require('path').join(basePath, 'dashboards/**/*.json');
glob(dashboardGlob, function(err, files) {
if (err) callback(err);
else {
async.map(files, fs.readFile, function(err, results) {
var dashboardMap = results.map(JSON.parse.bind(JSON))
.map(removeDisabledModules)
.map(enrichDashboard.bind(null, related))
.reduce(function(dashboards, dashboard) {
dashboards[dashboard.slug] = dashboard;
return dashboards;
}, { });
callback(null, dashboardMap);
});
}
});
}
});
}
module.exports = function(config, callback) {
var dashboardGlob = require('path').join(
config.stagecraftStubPath, '../');
loadDashboards(dashboardGlob, function (err, dashboards) {
callback(function(req, res) {
var slug = req.params[0],
dashboard = dashboards[slug],
slugParts, parentSlug;
console.log(slug, dashboard);
if (!dashboard) {
slugParts = slug.split('/');
slug = slugParts.pop();
parentSlug = slugParts.join('/');
dashboard = dashboards[parentSlug];
console.log(parentSlug, dashboard);
if (!dashboard) send404(res, parentSlug + '/' + slug);
else {
module = parseParent(dashboard, slug);
if (module) res.send(module);
else send404(res, parentSlug + '/' + slug);
}
} else {
res.send(dashboard);
}
});
});
};