@@ -48,6 +48,53 @@ function log(msg: string) {
4848 }
4949}
5050
51+
52+ let directorySeparator = "/" ;
53+
54+ function getRootLength ( path : string ) : number {
55+ if ( path . charAt ( 0 ) === directorySeparator ) {
56+ if ( path . charAt ( 1 ) !== directorySeparator ) return 1 ;
57+ const p1 = path . indexOf ( "/" , 2 ) ;
58+ if ( p1 < 0 ) return 2 ;
59+ const p2 = path . indexOf ( "/" , p1 + 1 ) ;
60+ if ( p2 < 0 ) return p1 + 1 ;
61+ return p2 + 1 ;
62+ }
63+ if ( path . charAt ( 1 ) === ":" ) {
64+ if ( path . charAt ( 2 ) === directorySeparator ) return 3 ;
65+ return 2 ;
66+ }
67+ // Per RFC 1738 'file' URI schema has the shape file://<host>/<path>
68+ // if <host> is omitted then it is assumed that host value is 'localhost',
69+ // however slash after the omitted <host> is not removed.
70+ // file:///folder1/file1 - this is a correct URI
71+ // file://folder2/file2 - this is an incorrect URI
72+ if ( path . lastIndexOf ( "file:///" , 0 ) === 0 ) {
73+ return "file:///" . length ;
74+ }
75+ const idx = path . indexOf ( "://" ) ;
76+ if ( idx !== - 1 ) {
77+ return idx + "://" . length ;
78+ }
79+ return 0 ;
80+ }
81+
82+ function getDirectoryPath ( path : string ) : any {
83+ path = switchToForwardSlashes ( path ) ;
84+ return path . substr ( 0 , Math . max ( getRootLength ( path ) , path . lastIndexOf ( directorySeparator ) ) ) ;
85+ }
86+
87+ function ensureDirectoriesExist ( path : string ) {
88+ path = switchToForwardSlashes ( path ) ;
89+ if ( path . length > getRootLength ( path ) && ! fs . existsSync ( path ) ) {
90+ const parentDirectory = getDirectoryPath ( path ) ;
91+ ensureDirectoriesExist ( parentDirectory ) ;
92+ if ( ! fs . existsSync ( path ) ) {
93+ fs . mkdirSync ( path ) ;
94+ }
95+ }
96+ }
97+
5198// Copied from the compiler sources
5299function dir ( path : string , spec ?: string , options ?: any ) {
53100 options = options || < { recursive ?: boolean ; } > { } ;
@@ -94,28 +141,16 @@ function deleteFolderRecursive(path: string) {
94141} ;
95142
96143function writeFile ( path : string , data : any , opts : { recursive : boolean } ) {
97- try {
98- fs . writeFileSync ( path , data ) ;
99- } catch ( e ) {
100- // assume file was written to a directory that exists, if not, start recursively creating them as necessary
101- var parts = switchToForwardSlashes ( path ) . split ( '/' ) ;
102- for ( var i = 0 ; i < parts . length ; i ++ ) {
103- var subDir = parts . slice ( 0 , i ) . join ( '/' ) ;
104- if ( ! fs . existsSync ( subDir ) ) {
105- fs . mkdir ( subDir ) ;
106- }
107- }
108- fs . writeFileSync ( path , data ) ;
109- }
144+ ensureDirectoriesExist ( getDirectoryPath ( path ) ) ;
145+ fs . writeFileSync ( path , data ) ;
110146}
111147
112148/// Request Handling ///
113149
114150function handleResolutionRequest ( filePath : string , res : http . ServerResponse ) {
115- var resolvedPath = path . resolve ( filePath , '' ) ;
116- resolvedPath = resolvedPath . substring ( resolvedPath . indexOf ( 'tests' ) ) ;
151+ var resolvedPath = path . resolve ( filePath ) ;
117152 resolvedPath = switchToForwardSlashes ( resolvedPath ) ;
118- send ( 'success' , res , resolvedPath ) ;
153+ send ( 'success' , res , resolvedPath , 'text/javascript' ) ;
119154 return ;
120155}
121156
@@ -174,6 +209,7 @@ function getRequestOperation(req: http.ServerRequest, filename: string) {
174209 else return RequestType . GetDir ;
175210 }
176211 else {
212+
177213 var queryData : any = url . parse ( req . url , true ) . query ;
178214 if ( req . method === 'GET' && queryData . resolve !== undefined ) return RequestType . ResolveFile
179215 // mocha uses ?grep=<regexp> query string as equivalent to the --grep command line option used to filter tests
@@ -223,7 +259,7 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
223259 send ( 'success' , res , null ) ;
224260 break ;
225261 case RequestType . WriteDir :
226- fs . mkdirSync ( reqPath ) ;
262+ ensureDirectoriesExist ( reqPath ) ;
227263 send ( 'success' , res , null ) ;
228264 break ;
229265 case RequestType . DeleteFile :
0 commit comments