Skip to content

Commit 679af88

Browse files
authored
[vscode-extension] Handling for window os and devbox not installed (#835)
## Summary - Added handling for open in vscode on windows (show as unsupported will implement handling WSL in future). - Added handling for devbox not installed for open in vscode by downloading and using launcher script. ## How was it tested? See steps in #804
1 parent ca8f1fe commit 679af88

File tree

3 files changed

+287
-239
lines changed

3 files changed

+287
-239
lines changed

vscode-extension/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"@types/node": "16.x",
109109
"@types/node-fetch": "^2",
110110
"@types/vscode": "^1.72.0",
111+
"@types/which": "^3.0.0",
111112
"@typescript-eslint/eslint-plugin": "^5.38.1",
112113
"@typescript-eslint/parser": "^5.38.1",
113114
"@vscode/test-electron": "^2.1.5",
@@ -117,7 +118,9 @@
117118
"typescript": "^4.8.4"
118119
},
119120
"dependencies": {
121+
"@types/node": "16.x",
120122
"form-data": "^4.0.0",
121-
"node-fetch": "^2"
123+
"node-fetch": "^2",
124+
"which": "^3.0.0"
122125
}
123-
}
126+
}

vscode-extension/src/openinvscode.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Uri, commands, window } from 'vscode';
1+
import * as os from 'os';
2+
import * as which from 'which';
23
import fetch from 'node-fetch';
34
import { exec } from 'child_process';
45
import * as FormData from 'form-data';
6+
import { Uri, commands, window } from 'vscode';
57
import { chmod, open, writeFile } from 'fs/promises';
68

79
type VmInfo = {
@@ -17,17 +19,21 @@ export async function handleOpenInVSCode(uri: Uri) {
1719
const queryParams = new URLSearchParams(uri.query);
1820

1921
if (queryParams.has('vm_id') && queryParams.has('token')) {
20-
window.showInformationMessage('Setting up devbox');
21-
22-
// getting ssh keys
23-
const response = await getVMInfo(queryParams.get('token'), queryParams.get('vm_id'));
24-
const res = await response.json() as VmInfo;
25-
console.debug("data:");
26-
console.debug(res);
27-
// set ssh config
28-
await setupSSHConfig(res.vm_id, res.private_key);
29-
// connect to remote vm
30-
connectToRemote(res.username, res.vm_id, res.working_directory);
22+
//Not yet supported for windows + WSL - will be added in future
23+
if (os.platform() !== 'win32') {
24+
window.showInformationMessage('Setting up devbox');
25+
// getting ssh keys
26+
const response = await getVMInfo(queryParams.get('token'), queryParams.get('vm_id'));
27+
const res = await response.json() as VmInfo;
28+
console.debug("data:");
29+
console.debug(res);
30+
// set ssh config
31+
await setupSSHConfig(res.vm_id, res.private_key);
32+
// connect to remote vm
33+
connectToRemote(res.username, res.vm_id, res.working_directory);
34+
} else {
35+
window.showErrorMessage('This function is not yet supported in Windows operating system.');
36+
}
3137
} else {
3238
window.showErrorMessage('Error parsing information for remote environment.');
3339
console.debug(queryParams.toString());
@@ -49,13 +55,39 @@ async function getVMInfo(token: string | null, vmId: string | null): Promise<any
4955
return response;
5056
}
5157

58+
async function setupDevboxLauncher(): Promise<any> {
59+
// download devbox launcher script
60+
const gatewayHost = 'https://releases.jetpack.io/devbox';
61+
const response = await fetch(gatewayHost, {
62+
method: 'get',
63+
});
64+
const launcherPath = `${process.env['HOME']}/.config/devbox/launcher.sh`;
65+
66+
try {
67+
const launcherScript = await response.text();
68+
const launcherData = new Uint8Array(Buffer.from(launcherScript));
69+
const fileHandler = await open(launcherPath, 'w');
70+
await writeFile(fileHandler, launcherData, { flag: 'w' });
71+
await chmod(launcherPath, 0o711);
72+
await fileHandler.close();
73+
} catch (err) {
74+
console.error(err);
75+
}
76+
return launcherPath;
77+
}
78+
5279
async function setupSSHConfig(vmId: string, prKey: string) {
53-
// TODO: change this back before to devbox generate ssh-config
54-
// This requires a release for devbox that has generate ssh-config included in it
80+
81+
const devboxBinary = await which('devbox', { nothrow: true });
82+
let devboxPath = 'devbox';
83+
if (devboxBinary === null) {
84+
devboxPath = await setupDevboxLauncher();
85+
}
5586
// For testing change devbox to path to a compiled devbox binary or add --config
56-
exec('devbox generate ssh-config', (error, stdout, stderr) => {
87+
exec(`${devboxPath} generate ssh-config`, (error, stdout, stderr) => {
5788
if (error) {
58-
console.error(`exec error: ${error}`);
89+
window.showErrorMessage('Failed to setup ssh config. Run VSCode in debug mode to see logs.');
90+
console.error(`Failed to setup ssh config: ${error}`);
5991
return;
6092
}
6193
console.debug(`stdout: ${stdout}`);
@@ -71,6 +103,7 @@ async function setupSSHConfig(vmId: string, prKey: string) {
71103
await fileHandler.close();
72104
} catch (err) {
73105
// When a request is aborted - err is an AbortError
106+
window.showErrorMessage('Failed to setup ssh config. Run VSCode in debug mode to see logs.');
74107
console.error(err);
75108
}
76109
}

0 commit comments

Comments
 (0)