-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWinWSLCodacyCli.ts
More file actions
55 lines (48 loc) · 2.04 KB
/
WinWSLCodacyCli.ts
File metadata and controls
55 lines (48 loc) · 2.04 KB
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
import { MacCodacyCli } from './MacCodacyCli.js';
export class WinWSLCodacyCli extends MacCodacyCli {
constructor(rootPath: string, provider?: string, organization?: string, repository?: string) {
const winRootPath = rootPath.startsWith('/mnt/')
? WinWSLCodacyCli.fromWSLPath(rootPath)
: rootPath;
super(winRootPath, provider, organization, repository);
}
private static toWSLPath(path: string): string {
// First, remove outer quotes if present
const cleanPath = path.replace(/^["']|["']$/g, '');
// Convert backslashes to slashes and handle drive letter
const wslPath = cleanPath
.replace(/\\/g, '/')
.replace(/^([a-zA-Z]):/, (match, letter) => `/mnt/${letter.toLowerCase()}`);
return wslPath;
}
private static fromWSLPath(path: string): string {
// Convert WSL path to Windows path
// Example: /mnt/c/Users/user/project -> C:\Users\user\project
const windowsPath = path
.replace(/^'\/mnt\/([a-zA-Z])/, (match, letter) => `'${letter.toUpperCase()}:`)
.replace(/^\/mnt\/([a-zA-Z])/, (match, letter) => `${letter.toUpperCase()}:`)
.replace(/\//g, '\\');
return windowsPath;
}
protected preparePathForExec(path: string): string {
// Convert WSL path to Windows format for validation
const winFilePath = path.startsWith('/mnt/') ? WinWSLCodacyCli.fromWSLPath(path) : path;
// Validate path security before escaping
if (!this.isPathSafe(winFilePath)) {
throw new Error(`Unsafe file path rejected: ${winFilePath}`);
}
// Convert to WSL format and escape special characters
const wslPath = WinWSLCodacyCli.toWSLPath(winFilePath);
const escapedPath = wslPath.replace(/([\s'"\\;&|`$()[\]{}*?~<>])/g, '\\$1');
return `'${escapedPath}'`;
}
protected async execAsync(
command: string,
args?: Record<string, string>
): Promise<{ stdout: string; stderr: string }> {
return await super.execAsync(`wsl bash -c "${command}"`, args);
}
protected getCliCommand(): string {
return WinWSLCodacyCli.toWSLPath(super.getCliCommand());
}
}