1
- import { Uri , commands , window } from 'vscode' ;
1
+ import * as os from 'os' ;
2
+ import * as which from 'which' ;
2
3
import fetch from 'node-fetch' ;
3
4
import { exec } from 'child_process' ;
4
5
import * as FormData from 'form-data' ;
6
+ import { Uri , commands , window } from 'vscode' ;
5
7
import { chmod , open , writeFile } from 'fs/promises' ;
6
8
7
9
type VmInfo = {
@@ -17,17 +19,21 @@ export async function handleOpenInVSCode(uri: Uri) {
17
19
const queryParams = new URLSearchParams ( uri . query ) ;
18
20
19
21
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
+ }
31
37
} else {
32
38
window . showErrorMessage ( 'Error parsing information for remote environment.' ) ;
33
39
console . debug ( queryParams . toString ( ) ) ;
@@ -49,13 +55,39 @@ async function getVMInfo(token: string | null, vmId: string | null): Promise<any
49
55
return response ;
50
56
}
51
57
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
+
52
79
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
+ }
55
86
// 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 ) => {
57
88
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 } ` ) ;
59
91
return ;
60
92
}
61
93
console . debug ( `stdout: ${ stdout } ` ) ;
@@ -71,6 +103,7 @@ async function setupSSHConfig(vmId: string, prKey: string) {
71
103
await fileHandler . close ( ) ;
72
104
} catch ( err ) {
73
105
// 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.' ) ;
74
107
console . error ( err ) ;
75
108
}
76
109
}
0 commit comments