11export class KarmaFilesService {
2- constructor ( private http ) { }
2+ private extensionRegex = / \. ( [ ^ . \/ ] + ) $ / ;
3+ private appPrefix = null ;
4+ private testsPrefix = null ;
5+ private nodeModulesPrefix = null ;
6+ private bundle = false ;
37
4- public getServedFilesData ( baseUrl : string , config : IHostConfiguration ) : Promise < IScriptInfo [ ] > {
8+ constructor ( private http , config : IHostConfiguration ) {
9+ this . appPrefix = `/base/${ config . options . appDirectoryRelativePath } /` ;
10+ this . testsPrefix = `/base/${ config . options . appDirectoryRelativePath } /tests` ;
11+ this . nodeModulesPrefix = `/base/node_modules/` ;
12+ this . bundle = config . options . bundle ;
13+ }
14+
15+ public getServedFilesData ( baseUrl : string ) : Promise < IScriptInfo [ ] > {
516 const contextUrl = `${ baseUrl } /context.json` ;
617 console . log ( "NSUTR: downloading " + contextUrl ) ;
7- const bundle = config && config . options && config . options . bundle ;
18+
819 const result = this . http . getString ( contextUrl )
920 . then ( content => {
1021 var parsedContent : IKarmaContext = JSON . parse ( content ) ;
1122 return parsedContent . files ;
1223 } )
1324 . then ( scriptUrls => {
1425 return Promise . all ( scriptUrls . map ( ( url ) : Promise < IScriptInfo > => {
15- var appPrefix = `/base/${ config . options . appDirectoryRelativePath } /` ;
16- const type = this . getScriptType ( url , config ) ;
17- if ( ! bundle && url . startsWith ( appPrefix ) ) {
18- var paramsStart = url . indexOf ( '?' ) ;
19- var relativePath = url . substring ( appPrefix . length , paramsStart ) ;
26+ const { extension, localPath, type } = this . getScriptData ( url ) ;
27+ if ( localPath ) {
2028 return Promise . resolve ( {
2129 url,
2230 type,
23- localPath : '../../../' + relativePath ,
31+ localPath,
2432 } ) ;
2533 } else {
2634 return this . http . getString ( baseUrl + url )
2735 . then ( contents => {
2836 return {
2937 url,
3038 type,
31- contents
39+ contents,
40+ shouldEval : ! extension || extension . toLowerCase ( ) === "js"
3241 } ;
3342 } ) ;
3443 }
@@ -38,15 +47,43 @@ export class KarmaFilesService {
3847 return result ;
3948 }
4049
41- private getScriptType ( url : string , config : IHostConfiguration ) : ScriptTypes {
50+ private getScriptData ( url : string ) : { extension : string , localPath : string , type : ScriptTypes } {
51+ const queryStringStartIndex = url . lastIndexOf ( '?' ) ;
52+ const pathWithoutQueryString = url . substring ( 0 , queryStringStartIndex ) ;
53+ const extension = this . extensionRegex . exec ( pathWithoutQueryString ) [ 1 ] ;
54+
55+ const type = this . getScriptType ( url ) ;
56+
57+ let localPath = null ;
58+ if ( ! this . bundle && url . startsWith ( this . appPrefix ) ) {
59+ localPath = this . getScriptLocalPath ( url , extension ) ;
60+ }
61+
62+ return { extension, localPath, type } ;
63+ }
64+
65+ private getScriptType ( url : string ) : ScriptTypes {
4266 let type = ScriptTypes . CodeUnderTestType ;
4367
44- if ( url . startsWith ( `/base/ ${ config . options . appDirectoryRelativePath } /tests` ) ) {
68+ if ( url . startsWith ( this . testsPrefix ) ) {
4569 type = ScriptTypes . TestType ;
46- } else if ( url . startsWith ( `/base/node_modules/` ) ) {
70+ } else if ( url . startsWith ( this . nodeModulesPrefix ) ) {
4771 type = ScriptTypes . FrameworkAdapterType ;
4872 }
4973
5074 return type ;
5175 }
76+
77+ private getScriptLocalPath ( url : string , scriptExtension : string ) : string {
78+ let localPath = null ;
79+ const queryStringStartIndex = url . lastIndexOf ( '?' ) ;
80+ const relativePath = url . substring ( this . appPrefix . length , queryStringStartIndex ) ;
81+ localPath = '../../../' + relativePath ;
82+
83+ if ( scriptExtension === "ts" ) {
84+ localPath = localPath . substring ( 0 , localPath . length - 2 ) + "js" ;
85+ }
86+
87+ return localPath ;
88+ }
5289}
0 commit comments