11import { hostname } from 'node:os'
2+ import { resolve } from 'node:path'
3+ import fs from 'node:fs/promises'
4+ import { existsSync } from 'node:fs'
25import { logger } from '@nuxt/kit'
36import { execa } from 'execa'
47import { checkPort , getPort } from 'get-port-please'
@@ -8,7 +11,7 @@ import { startSubprocess } from '@nuxt/devtools-kit'
811import { LOG_PREFIX } from '../logger'
912import type { NuxtDevtoolsServerContext } from '../types'
1013
11- export async function setup ( { nuxt, options } : NuxtDevtoolsServerContext ) {
14+ export async function setup ( { nuxt, options, openInEditorHooks , rpc } : NuxtDevtoolsServerContext ) {
1215 const installed = ! ! await which ( 'code-server' ) . catch ( ( ) => null )
1316
1417 const vsOptions = options ?. vscode || { }
@@ -19,20 +22,53 @@ export async function setup({ nuxt, options }: NuxtDevtoolsServerContext) {
1922 let promise : Promise < void > | null = null
2023 const mode = vsOptions ?. mode || 'local-serve'
2124 const computerHostName = vsOptions . tunnel ?. name || hostname ( ) . split ( '.' ) . join ( '' )
25+ const root = nuxt . options . rootDir
26+ const vscodeServerControllerFile = resolve ( root , '.vscode' , '.server-controller-port.log' )
27+
28+ openInEditorHooks . push ( async ( file ) => {
29+ if ( ! existsSync ( vscodeServerControllerFile ) )
30+ return false
31+
32+ // With vscode-server-controller,
33+ // we can open files in VS Code Server
34+ try {
35+ const { port } = JSON . parse ( await fs . readFile ( vscodeServerControllerFile , 'utf-8' ) ) as any
36+ const url = `http://localhost:${ port } /open?path=${ encodeURIComponent ( file ) } `
37+ await fetch ( url )
38+ rpc . broadcast . navigateTo ( '/modules/custom-builtin-vscode' )
39+ return true
40+ }
41+ catch ( e ) {
42+ console . error ( e )
43+ return false
44+ }
45+ } )
2246
2347 async function startCodeServer ( ) {
48+ if ( existsSync ( vscodeServerControllerFile ) )
49+ await fs . rm ( vscodeServerControllerFile , { force : true } )
50+
2451 if ( vsOptions ?. reuseExistingServer && ! ( await checkPort ( port ) ) ) {
2552 loaded = true
26- url = `http://localhost:${ port } /?folder=${ encodeURIComponent ( nuxt . options . rootDir ) } `
53+ url = `http://localhost:${ port } /?folder=${ encodeURIComponent ( root ) } `
2754 logger . info ( LOG_PREFIX , `Existing VS Code Server found at port ${ port } ...` )
2855 return
2956 }
3057
3158 port = await getPort ( { port } )
32- url = `http://localhost:${ port } /?folder=${ encodeURIComponent ( nuxt . options . rootDir ) } `
59+ url = `http://localhost:${ port } /?folder=${ encodeURIComponent ( root ) } `
3360
3461 logger . info ( LOG_PREFIX , `Starting VS Code Server at ${ url } ...` )
3562
63+ // Install VS Code Server Controller
64+ // https://github.com/antfu/vscode-server-controller
65+ execa ( 'code-server' , [
66+ 'serve-local' ,
67+ '--accept-server-license-terms' ,
68+ '--install-extension' ,
69+ 'antfu.vscode-server-controller' ,
70+ ] , { stderr : 'inherit' , stdout : 'ignore' , reject : false } )
71+
3672 startSubprocess (
3773 {
3874 command : 'code-server' ,
0 commit comments