-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.js
executable file
·73 lines (64 loc) · 2.49 KB
/
run.js
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
#!/usr/bin/env node
import extract from 'extract-zip';
import figlet from 'figlet';
import { execSync } from 'node:child_process';
import { chmodSync, existsSync, unlinkSync } from 'node:fs';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import delay from './utils/delay.js';
import download from './utils/download.js';
const dir = dirname(fileURLToPath(import.meta.url));
console.log(figlet.textSync('expose-wsl'), '\n');
// Try cleanup leftovers from previous runs, if any
try {
if (existsSync(`${dir}/WSLHostPatcher.zip`)) unlinkSync(`${dir}/WSLHostPatcher.zip`);
if (existsSync(`${dir}/WSLHostPatch.dll`)) unlinkSync(`${dir}/WSLHostPatch.dll`);
if (existsSync(`${dir}/WSLHostPatcher.exe`)) unlinkSync(`${dir}/WSLHostPatcher.exe`);
} catch {
/* ignore */
}
let success = false;
try {
process.stdout.write('⏳ Downloading WSLHostPatcher... ');
await download(
'https://github.com/CzBiX/WSLHostPatcher/releases/latest/download/WSLHostPatcher.zip',
`${dir}/WSLHostPatcher.zip`
);
process.stdout.write('extracting... ');
await extract(`${dir}/WSLHostPatcher.zip`, { dir });
// Remove the zip file
unlinkSync(`${dir}/WSLHostPatcher.zip`);
// Make sure the patcher is executable
chmodSync(`${dir}/WSLHostPatcher.exe`, 0o755);
// Wait a bit to make sure the patcher is ready, then run it
await delay(100);
process.stdout.write('running... ');
execSync(`${dir}/WSLHostPatcher.exe`);
console.log('done ✔️');
process.stdout.write('🎉 WSL should be accessible at: ');
try {
execSync(
"powershell.exe 'Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile | Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress'",
{ stdio: 'inherit' }
);
} catch (err) {
console.log(
"💡 The patcher worked, but powershell.exe could not be called to determine the IP address of the WSL interface; you'll have to figure it out yourself."
);
}
console.log('💡 Make sure to restart your server application(s) before trying to access them!');
success = true;
} catch (err) {
console.log('failed to run ❌');
console.log('🔥 Please report this issue at https://github.com/icflorescu/expose-wsl/issues');
console.log(err.stderr ? err.stderr.toString() : err);
} finally {
// Remove the patcher files
try {
unlinkSync(`${dir}/WSLHostPatcher.exe`);
unlinkSync(`${dir}/WSLHostPatch.dll`);
} catch {
/* ignore */
}
}
process.exit(success ? 0 : 1);