Skip to content

Commit 3827130

Browse files
committed
Support AMD loaders that provide a Node.js based server-side component
1 parent e516c5e commit 3827130

File tree

2 files changed

+159
-122
lines changed

2 files changed

+159
-122
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"https://github.com/amdjs/amdjs-api/wiki/AMD"
99
],
1010
"devDependencies": {
11-
"express": "3.0.0"
11+
"express": "3.0.0",
12+
"npm": ">=1.2.16"
1213
},
1314
"dependencies": {},
1415
"engine": "node >= 0.6.0"

server/server.js

Lines changed: 157 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -11,125 +11,161 @@ function fourOhFour(res, msg) {
1111
res.end('', 404);
1212
}
1313

14-
app.get('/', function(req, res) {
15-
// run all tests
16-
if (req.query)
17-
fs.readFile(path.normalize(path.join(__dirname, './resources/all.html')), function(err, data) {
18-
if (err) return fourOhFour(res, err);
19-
20-
var frameworkConfigScriptTag = (req.query.framework) ? '<script src="/config/'+req.query.framework+'.js"></script>' : '';
21-
var frameworkLibScriptTag = (req.query.framework) ? '<script src="/framework/'+req.query.framework+'.js"></script>' : '';
22-
23-
var frameworkOptions = [];
24-
for (var id in manifest) {
25-
frameworkOptions.push('<option value="'+id+'">'+manifest[id].name+'</option>');
26-
}
27-
28-
var output = data.toString()
29-
.replace(/\{\{HASTESTS\}\}/g, (req.query.framework) ? 'has-tests' : '')
30-
.replace(/\{\{FRAMEWORK_OPTIONS\}\}/g, frameworkOptions.join('\n'))
31-
.replace(/\{\{FRAMEWORK\}\}/g, req.query.framework)
32-
.replace(/\{\{FRAMEWORK_CONFIG\}\}/, frameworkConfigScriptTag)
33-
.replace(/\{\{FRAMEWORK_LIB\}\}/, frameworkLibScriptTag);
34-
res.setHeader('Content-Type', 'text/html');
35-
res.end(output);
36-
});
37-
});
38-
39-
app.get('/util/reporter.js', function(req, res) {
40-
// load the reporters
41-
fs.readFile(path.normalize(path.join(__dirname, './resources/reporter.js')), function(err, data) {
42-
if (err) return fourOhFour(res, err);
43-
res.setHeader('Content-Type', 'text/javascript');
44-
res.end(data.toString());
45-
});
46-
});
47-
48-
app.get('/util/all.js', function(req, res) {
49-
// load the reporters
50-
fs.readFile(path.normalize(path.join(__dirname, './resources/all.js')), function(err, data) {
51-
if (err) return fourOhFour(res, err);
52-
res.setHeader('Content-Type', 'text/javascript');
53-
res.end(data.toString());
54-
});
55-
});
56-
57-
app.get('/util/all.css', function(req, res) {
58-
// load the reporters
59-
fs.readFile(path.normalize(path.join(__dirname, './resources/all.css')), function(err, data) {
60-
if (err) return fourOhFour(res, err);
61-
res.setHeader('Content-Type', 'text/css');
62-
res.end(data.toString());
63-
});
64-
});
65-
66-
app.get('/framework/:framework', function(req, res) {
67-
// load a framework file
68-
var framework = req.params.framework.replace(/\.js$/, '');
69-
fs.readFile(path.normalize(path.join(__dirname, '../impl/'+manifest[framework].impl)), function(err, data) {
70-
if (err) return fourOhFour(res, err);
71-
res.setHeader('Content-Type', 'text/javascript');
72-
res.end(data.toString());
73-
});
74-
});
75-
76-
app.get('/config/:framework', function(req, res) {
77-
// load a config file
78-
var framework = req.params.framework.replace(/\.js$/, '');
79-
fs.readFile(path.normalize(path.join(__dirname, '../impl/'+manifest[framework].config)), function(err, data) {
80-
if (err) return fourOhFour(res, err);
81-
res.setHeader('Content-Type', 'text/javascript');
82-
res.end(data.toString());
83-
});
84-
});
85-
86-
app.get('/:framework/:test/system.js', function(req, res) {
87-
// get a file for the specified test
88-
res.setHeader('Content-Type', 'text/javascript');
89-
if (!systemNotify) {
90-
systemNotify = true;
91-
console.log('"system" module requested by browser. Usually a side-effect of doing static analysis');
14+
function start() {
15+
app.get('/', function(req, res) {
16+
// run all tests
17+
if (req.query)
18+
fs.readFile(path.normalize(path.join(__dirname, './resources/all.html')), function(err, data) {
19+
if (err) return fourOhFour(res, err);
20+
21+
var frameworkConfigScriptTag = (req.query.framework) ? '<script src="/config/'+req.query.framework+'.js"></script>' : '';
22+
var frameworkLibScriptTag = (req.query.framework) ? '<script src="/framework/'+req.query.framework+'.js"></script>' : '';
23+
24+
var frameworkOptions = [];
25+
for (var id in manifest) {
26+
frameworkOptions.push('<option value="'+id+'">'+manifest[id].name+'</option>');
27+
}
28+
29+
var output = data.toString()
30+
.replace(/\{\{HASTESTS\}\}/g, (req.query.framework) ? 'has-tests' : '')
31+
.replace(/\{\{FRAMEWORK_OPTIONS\}\}/g, frameworkOptions.join('\n'))
32+
.replace(/\{\{FRAMEWORK\}\}/g, req.query.framework)
33+
.replace(/\{\{FRAMEWORK_CONFIG\}\}/, frameworkConfigScriptTag)
34+
.replace(/\{\{FRAMEWORK_LIB\}\}/, frameworkLibScriptTag);
35+
res.setHeader('Content-Type', 'text/html');
36+
res.end(output);
37+
});
38+
});
39+
40+
app.get('/util/reporter.js', function(req, res) {
41+
// load the reporters
42+
fs.readFile(path.normalize(path.join(__dirname, './resources/reporter.js')), function(err, data) {
43+
if (err) return fourOhFour(res, err);
44+
res.setHeader('Content-Type', 'text/javascript');
45+
res.end(data.toString());
46+
});
47+
});
48+
49+
app.get('/util/all.js', function(req, res) {
50+
// load the reporters
51+
fs.readFile(path.normalize(path.join(__dirname, './resources/all.js')), function(err, data) {
52+
if (err) return fourOhFour(res, err);
53+
res.setHeader('Content-Type', 'text/javascript');
54+
res.end(data.toString());
55+
});
56+
});
57+
58+
app.get('/util/all.css', function(req, res) {
59+
// load the reporters
60+
fs.readFile(path.normalize(path.join(__dirname, './resources/all.css')), function(err, data) {
61+
if (err) return fourOhFour(res, err);
62+
res.setHeader('Content-Type', 'text/css');
63+
res.end(data.toString());
64+
});
65+
});
66+
67+
app.get('/framework/:framework', function(req, res) {
68+
// load a framework file
69+
var framework = req.params.framework.replace(/\.js$/, '');
70+
fs.readFile(path.normalize(path.join(__dirname, '../impl/'+manifest[framework].impl)), function(err, data) {
71+
if (err) return fourOhFour(res, err);
72+
res.setHeader('Content-Type', 'text/javascript');
73+
res.end(data.toString());
74+
});
75+
});
76+
77+
app.get('/config/:framework', function(req, res) {
78+
// load a config file
79+
var framework = req.params.framework.replace(/\.js$/, '');
80+
fs.readFile(path.normalize(path.join(__dirname, '../impl/'+manifest[framework].config)), function(err, data) {
81+
if (err) return fourOhFour(res, err);
82+
res.setHeader('Content-Type', 'text/javascript');
83+
res.end(data.toString());
84+
});
85+
});
86+
87+
app.get('/:framework/:test/system.js', function(req, res) {
88+
// get a file for the specified test
89+
res.setHeader('Content-Type', 'text/javascript');
90+
if (!systemNotify) {
91+
systemNotify = true;
92+
console.log('"system" module requested by browser. Usually a side-effect of doing static analysis');
93+
}
94+
res.end('', 404);
95+
});
96+
97+
app.get('/:framework/:test/test.html', function(req, res) {
98+
// run one test
99+
fs.readFile(path.normalize(path.join(__dirname, './resources/template.html')), function(err, data) {
100+
if (err) return fourOhFour(res, err);
101+
102+
var framework = '/framework/'+req.params.framework+'.js';
103+
var fwkConfig = '/config/'+req.params.framework+'.js';
104+
var reporter = '/util/reporter.js';
105+
var testFile = '/'+req.params.framework+'/'+req.params.test+'/_test.js';
106+
var testName = req.params.test;
107+
108+
var output = data.toString()
109+
.replace(/\{\{FRAMEWORK\}\}/g, framework)
110+
.replace(/\{\{FRAMEWORK_CONFIG\}\}/g, fwkConfig)
111+
.replace(/\{\{REPORTER\}\}/g, reporter)
112+
.replace(/\{\{TEST\}\}/g, testFile)
113+
.replace(/\{\{TEST_NAME\}\}/g, testName);
114+
res.setHeader('Content-Type', 'text/html');
115+
res.end(output);
116+
});
117+
});
118+
119+
app.get('/:framework/:test/*', function(req, res) {
120+
// get a file for the specified test
121+
var testPath = '../tests/'+req.params.test+'/'+req.params[0];
122+
fs.readFile(path.normalize(path.join(__dirname, testPath)), function(err, data) {
123+
if (err) return fourOhFour(res, err);
124+
res.setHeader('Content-Type', 'text/javascript');
125+
res.end(data.toString());
126+
});
127+
});
128+
129+
app.get('*', function(req, res){
130+
res.send('', 404);
131+
});
132+
133+
app.listen(4000);
134+
135+
util.log('AMD JS Test server running on port 4000');
136+
util.log('To run: http://localhost:4000');
137+
}
138+
139+
// This will be called when an installer has completed. If there are more than one
140+
// manifest with an installer wait for all of them to complete before starting.
141+
function installerCallback(framework, installed) {
142+
manifest[framework].installed = installed;
143+
var readyToStart = true;
144+
for (var framework in manifest) {
145+
if (manifest[framework].installer && manifest[framework].installed === false) {
146+
readyToStart = false;
147+
}
148+
}
149+
150+
if (readyToStart) {
151+
util.log("Installers have completed");
152+
start();
153+
}
154+
}
155+
156+
// Look in the manifests for any installers and run them.
157+
var installer;
158+
for (var framework in manifest) {
159+
if (manifest[framework].installer) {
160+
manifest[framework].installed = false;
161+
installer = require(manifest[framework].installer);
162+
// Call the installer with the express app object allowing it to setup any handlers
163+
installer.install(app, installerCallback);
92164
}
93-
res.end('', 404);
94-
});
95-
96-
app.get('/:framework/:test/test.html', function(req, res) {
97-
// run one test
98-
fs.readFile(path.normalize(path.join(__dirname, './resources/template.html')), function(err, data) {
99-
if (err) return fourOhFour(res, err);
100-
101-
var framework = '/framework/'+req.params.framework+'.js';
102-
var fwkConfig = '/config/'+req.params.framework+'.js';
103-
var reporter = '/util/reporter.js';
104-
var testFile = '/'+req.params.framework+'/'+req.params.test+'/_test.js';
105-
var testName = req.params.test;
106-
107-
var output = data.toString()
108-
.replace(/\{\{FRAMEWORK\}\}/g, framework)
109-
.replace(/\{\{FRAMEWORK_CONFIG\}\}/g, fwkConfig)
110-
.replace(/\{\{REPORTER\}\}/g, reporter)
111-
.replace(/\{\{TEST\}\}/g, testFile)
112-
.replace(/\{\{TEST_NAME\}\}/g, testName);
113-
res.setHeader('Content-Type', 'text/html');
114-
res.end(output);
115-
});
116-
});
117-
118-
app.get('/:framework/:test/*', function(req, res) {
119-
// get a file for the specified test
120-
var testPath = '../tests/'+req.params.test+'/'+req.params[0];
121-
fs.readFile(path.normalize(path.join(__dirname, testPath)), function(err, data) {
122-
if (err) return fourOhFour(res, err);
123-
res.setHeader('Content-Type', 'text/javascript');
124-
res.end(data.toString());
125-
});
126-
});
127-
128-
app.get('*', function(req, res){
129-
res.send('', 404);
130-
});
131-
132-
app.listen(4000);
133-
134-
util.log('AMD JS Test server running on port 4000');
135-
util.log('To run: http://localhost:4000');
165+
}
166+
167+
// Check for any installers. If none found call start();
168+
if (!installer) {
169+
start();
170+
}
171+

0 commit comments

Comments
 (0)