import { ICordovaAttachRequestArgs } from "./requestArgs" ; import { logger } from "vscode-debugadapter" ; import { CordovaProjectHelper } from "../utils/cordovaProjectHelper" ; import * as nls from "vscode-nls" ; nls . config ( { messageFormat : nls . MessageFormat . bundle , bundleFormat : nls . BundleFormat . standalone } ) ( ) ; const localize = nls . loadMessageBundle ( ) ; export interface IStringDictionary < T > { [ name ] : T ; } export type ISourceMapPathOverrides = IStringDictionary < string > ; export class JsDebugConfigAdapter { private readonly DefaultWebSourceMapPathOverrides : ISourceMapPathOverrides = { "webpack:///./~/*" : "${cwd}/node_modules/*" , "webpack:///./*" : "${cwd}/*" , "webpack:///*" : "*" , "webpack:///src/*" : "${cwd}/*" , "./*" : "${cwd}/*" , } ; private readonly Ionic3LiveReloadSourceMapPathOverrides : ISourceMapPathOverrides = { "../../*" : "${cwd}/*" , } ; public createChromeDebuggingConfig ( attachArgs , cdpProxyPort , pwaSessionName , sessionId ) { let extraArgs = { } ; if ( ! CordovaProjectHelper . isIonicAngularProject ( attachArgs . cwd ) && ! attachArgs . simulatePort ) { extraArgs . pathMapping = { "/android_asset/www" : `${ attachArgs . cwd }/www` , } ; extraArgs . url = "file:///" ; extraArgs . urlFilter = "*" ; } return Object . assign ( { } , this . getExistingExtraArgs ( attachArgs ) , extraArgs , { type : pwaSessionName , request : "attach" , name : "Attach" , port : cdpProxyPort , webRoot : `${ attachArgs . cwd }/www` , browserAttachLocation : "workspace" , cordovaDebugSessionId : sessionId , } ) ; } public createSafariDebuggingConfig ( attachArgs , cdpProxyPort , pwaSessionName , sessionId ) { let extraArgs = { } ; if ( attachArgs . ionicLiveReload ) { extraArgs . resolveSourceMapLocations = [ "**" , "!**/node_modules/**" , ] ; if ( CordovaProjectHelper . checkIonicVersions ( attachArgs . cwd ) . isIonic3 ) { if ( attachArgs . sourceMapPathOverrides ) { attachArgs . sourceMapPathOverrides = Object . assign ( attachArgs . sourceMapPathOverrides , this . Ionic3LiveReloadSourceMapPathOverrides ) ; } else { attachArgs . sourceMapPathOverrides = Object . assign ( { } , this . DefaultWebSourceMapPathOverrides , this . Ionic3LiveReloadSourceMapPathOverrides ) ; } } } return Object . assign ( { } , this . getExistingExtraArgs ( attachArgs ) , extraArgs , { type : pwaSessionName , request : "attach" , name : "Attach" , port : cdpProxyPort , cordovaDebugSessionId : sessionId , } ) ; } private getExistingExtraArgs ( attachArgs ) { let existingExtraArgs = { } ; if ( attachArgs . env ) { existingExtraArgs . env = attachArgs . env ; } if ( attachArgs . envFile ) { existingExtraArgs . envFile = attachArgs . envFile ; } if ( typeof attachArgs . sourceMaps === "boolean" ) { existingExtraArgs . sourceMaps = attachArgs . sourceMaps ; } existingExtraArgs . sourceMapPathOverrides = this . getSourceMapPathOverrides ( attachArgs . cwd , attachArgs . sourceMapPathOverrides || this . DefaultWebSourceMapPathOverrides ) ; if ( attachArgs . skipFiles ) { existingExtraArgs . skipFiles = attachArgs . skipFiles ; } if ( attachArgs . trace ) { existingExtraArgs . trace = attachArgs . trace ; } if ( attachArgs . attachTimeout ) { existingExtraArgs . timeout = attachArgs . attachTimeout ; } return existingExtraArgs ; } private getSourceMapPathOverrides ( cwd , sourceMapPathOverrides ? ) { return sourceMapPathOverrides ? this . resolveWebRootPattern ( cwd , sourceMapPathOverrides , true ) : this . resolveWebRootPattern ( cwd , this . DefaultWebSourceMapPathOverrides , false ) ; } /**
     * Returns a copy of sourceMapPathOverrides with the ${cwd} pattern resolved in all entries.
     */ private resolveWebRootPattern ( cwd , sourceMapPathOverrides , warnOnMissing ) { const resolvedOverrides = { } ; for ( let pattern in sourceMapPathOverrides ) { const replacePattern = this . replaceWebRootInSourceMapPathOverridesEntry ( cwd , pattern , warnOnMissing ) ; const replacePatternValue = this . replaceWebRootInSourceMapPathOverridesEntry ( cwd , sourceMapPathOverrides [ pattern ] , warnOnMissing ) ; resolvedOverrides [ replacePattern ] = replacePatternValue ; } return resolvedOverrides ; } private replaceWebRootInSourceMapPathOverridesEntry ( cwd , entry , warnOnMissing ) { const cwdIndex = entry . indexOf ( "${cwd}" ) ; if ( cwdIndex === 0 ) { if ( cwd ) { return entry . replace ( "${cwd}" , cwd ) ; } else if ( warnOnMissing ) { logger . warn ( localize ( "SourceMapOverridesEntryContainsCwd" , "Warning: sourceMapPathOverrides entry contains ${cwd}, but cwd is not set" ) ) ; } } else if ( cwdIndex > 0 ) { logger . warn ( localize ( "InASourceMapOverridesEntryCwdValidAtTheBeginning" , "Warning: in a sourceMapPathOverrides entry, ${cwd} is only valid at the beginning of the path" ) ) ; } return entry ; } }