Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wslPathParsing.ts

121 lines4.1 KB

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