@@ -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 ( / \{ \{ H A S T E S T S \} \} / g, ( req . query . framework ) ? 'has-tests' : '' )
30- . replace ( / \{ \{ F R A M E W O R K _ O P T I O N S \} \} / g, frameworkOptions . join ( '\n' ) )
31- . replace ( / \{ \{ F R A M E W O R K \} \} / g, req . query . framework )
32- . replace ( / \{ \{ F R A M E W O R K _ C O N F I G \} \} / , frameworkConfigScriptTag )
33- . replace ( / \{ \{ F R A M E W O R K _ L I B \} \} / , 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 ( / \. j s $ / , '' ) ;
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 ( / \. j s $ / , '' ) ;
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 ( / \{ \{ H A S T E S T S \} \} / g, ( req . query . framework ) ? 'has-tests' : '' )
31+ . replace ( / \{ \{ F R A M E W O R K _ O P T I O N S \} \} / g, frameworkOptions . join ( '\n' ) )
32+ . replace ( / \{ \{ F R A M E W O R K \} \} / g, req . query . framework )
33+ . replace ( / \{ \{ F R A M E W O R K _ C O N F I G \} \} / , frameworkConfigScriptTag )
34+ . replace ( / \{ \{ F R A M E W O R K _ L I B \} \} / , 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 ( / \. j s $ / , '' ) ;
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 ( / \. j s $ / , '' ) ;
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 ( / \{ \{ F R A M E W O R K \} \} / g, framework )
110+ . replace ( / \{ \{ F R A M E W O R K _ C O N F I G \} \} / g, fwkConfig )
111+ . replace ( / \{ \{ R E P O R T E R \} \} / g, reporter )
112+ . replace ( / \{ \{ T E S T \} \} / g, testFile )
113+ . replace ( / \{ \{ T E S T _ N A M E \} \} / 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 ( / \{ \{ F R A M E W O R K \} \} / g, framework )
109- . replace ( / \{ \{ F R A M E W O R K _ C O N F I G \} \} / g, fwkConfig )
110- . replace ( / \{ \{ R E P O R T E R \} \} / g, reporter )
111- . replace ( / \{ \{ T E S T \} \} / g, testFile )
112- . replace ( / \{ \{ T E S T _ N A M E \} \} / 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