1
1
export class KarmaFilesService {
2
+ private extensionRegex = / \. ( [ ^ . \/ ] + ) $ / ;
3
+
2
4
constructor ( private http ) { }
3
5
4
6
public getServedFilesData ( baseUrl : string , config : IHostConfiguration ) : Promise < IScriptInfo [ ] > {
5
7
const contextUrl = `${ baseUrl } /context.json` ;
6
8
console . log ( "NSUTR: downloading " + contextUrl ) ;
7
9
const bundle = config && config . options && config . options . bundle ;
10
+ const appPrefix = `/base/${ config . options . appDirectoryRelativePath } /` ;
8
11
const result = this . http . getString ( contextUrl )
9
12
. then ( content => {
10
13
var parsedContent : IKarmaContext = JSON . parse ( content ) ;
11
14
return parsedContent . files ;
12
15
} )
13
16
. then ( scriptUrls => {
14
17
return Promise . all ( scriptUrls . map ( ( url ) : Promise < IScriptInfo > => {
15
- var appPrefix = `/base/${ config . options . appDirectoryRelativePath } /` ;
16
18
const type = this . getScriptType ( url , config ) ;
19
+ const scriptExtension = this . getScriptExtension ( url ) ;
17
20
if ( ! bundle && url . startsWith ( appPrefix ) ) {
18
- var paramsStart = url . indexOf ( '?' ) ;
19
- var relativePath = url . substring ( appPrefix . length , paramsStart ) ;
21
+ const localPath = this . getScriptLocalPath ( url , scriptExtension , appPrefix ) ;
20
22
return Promise . resolve ( {
21
23
url,
22
24
type,
23
- localPath : '../../../' + relativePath ,
25
+ localPath,
24
26
} ) ;
25
27
} else {
26
28
return this . http . getString ( baseUrl + url )
27
29
. then ( contents => {
30
+ const shouldEval = ! scriptExtension || scriptExtension . toLowerCase ( ) === "js" ;
28
31
return {
29
32
url,
30
33
type,
31
- contents
34
+ contents,
35
+ shouldEval
32
36
} ;
33
37
} ) ;
34
38
}
@@ -49,4 +53,21 @@ export class KarmaFilesService {
49
53
50
54
return type ;
51
55
}
56
+
57
+ private getScriptExtension ( url : string ) : string {
58
+ const queryStringStart = url . lastIndexOf ( '?' ) ;
59
+ const pathWithoutQueryString = url . substring ( 0 , queryStringStart ) ;
60
+ const scriptExtension = this . extensionRegex . exec ( pathWithoutQueryString ) [ 1 ] ;
61
+ return scriptExtension ;
62
+ }
63
+
64
+ private getScriptLocalPath ( url : string , scriptExtension : string , appPrefix : string ) : string {
65
+ const paramsStart = url . indexOf ( '?' ) ;
66
+ const relativePath = url . substring ( appPrefix . length , paramsStart ) ;
67
+ let localPath = '../../../' + relativePath ;
68
+ if ( scriptExtension === "ts" ) {
69
+ localPath = localPath . substring ( 0 , localPath . length - 2 ) + "js" ;
70
+ }
71
+ return localPath ;
72
+ }
52
73
}
0 commit comments