Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/support/writeReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { pathExists, readFile, writeFile, writeJson } from "fs-extra";
import { EOL } from "os";
import { join } from "path";
import * as vscode from "vscode";
import { Logger } from "../logger/logger";
import { reportObj } from "./types";

export async function writeTextReport(
Expand Down Expand Up @@ -217,7 +216,7 @@ export async function writeTextReport(
if (logFileExists) {
const logFileContent = await readFile(logFile, "utf8");
output += `----------------------------------------------------------- Logfile -----------------------------------------------------------------${EOL}`;
output += logFileContent + EOL + lineBreak;
output += replaceUserPathInStr(logFileContent) + EOL + lineBreak;
}
const resultFile = join(context.extensionPath, "report.txt");
await writeFile(resultFile, output);
Expand All @@ -232,13 +231,31 @@ export function replaceUserPath(report: reportObj): reportObj {
const strReport = JSON.stringify(report);

// Replacing all home paths (based on OS) with '...' using es6 syntax. Can be replaced with one line using .replaceAll() when we will update the version of ECMAScript to 2021 or higher
let re = new RegExp(process.env.HOME, "g");
if (process.env.windir) {
const reWin = new RegExp("\\\\", "g");
const result = process.env.HOMEPATH.replace(reWin, "\\\\\\\\");
re = new RegExp(result, "g");
}
const parsedReport = strReport.replace(re, "<HOMEPATH>");
const parsedReport = replaceUserPathInStr(strReport);

return JSON.parse(parsedReport);
}

function replaceUserPathInStr(strReport: string) {
if (process.env.windir) {
const homePath = process.env.HOMEPATH;
// Escape the path for regex, but keep backslashes as is
const escapedPath = homePath.replace(/[.*+?^${}()|[\]\\]/g, (match) => {
return match === "\\" ? "\\\\\\\\" : "\\" + match;
});
// Create pattern that matches both Windows and Posix style
const posixPath = homePath
.replace(/\\/g, "/")
.replace(/[.*+?^${}()|[\]\\]/g, (match) => {
return match === "/" ? "/" : "\\" + match;
});
const pattern = `(${escapedPath}|${posixPath})`;
const re = new RegExp(pattern, "g");
return strReport.replace(re, "<HOMEPATH>");
} else {
// For non-Windows systems, escape HOME path for regex
const escapedHome = process.env.HOME.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const re = new RegExp(escapedHome, "g");
return strReport.replace(re, "<HOMEPATH>");
}
}