@@ -240,13 +240,13 @@ export function webkitUrlToClientPath(webRoot: string, additionalFileExtension:
240240 return '' ;
241241 }
242242
243- aUrl = decodeURI ( aUrl ) ;
244-
245- // If we don't have the client workingDirectory for some reason, don't try to map the url to a client path
243+ // If we don't have the client workingDirectory for some reason, don't try to map the url to a client path
246244 if ( ! webRoot ) {
247245 return '' ;
248246 }
249247
248+ aUrl = decodeURI ( aUrl ) ;
249+
250250 // Search the filesystem under the webRoot for the file that best matches the given url
251251 let pathName = url . parse ( canonicalizeUrl ( aUrl ) ) . pathname ;
252252 if ( ! pathName || pathName === '/' ) {
@@ -261,14 +261,16 @@ export function webkitUrlToClientPath(webRoot: string, additionalFileExtension:
261261 return nsProjectFile ;
262262 }
263263
264+ let shiftedParts = [ ] ;
264265 let pathParts = pathName . split ( path . sep ) ;
265266 while ( pathParts . length > 0 ) {
266267 const clientPath = path . join ( webRoot , pathParts . join ( path . sep ) ) ;
267268 if ( existsSync ( clientPath ) ) {
268269 return canonicalizeUrl ( clientPath ) ;
269270 }
270271
271- pathParts . shift ( ) ;
272+ let shifted = pathParts . shift ( ) ;
273+ shiftedParts . push ( shifted ) ;
272274 }
273275
274276 //check for {N} android internal files
@@ -285,6 +287,64 @@ export function webkitUrlToClientPath(webRoot: string, additionalFileExtension:
285287 return '' ;
286288}
287289
290+ /**
291+ * Infers the device root of a given path.
292+ * The device root is the parent directory of all {N} source files
293+ * This implementation assumes that all files are all under one common root on the device
294+ * Returns all the device parent directories of a source file until the file is found on the client by client path
295+ */
296+ export function inferDeviceRoot ( projectRoot : string , additionalFileExtension : string , aUrl : string ) : string {
297+ if ( ! aUrl ) {
298+ return null ;
299+ }
300+
301+ // If we don't have the projectRoot for some reason, don't try to map the url to a client path
302+ if ( ! projectRoot ) {
303+ return null ;
304+ }
305+
306+ aUrl = decodeURI ( aUrl ) ;
307+
308+ // Search the filesystem under the webRoot for the file that best matches the given url
309+ let pathName = url . parse ( canonicalizeUrl ( aUrl ) ) . pathname ;
310+ if ( ! pathName || pathName === '/' ) {
311+ return null ;
312+ }
313+
314+ // Dealing with the path portion of either a url or an absolute path to remote file.
315+ // Need to force path.sep separator
316+ pathName = pathName . replace ( / \/ / g, path . sep ) ;
317+
318+ let shiftedParts = [ ] ;
319+ let pathParts = pathName . split ( path . sep ) ;
320+ while ( pathParts . length > 0 ) {
321+ const clientPath = path . join ( projectRoot , pathParts . join ( path . sep ) ) ;
322+ if ( existsSync ( clientPath ) ) {
323+ //return canonicalizeUrl(clientPath);
324+ return shiftedParts . join ( path . sep ) . replace ( / \\ / g, "/" ) ;
325+ }
326+
327+ let shifted = pathParts . shift ( ) ;
328+ shiftedParts . push ( shifted ) ;
329+ }
330+
331+ //check for {N} android internal files
332+ shiftedParts = [ ] ;
333+ pathParts = pathName . split ( path . sep ) ;
334+ while ( pathParts . length > 0 ) {
335+ const clientPath = path . join ( projectRoot , "platforms/android/src/main/assets" , pathParts . join ( path . sep ) ) ;
336+ if ( existsSync ( clientPath ) ) {
337+ //return canonicalizeUrl(clientPath);
338+ return shiftedParts . join ( path . sep ) . replace ( / \\ / g, "/" ) ;
339+ }
340+
341+ let shifted = pathParts . shift ( ) ;
342+ shiftedParts . push ( shifted ) ;
343+ }
344+
345+ return null ;
346+ }
347+
288348/**
289349 * Modify a url either from the client or the webkit target to a common format for comparing.
290350 * The client can handle urls in this format too.
0 commit comments