1
1
export 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 ;
3
7
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 [ ] > {
5
16
const contextUrl = `${ baseUrl } /context.json` ;
6
17
console . log ( "NSUTR: downloading " + contextUrl ) ;
7
- const bundle = config && config . options && config . options . bundle ;
18
+
8
19
const result = this . http . getString ( contextUrl )
9
20
. then ( content => {
10
21
var parsedContent : IKarmaContext = JSON . parse ( content ) ;
11
22
return parsedContent . files ;
12
23
} )
13
24
. then ( scriptUrls => {
14
25
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 ) {
20
28
return Promise . resolve ( {
21
29
url,
22
30
type,
23
- localPath : '../../../' + relativePath ,
31
+ localPath,
24
32
} ) ;
25
33
} else {
26
34
return this . http . getString ( baseUrl + url )
27
35
. then ( contents => {
28
36
return {
29
37
url,
30
38
type,
31
- contents
39
+ contents,
40
+ shouldEval : ! extension || extension . toLowerCase ( ) === "js"
32
41
} ;
33
42
} ) ;
34
43
}
@@ -38,15 +47,43 @@ export class KarmaFilesService {
38
47
return result ;
39
48
}
40
49
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 {
42
66
let type = ScriptTypes . CodeUnderTestType ;
43
67
44
- if ( url . startsWith ( `/base/ ${ config . options . appDirectoryRelativePath } /tests` ) ) {
68
+ if ( url . startsWith ( this . testsPrefix ) ) {
45
69
type = ScriptTypes . TestType ;
46
- } else if ( url . startsWith ( `/base/node_modules/` ) ) {
70
+ } else if ( url . startsWith ( this . nodeModulesPrefix ) ) {
47
71
type = ScriptTypes . FrameworkAdapterType ;
48
72
}
49
73
50
74
return type ;
51
75
}
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
+ }
52
89
}
0 commit comments