-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlog-source-map-service.ts
216 lines (187 loc) · 8.76 KB
/
log-source-map-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as path from "path";
import * as util from "util";
import * as sourcemap from "source-map";
import * as sourceMapConverter from "convert-source-map";
import * as semver from "semver";
import { stringReplaceAll } from "../common/helpers";
import { ANDROID_DEVICE_APP_ROOT_TEMPLATE, APP_FOLDER_NAME, NODE_MODULES_FOLDER_NAME } from "../constants";
interface IParsedMessage {
filePath?: string;
line?: number;
column?: number;
messagePrefix: string;
messageSuffix: string;
}
interface IFileLocation {
line: number;
column: number;
sourceFile: string;
}
export class LogSourceMapService implements Mobile.ILogSourceMapService {
private static FILE_PREFIX = "file:///";
private static MEMOIZE_FUNCTION_RANDOM_KEY_FOR_JOIN = "__some_random_value__";
private getProjectData: (projectDir: string) => IProjectData;
private getRuntimeVersion: (projectDir: string, platform: string) => string;
private cache: IDictionary<sourcemap.SourceMapConsumer> = {};
private get $platformsDataService(): IPlatformsDataService {
return this.$injector.resolve<IPlatformsDataService>("platformsDataService");
}
constructor(
private $fs: IFileSystem,
private $projectDataService: IProjectDataService,
private $injector: IInjector,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $logger: ILogger) {
this.getProjectData = _.memoize(this.$projectDataService.getProjectData.bind(this.$projectDataService));
this.getRuntimeVersion = _.memoize(this.getRuntimeVersionCore, (...args) => args.join(LogSourceMapService.MEMOIZE_FUNCTION_RANDOM_KEY_FOR_JOIN));
}
public async setSourceMapConsumerForFile(filePath: string): Promise<void> {
try {
if (!this.$fs.getFsStats(filePath).isDirectory()) {
const source = this.$fs.readText(filePath);
const sourceMapRaw = sourceMapConverter.fromSource(source);
let smc: sourcemap.SourceMapConsumer = null;
if (sourceMapRaw && sourceMapRaw.sourcemap) {
const sourceMap = sourceMapRaw.sourcemap;
smc = new sourcemap.SourceMapConsumer(sourceMap);
}
this.cache[filePath] = smc;
}
} catch (err) {
this.$logger.trace(`Unable to set sourceMapConsumer for file ${filePath}. Error is: ${err}`);
}
}
public replaceWithOriginalFileLocations(platform: string, messageData: string, loggingOptions: Mobile.IDeviceLogOptions): string {
if (!messageData || !loggingOptions || !loggingOptions.projectDir) {
return messageData;
}
const projectData = this.getProjectData(loggingOptions.projectDir);
const lines = messageData.split("\n");
const isAndroid = platform.toLowerCase() === this.$devicePlatformsConstants.Android.toLowerCase();
const parserFunction = isAndroid ? this.parseAndroidLog.bind(this, projectData) : this.parseIosLog.bind(this);
let outputData = "";
lines.forEach(rawLine => {
const parsedLine = parserFunction(rawLine);
const originalLocation = this.getOriginalFileLocation(platform, parsedLine, projectData);
if (originalLocation && originalLocation.sourceFile) {
const runtimeVersion = this.getRuntimeVersion(loggingOptions.projectDir, platform);
const { sourceFile, line, column } = originalLocation;
if (semver.valid(runtimeVersion) && semver.gte(semver.coerce(runtimeVersion), "6.1.0")) {
const lastIndexOfFile = rawLine.lastIndexOf(LogSourceMapService.FILE_PREFIX);
const firstPart = rawLine.substr(0, lastIndexOfFile);
outputData += firstPart + rawLine.substr(lastIndexOfFile).replace(/file:\/\/\/.+?:\d+:\d+/, `${LogSourceMapService.FILE_PREFIX}${sourceFile}:${line}:${column}`) + '\n';
} else {
outputData = `${outputData}${parsedLine.messagePrefix}${LogSourceMapService.FILE_PREFIX}${sourceFile}:${line}:${column}${parsedLine.messageSuffix}\n`;
}
} else if (rawLine !== "") {
outputData = `${outputData}${rawLine}\n`;
}
});
return outputData;
}
private getRuntimeVersionCore(projectDir: string, platform: string): string {
let runtimeVersion: string = null;
try {
const projectData = this.getProjectData(projectDir);
const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
const runtimeVersionData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
runtimeVersion = runtimeVersionData && runtimeVersionData.version;
} catch (err) {
this.$logger.trace(`Unable to get runtime version for project directory: ${projectDir} and platform ${platform}. Error is: `, err);
}
return runtimeVersion;
}
private getOriginalFileLocation(platform: string, parsedLine: IParsedMessage, projectData: IProjectData): IFileLocation {
const fileLocation = path.join(this.getFilesLocation(platform, projectData), APP_FOLDER_NAME);
if (parsedLine && parsedLine.filePath) {
const sourceMapFile = path.join(fileLocation, parsedLine.filePath);
const smc = this.cache[sourceMapFile];
if (smc) {
const originalPosition = smc.originalPositionFor({ line: parsedLine.line, column: parsedLine.column });
let sourceFile = originalPosition.source && originalPosition.source.replace("webpack:///", "");
if (sourceFile) {
if (!_.startsWith(sourceFile, NODE_MODULES_FOLDER_NAME)) {
sourceFile = path.join(projectData.getAppDirectoryRelativePath(), sourceFile);
}
sourceFile = stringReplaceAll(sourceFile, "/", path.sep);
return { sourceFile, line: originalPosition.line, column: originalPosition.column };
}
}
}
}
private parseAndroidLog(projectData: IProjectData, rawMessage: string): IParsedMessage {
// "JS: at module.exports.push../main-view-model.ts.HelloWorldModel.onTap (file:///data/data/org.nativescript.sourceMap/files/app/bundle.js:303:17)"
// "System.err: File: "file:///data/data/org.nativescript.sourceMap/files/app/bundle.js, line: 304, column: 8"
const fileIndex = rawMessage.lastIndexOf(LogSourceMapService.FILE_PREFIX);
const deviceProjectPath = util.format(ANDROID_DEVICE_APP_ROOT_TEMPLATE, projectData.projectIdentifiers.android);
let separator = ",";
let messageSuffix = "";
let parts, filePath, line, column, messagePrefix;
if (fileIndex >= 0) {
const fileSubstring = rawMessage.substring(fileIndex + LogSourceMapService.FILE_PREFIX.length);
//"data/data/org.nativescript.sourceMap/files/app/bundle.js, line: 304, column: 8"
parts = fileSubstring.split(separator);
if (parts.length >= 3) {
// "data/data/org.nativescript.sourceMap/files/app/bundle.js"
parts[0] = parts[0].replace("'", "");
// " line: 304"
parts[1] = parts[1].replace(" line: ", "");
// " column: 8"
parts[2] = parts[2].replace(" column: ", "");
} else {
// "data/data/org.nativescript.sourceMap/files/app/bundle.js:303:17)"
separator = ":";
parts = fileSubstring.split(separator);
}
if (parts.length >= 3) {
// "/data/data/org.nativescript.sourceMap/files/app/"
const devicePath = `${deviceProjectPath}/${APP_FOLDER_NAME}/`;
// "bundle.js"
filePath = path.relative(devicePath, `${"/"}${parts[0]}`);
line = parseInt(parts[1]);
column = parseInt(parts[2]);
messagePrefix = rawMessage.substring(0, fileIndex);
for (let i = 3; i < parts.length; i++) {
messageSuffix += `${parts[i]}${i === (parts.length - 1) ? "" : separator}`;
}
// "JS: at module.exports.push../main-view-model.ts.HelloWorldModel.onTap ("
messagePrefix = _.trimEnd(messagePrefix, "(");
}
}
return { filePath, line, column, messagePrefix, messageSuffix };
}
private parseIosLog(rawMessage: string): IParsedMessage {
// "CONSOLE INFO file:///app/vendor.js:131:36: HMR: Hot Module Replacement Enabled. Waiting for signal."
const fileIndex = rawMessage.lastIndexOf(LogSourceMapService.FILE_PREFIX);
let messageSuffix = "";
let parts, filePath, line, column, messagePrefix;
if (fileIndex >= 0) {
// "app/vendor.js:131:36: HMR: Hot Module Replacement Enabled. Waiting for signal."
const fileSubstring = rawMessage.substring(fileIndex + LogSourceMapService.FILE_PREFIX.length);
parts = fileSubstring.split(":");
if (parts && parts.length >= 3) {
filePath = parts[0];
// "app/vendor.js"
if (_.startsWith(filePath, APP_FOLDER_NAME)) {
filePath = path.relative(APP_FOLDER_NAME, parts[0]);
}
line = parseInt(parts[1]);
column = parseInt(parts[2]);
messagePrefix = rawMessage.substring(0, fileIndex);
for (let i = 3; i < parts.length; i++) {
messageSuffix += `${parts[i]}${i === (parts.length - 1) ? "" : ":"}`;
}
}
}
return { filePath, line, column, messagePrefix, messageSuffix };
}
private getFilesLocation(platform: string, projectData: IProjectData): string {
try {
const platformsData = this.$platformsDataService.getPlatformData(platform.toLowerCase(), projectData);
return platformsData.appDestinationDirectoryPath;
} catch (err) {
return "";
}
}
}
$injector.register("logSourceMapService", LogSourceMapService);