@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 120 lines 4.02 KB
Raw
1 export interface WslDistro {
2 readonly name: string;
3 readonly isDefault: boolean;
4 readonly version: 1 | 2;
5 }
6
7 export interface WslConfig {
8 readonly distro: string | null;
9 }
10
11 // Literal spaces only; \s would accept controls and corrupt UNC distro paths.
12 // "." and ".." would be path segments in a UNC path, not distro names.
13 const DISTRO_NAME_PATTERN = /^(?!\.{1,2}$)[\w.-](?:[\w .-]*[\w.-])?$/;
14
15 export function parseWslDistroList(stdout: Buffer): readonly WslDistro[] {
16 const hasUtf16Bom = stdout.length >= 2 && stdout[0] === 0xff && stdout[1] === 0xfe;
17 const likelyUtf16Le =
18 hasUtf16Bom ||
19 Array.from(stdout.subarray(0, Math.min(stdout.length, 80))).filter(
20 (byte, index) => index % 2 === 1 && byte === 0,
21 ).length > 10;
22 let text = stdout.toString(likelyUtf16Le ? "utf16le" : "utf8");
23 if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
24
25 const lines = text.split(/\r?\n/).filter((line) => line.trim().length > 0);
26 const distros: WslDistro[] = [];
27 for (let i = 1; i < lines.length; i++) {
28 const line = lines[i]!;
29 const isDefault = line.startsWith("*");
30 const cleaned = isDefault ? line.slice(1).trim() : line.trim();
31 const fields = cleaned.split(/\s{2,}/);
32 if (fields.length < 3) continue;
33 const name = fields[0]!.trim();
34 const versionNum = parseInt(fields[2]!, 10);
35 if (!name || (versionNum !== 1 && versionNum !== 2)) continue;
36 distros.push({ name, isDefault, version: versionNum as 1 | 2 });
37 }
38 return distros;
39 }
40
41 // Resolve UNC paths in their owning distro, which can differ from the configured desktop backend.
42 export function extractDistroFromUncPath(windowsPath: string): string | null {
43 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)/i.exec(windowsPath);
44 if (!match) return null;
45 const candidate = match[1]!;
46 return DISTRO_NAME_PATTERN.test(candidate) ? candidate : null;
47 }
48
49 export function wslUncPathToLinuxPath(windowsPath: string): string | null {
50 const match = /^\\\\(?:wsl\.localhost|wsl\$)\\([^\\]+)(?:\\(.*))?$/i.exec(windowsPath.trim());
51 if (!match) return null;
52
53 const distro = match[1]!;
54 if (!DISTRO_NAME_PATTERN.test(distro)) return null;
55
56 const rest = match[2] ?? "";
57 if (rest.length === 0) return "/";
58
59 return `/${rest.split("\\").filter(Boolean).join("/")}`;
60 }
61
62 function resolveWslHomeUncPath(config: WslConfig, distros: readonly WslDistro[]): string | null {
63 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
64 return distroName ? `\\\\wsl.localhost\\${distroName}\\home` : null;
65 }
66
67 export function resolveWslPickFolderDefaultPath(
68 rawOptions: unknown,
69 config: WslConfig,
70 distros: readonly WslDistro[],
71 userHome: string | null = null,
72 ): string | null {
73 const homePath = resolveWslHomeUncPath(config, distros);
74 if (typeof rawOptions !== "object" || rawOptions === null) {
75 return homePath;
76 }
77
78 const { initialPath } = rawOptions as { initialPath?: unknown };
79 if (typeof initialPath !== "string") {
80 return homePath;
81 }
82
83 const trimmedPath = initialPath.trim();
84 if (trimmedPath.length === 0) {
85 return homePath;
86 }
87 if (trimmedPath.startsWith("\\\\")) {
88 return trimmedPath;
89 }
90
91 const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
92 if (!distroName) {
93 return homePath;
94 }
95
96 const toUncPath = (linuxPath: string) =>
97 `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`;
98
99 const normalizedUserHome = userHome && userHome.startsWith("/") ? userHome : null;
100
101 if (trimmedPath === "~") {
102 return normalizedUserHome ? toUncPath(normalizedUserHome) : homePath;
103 }
104 if (trimmedPath.startsWith("~/")) {
105 const remainder = trimmedPath.slice(2);
106 if (normalizedUserHome) {
107 return toUncPath(`${normalizedUserHome}/${remainder}`);
108 }
109 return homePath ? `${homePath}\\${remainder.replaceAll("/", "\\")}` : null;
110 }
111 if (trimmedPath.startsWith("/")) {
112 return toUncPath(trimmedPath);
113 }
114
115 return homePath;
116 }
117
118 export function isValidDistroName(value: string): boolean {
119 return DISTRO_NAME_PATTERN.test(value);
120 }