This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
control-app.js
142 lines (107 loc) · 3.73 KB
/
control-app.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
var path = require('path');
var fs = require('fs');
var deglob = require('globs-to-files');
var compression = require('compression');
var express = require('express');
var expstate = require('express-state');
var browserify = require('browserify');
var im = require('istanbul-middleware');
var watchify = require('watchify');
var assign = require('lodash').assign;
var humanizeDuration = require('humanize-duration');
var debug = require('debug')('zuul:control-app');
var defaultBuilder = '../lib/builder-browserify';
module.exports = function(config) {
var files = config.files;
var ui = config.ui;
var framework_dir = config.framework_dir;
var prj_dir = config.prj_dir;
var opt = {
debug: true
};
// watchify options
// https://github.com/substack/watchify#var-w--watchifyb-opts
opt = assign(opt, {
cache: {},
packageCache: {},
fullPaths: true
});
files = deglob.sync(files, {cwd: prj_dir});
var user_html = '';
if (config.html) {
user_html = fs.readFileSync(path.join(prj_dir, config.html), 'utf-8');
}
var build;
// default builder is browserify which we provide
config.builder = config.builder || defaultBuilder;
build = require(config.builder)(files, config);
var app = express();
app.use(compression());
expstate.extend(app);
app.set('state namespace', 'zuul');
app.expose(ui, 'ui');
app.expose(config.name, 'title');
app.set('views', __dirname + '/../frameworks');
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);
app.use(function(req, res, next) {
res.locals.title = config.name;
res.locals.user_scripts = config.scripts || [];
res.locals.user_html = user_html;
next();
});
app.use(app.router);
var bundle_router = new express.Router();
app.use(bundle_router.middleware);
// zuul files
app.use('/__zuul', express.static(__dirname + '/../frameworks'));
// framework files
app.use('/__zuul', express.static(framework_dir));
// any user's files
app.use(express.static(process.cwd()));
if (config.coverage && config.local) {
// coverage endpoint
app.use('/__zuul/coverage', im.createHandler());
}
app.get('/__zuul', function(req, res) {
res.locals.config = { port: config.support_port };
res.render('index');
});
var map = undefined;
var clientBundler = browserify(opt);
clientBundler.require(path.join(framework_dir, '/client.js'), { entry: true });
// we use watchify to speed up `.bundle()` calls
clientBundler = watchify(clientBundler);
bundle_router.get('/__zuul/client.js', function(req, res, next) {
res.contentType('application/javascript');
var start = Date.now();
clientBundler.bundle(function(err, buf) {
if (err) {
return next(err);
}
debug('zuul client took %s to bundle', humanizeDuration(Date.now() - start));
res.send(buf.toString());
});
});
bundle_router.get('/__zuul/test-bundle.map.json', function(req, res, next) {
if (!map) {
return res.status(404).send('');
}
res.json(map);
});
bundle_router.get('/__zuul/test-bundle.js', function(req, res, next) {
res.contentType('application/javascript');
build(function(err, src, srcmap) {
if (err) {
return next(err);
}
if (srcmap) {
map = srcmap;
map.file = '/__zuul/test-bundle.js';
src += '//# sourceMappingURL=' + '/__zuul/test-bundle.map.json';
}
res.send(src);
});
});
return app;
};