import * as path from "path" ; import * as fs from "fs" ; import * as url from "url" ; import { ICordovaAttachRequestArgs } from "../requestArgs" ; import { PlatformType } from "../cordovaDebugSession" ; import { CordovaProjectHelper } from "../../utils/cordovaProjectHelper" ; import { IProjectType } from "../../utils/cordovaProjectHelper" ; export class SourcemapPathTransformer { private _cordovaRoot : string ; private _platform : string ; private _webRoot : string ; private _projectTypes : IProjectType ; private _ionicLiveReload : boolean ; private _debugRequestType : string ; constructor ( args , projectTypes ) { this . _cordovaRoot = args . cwd ; this . _platform = args . platform . toLowerCase ( ) ; this . _webRoot = args . address || this . _cordovaRoot ; this . _ionicLiveReload = args . ionicLiveReload || false ; this . _projectTypes = projectTypes ; this . _debugRequestType = args . request ; } public getClientPathFromFileBasedUrl ( sourceUrl ) { const regExp = new RegExp ( "file:\\/\\/\\/.*\\.app(?:\\/www)*(\\/.*\\.(js|html))" , "g" ) ; let foundStrings = regExp . exec ( sourceUrl ) ; if ( foundStrings && foundStrings [ 1 ] ) { return this . getClientPath ( foundStrings [ 1 ] ) ; } else { return this . getClientPath ( "/" ) ; } } public getClientPathFromHttpBasedUrl ( sourceUrl ) { let relativeSourcePath ; try { relativeSourcePath = url . parse ( sourceUrl ) . pathname || "/" ; } catch ( err ) { relativeSourcePath = "/" ; } return this . getClientPath ( relativeSourcePath ) ; } public getClientPath ( relativeSourcePath ) { let wwwRoot = path . join ( this . _cordovaRoot , "www" ) ; let defaultPath = "" ; let foldersForSearch = [ this . _webRoot , this . _cordovaRoot , wwwRoot ] ; if ( this . _projectTypes . isIonic4 || this . _projectTypes . isIonic5 ) { if ( ( this . _platform === PlatformType . Serve || this . _ionicLiveReload ) && this . _debugRequestType === "launch" ) { foldersForSearch . pop ( ) ; } } for ( const searchFolder of foldersForSearch ) { let mappedPath = this . targetUrlToClientPath ( relativeSourcePath , searchFolder ) ; if ( mappedPath ) { defaultPath = mappedPath ; break ; } } if ( defaultPath . toLowerCase ( ) . indexOf ( wwwRoot . toLowerCase ( ) ) === 0 ) { let relativePath = path . relative ( wwwRoot , defaultPath ) ; let mergesPath = path . join ( this . _cordovaRoot , "merges" , this . _platform , relativePath ) ; if ( fs . existsSync ( mergesPath ) ) { return mergesPath ; } } return defaultPath ; } private targetUrlToClientPath ( sourceUrlPath , searchFolder ) { const rest = sourceUrlPath . substring ( 1 ) ; const absoluteSourcePath = rest ? CordovaProjectHelper . properJoin ( searchFolder , rest ) : searchFolder ; return CordovaProjectHelper . existsSync ( absoluteSourcePath ) ? absoluteSourcePath : "" ; } }