|
| 1 | +const sourcemap = require("source-map"); |
| 2 | +import * as path from "path"; |
| 3 | +const sourceMapConverter = require("convert-source-map"); |
| 4 | +import { ANDROID_DEVICE_APP_ROOT_TEMPLATE, APP_FOLDER_NAME, NODE_MODULES_FOLDER_NAME } from "../constants"; |
| 5 | +import * as util from "util" |
| 6 | + |
| 7 | +interface IParsedMessage { |
| 8 | + filePath?: string, |
| 9 | + line?: number, |
| 10 | + column?: number, |
| 11 | + message: string |
| 12 | +} |
| 13 | + |
| 14 | +interface IFileLocation { |
| 15 | + line: number, |
| 16 | + column: number, |
| 17 | + sourceFile: string |
| 18 | +} |
| 19 | + |
| 20 | +export class LogSourceMapService { |
| 21 | + private static FILE_PREFIX = "file:///"; |
| 22 | + constructor( |
| 23 | + private $fs: IFileSystem, |
| 24 | + private $projectDataService: IProjectDataService, |
| 25 | + private $injector: IInjector, |
| 26 | + private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { |
| 27 | + } |
| 28 | + |
| 29 | + public replaceWithOriginalFileLocations(platform: string, messageData: string): string { |
| 30 | + if (!messageData) { |
| 31 | + return messageData; |
| 32 | + } |
| 33 | + |
| 34 | + const lines = messageData.split("\n"); |
| 35 | + const isAndroid = platform.toLowerCase() === this.$devicePlatformsConstants.Android.toLowerCase(); |
| 36 | + const parserFunction = isAndroid ? this.parseAndroidLog.bind(this) : this.parseIosLog.bind(this); |
| 37 | + let outputData = ""; |
| 38 | + |
| 39 | + lines.forEach(rawLine => { |
| 40 | + const parsedLine = parserFunction(rawLine); |
| 41 | + const originalLocation = this.getOriginalFileLocation(platform, parsedLine); |
| 42 | + |
| 43 | + if (originalLocation && originalLocation.sourceFile) { |
| 44 | + const {sourceFile, line, column} = originalLocation; |
| 45 | + outputData = `${outputData}${parsedLine.message} ${LogSourceMapService.FILE_PREFIX}${sourceFile}:${line}:${column}\n` |
| 46 | + } else if (rawLine !== "") { |
| 47 | + outputData = `${outputData}${rawLine}\n` |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + return outputData; |
| 52 | + } |
| 53 | + |
| 54 | + private getOriginalFileLocation(platform: string, parsedLine: IParsedMessage): IFileLocation { |
| 55 | + const fileLoaction = path.join(this.getFilesLocation(platform), APP_FOLDER_NAME); |
| 56 | + |
| 57 | + if (parsedLine && parsedLine.filePath) { |
| 58 | + const sourceMapFile = path.join(fileLoaction, parsedLine.filePath); |
| 59 | + if (this.$fs.exists(sourceMapFile)) { |
| 60 | + const source = this.$fs.readText(sourceMapFile); |
| 61 | + const sourceMapRaw = sourceMapConverter.fromSource(source); |
| 62 | + if (sourceMapRaw && sourceMapRaw.sourcemap) { |
| 63 | + const sourceMap = sourceMapRaw.sourcemap; |
| 64 | + const smc = new sourcemap.SourceMapConsumer(sourceMap); |
| 65 | + const originalPosition = smc.originalPositionFor({ line: parsedLine.line, column: parsedLine.column }); |
| 66 | + let sourceFile = originalPosition.source && originalPosition.source.replace("webpack:///", ""); |
| 67 | + if (sourceFile) { |
| 68 | + if (!_.startsWith(sourceFile, NODE_MODULES_FOLDER_NAME)) { |
| 69 | + sourceFile = path.join(APP_FOLDER_NAME, sourceFile); |
| 70 | + } |
| 71 | + |
| 72 | + return { sourceFile, line: originalPosition.line, column: originalPosition.column}; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private parseAndroidLog(rawMessage: string): IParsedMessage { |
| 80 | + const fileIndex = rawMessage.lastIndexOf(LogSourceMapService.FILE_PREFIX); |
| 81 | + const projectData = this.$projectDataService.getProjectData(); |
| 82 | + const deviceProjectPath = util.format(ANDROID_DEVICE_APP_ROOT_TEMPLATE, projectData.projectIdentifiers.android); |
| 83 | + let message = rawMessage; |
| 84 | + let parts, filePath, line, column; |
| 85 | + |
| 86 | + |
| 87 | + if (fileIndex >= 0) { |
| 88 | + const fileSubstring = rawMessage.substring(fileIndex + LogSourceMapService.FILE_PREFIX.length); |
| 89 | + parts = fileSubstring.split(","); |
| 90 | + if (parts.length >= 3) { |
| 91 | + parts[0] = parts[0].replace("'", ""); |
| 92 | + parts[1] = parts[1].replace(" line: ", ""); |
| 93 | + parts[2] = parts[2].replace(" column: ", ""); |
| 94 | + } else { |
| 95 | + parts = fileSubstring.split(":"); |
| 96 | + } |
| 97 | + |
| 98 | + if (parts.length >= 3) { |
| 99 | + const devicePath = `${deviceProjectPath}/${APP_FOLDER_NAME}/`; |
| 100 | + filePath = path.relative(devicePath, parts[0]); |
| 101 | + line = parseInt(parts[1]); |
| 102 | + column = parseInt(parts[2]); |
| 103 | + message = rawMessage.substring(0, fileIndex); |
| 104 | + for (let i = 3; i < parts.length; i++) { |
| 105 | + message += parts[i]; |
| 106 | + } |
| 107 | + message = _.trimEnd(message, "("); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return { filePath, line, column, message } |
| 112 | + } |
| 113 | + |
| 114 | + private parseIosLog(rawMessage: string): IParsedMessage { |
| 115 | + const fileIndex = rawMessage.lastIndexOf(LogSourceMapService.FILE_PREFIX); |
| 116 | + let message = rawMessage; |
| 117 | + let parts, filePath, line, column; |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + if (fileIndex >= 0) { |
| 122 | + const fileSubstring = rawMessage.substring(fileIndex + LogSourceMapService.FILE_PREFIX.length); |
| 123 | + parts = fileSubstring.split(":"); |
| 124 | + |
| 125 | + if (parts && parts.length >= 3) { |
| 126 | + filePath = parts[0]; |
| 127 | + if (_.startsWith(filePath, APP_FOLDER_NAME)) { |
| 128 | + filePath = path.relative(APP_FOLDER_NAME ,parts[0]); |
| 129 | + } |
| 130 | + line = parseInt(parts[1]); |
| 131 | + column = parseInt(parts[2]); |
| 132 | + |
| 133 | + message = rawMessage.substring(0, fileIndex).trim(); |
| 134 | + for (let i = 3; i < parts.length; i++) { |
| 135 | + message += parts[i]; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return { filePath, line, column, message } |
| 141 | + } |
| 142 | + |
| 143 | + private getFilesLocation(platform: string): string { |
| 144 | + try { |
| 145 | + const projectData = this.$projectDataService.getProjectData(); |
| 146 | + const platformsData = this.$injector.resolve("platformsData").getPlatformData(platform.toLowerCase(), projectData); |
| 147 | + return platformsData.appDestinationDirectoryPath; |
| 148 | + } catch (err) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +$injector.register("logSourceMapService", LogSourceMapService); |
0 commit comments